arrays - php array_reverse get last in loop -
so im trying reverse array , add last item on loop, when run code:
$data = ($json['data']); foreach($data $item){ if(isset($item['metadata']) && $item['metadata']['id_suscripcion'] == $id_sus){ if ($item === end($data)) echo $item['id'].' last element!'; } }
i results:
array ( [ticketnumber] => 172252526529700541 [metadata] => array ( [id_suscripcion] => 0000000000004314 ) [id] => 193172252526529772 ) array ( [ticketnumber] => 172242526591400527 [metadata] => array ( [id_suscripcion] => 0000000000004314 ) [id] => 193172242526591380 ) array ( [ticketnumber] => 172232526775600149 [metadata] => array ( [id_suscripcion] => 0000000000004314 ) [id] => 193172232526775687 ) 193172232526775687 last element!
now problem when add array_reverse() $data = array_reverse($json['data']);
last element! not show, suggestions?
if need add every array inside $data , last, suggest break or use loop count.
$data = array_reverse($json['data']); for($i = 0; $i < count($data); $i++){ if( isset( $data[$i]['metadata'] ) && $data[$i]['metadata']['id_suscripcion'] == $id_sus){ if ( $i ===0 || $data[$i] === reset( $data ) ) echo $item['id'].' last element!'; } }
Comments
Post a Comment