phploopsarray-walk

how to terminate array_walk on first loop?


I need to print only one sub_title of first child(1955) array.

Here is some sample code (that works) to show what I mean.

$array = Array
(
    [1955] => Array
        (
            [sub_title] => subtitle
            [sub_content] => content
        )
    [1957] => Array
        (
            [sub_title] => subtitle
            [sub_content] => conent
        )
    [1958] => Array
        (
            [sub_title] => subtitle
            [sub_content] => conent
        )
)
array_walk($array, function($item){
    echo $item['sub_title'];
    break;
});

Solution

  • As far as I understand your task, you need to get the first element of the array. Use reset(array $array) function to get the first element, than get the value. So your code should look like $fistElement = reset($array); echo $firstElement['sub_titile']; Please also read the documentation on array function and don't try to sew with a hammer