Last letters remove form array in foreach loop


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.

Popular posts from this blog

C++ thread error: no type named ‘type’ MINGW

Decreasing a static image progress bar horizontally in SDL

How to input without newline? (Python)