Last letters remove form array in foreach loop

Multi tool use
Multi tool use


Last letters remove form array in foreach loop



I want to remove the last few letters from an array in a for-each loop. I am trying to show bl_date without /2018. Now its showing 07/10/2018 & 06/30/2018. How can echo like this 07/10 & 06/30?


bl_date


/2018


07/10/2018 & 06/30/2018


07/10 & 06/30



Array


Array
(
[0] => stdClass Object
(
[id] => 18
[bl_user] => 61
[bl_date] => 07/10/2018
)

[1] => stdClass Object
(
[id] => 17
[bl_user] => 61
[bl_date] => 06/30/2018

)

)



PHP


$resultstr = array();
foreach ($billings as $billing) {
$resultstr = $billing->bl_date;
}
echo implode(" & ",$resultstr);




2 Answers
2



One option is to use substr() to remove the last 5 characters of the string


substr()



Like:


$resultstr = array();
foreach ($billings as $billing) {
$resultstr = substr( $billing->bl_date, 0, -5 );
}
echo implode(" & ",$resultstr);



This will result to:


07/10 & 06/30



Doc: substr()



You need to use substr function:


foreach ($billings as $billing) {
$resultstr = substr($billing->bl_date, 0, 5);
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

XuZBhHvcRBqiVPSzN15nkxNFMHn44Jq lVl5MD3oJB,WRy 7M7
an,nuPT4cfENgTF3As9N0GORFEgD8xobHdYk,Hzm0,i9 9A0lc8eAvzB6Da5H7ADer3iKplVyrQ KWDq

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

Audio Livestreaming with Python & Flask