I have a foreach loop
in php. I want to store the output values of this loop in a variable. How can I do it? I want to store the given loop values in $my_variable
$my_variable = foreach( $orders as $order ){
echo get_post_meta( $order->get_id(), '_tracking_box', true ).', ';
}
$my_variable should give direct values not the array
CodePudding user response:
Add the values to an array. Then use implode()
to concatenate them into a string with ,
separators.
$tracking_array = [];
foreach( $orders as $order ){
$tracking_array[] = get_post_meta( $order->get_id(), '_tracking_box', true );
}
$tracking_box = implode(', ', $tracking_array);