php array_reverse获取最后一个循环

So im trying to reverse array and ADD something to last item on loop, when i run this code:

$data = ($json['data']);
foreach($data as $item){
    if(isset($item['Metadata']) && $item['Metadata']['id_suscripcion'] == $id_sus){
        if ($item === end($data))
            echo $item['id'].' LAST ELEMENT!';
        }      
    }

I get this 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 the problem is when I add array_reverse() $data = array_reverse($json['data']); LAST ELEMENT! will not show, any suggestions?

If you need to add something for every array inside $data and something extra for the last, I suggest you break it up or use a for loop to 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!';
        }      
    }

I know you said that you want it to be executed in the loop, but you may be better served to have the loop(s) do the work they should do, and after the loops, do your work on the last element:

$data = ($json['data']);
foreach($data as $item){
    // Other work for looping...
}

$last = end($data);
$key  = key($data);

if(isset($last['Metadata']) && $last['Metadata']['id_suscripcion'] == $id_sus){
    echo $last['id'].' LAST ELEMENT!';
}

This would cut down on the number of tests in the loop.

Another option may be to get the last key prior to looping (to cut down on looped function calls):

$data = ($json['data']);

// $data = array_reverse($data);

end($data);
$last_key = key($data);

foreach($data as $key => $item){
    if(isset($item['Metadata']) && $item['Metadata']['id_suscripcion'] == $id_sus){
        if ($key === $last_key)
            echo $item['id'].' LAST ELEMENT!';
    }      
}

And finally, for perhaps the real answer, if you are trying to add something to the array, you will want to add it to the actual item (what you have there is a copy). You can do this by using the $item as a reference, or by referencing the item by key in the containing array:

foreach($data as &$item){
    if(isset($item['Metadata']) && $item['Metadata']['id_suscripcion'] == $id_sus){
        if ($key === $last_key)
            $item[] = 'LAST ELEMENT!';
    }      
}

Or:

foreach($data as $key => $item){
    if(isset($item['Metadata']) && $item['Metadata']['id_suscripcion'] == $id_sus){
        if ($key === $last_key)
            $data[$key][] = 'LAST ELEMENT!';
    }      
}

If you were to go with my first code-block, it would look something like:

foreach($data as $item){
    // Other work for looping...
}

end($data);
$key  = key($data);
$last &= $data[$key];

if(isset($last['Metadata']) && $last['Metadata']['id_suscripcion'] == $id_sus){
    $last[] = 'LAST ELEMENT!';
}

And one final hint: isset and empty do not throw errors when referencing variables or array/object members that are not set, so you can (and kind of should) test the whole path before accessing it, for sanity and safety:

if (
    isset($last['Metadata']['id_suscripcion']) && 
    $last['Metadata']['id_suscripcion'] == $id_sus
) {
    // do a thing
}

EDIT:

If this is still not working when using array_reverse it is likely that if this is coming from json, it may be an object. Be sure to decode json with JS objects as arrays: http://php.net/manual/en/function.json-decode.php