0

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;
});

2 Answers2

1

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

-2

You should create a global boolean variable which acts as a flag

<?php
$walk_arr = [
    0 => "A",
    1 => "B",
    3 => "D",
    5 => "F",
];
global $stop_walk;
$stop_walk = false;
$walk_func = function(&$v, $k) {
    global $stop_walk;
    if ($k === 3) {
        $stop_walk = true;
    }
    if ($stop_walk) { 
        return;
    }
    $v .= " changed";
};
unset($stop_walk);

array_walk($walk_arr, $walk_func);
print_r($walk_arr);
print $stop_walk;

See it in action: https://3v4l.org/6kceo

BTW: Can't you just output the first row?

echo $array[1995]['sub_title']
yunzen
  • 32,854
  • 11
  • 73
  • 106
  • Using global is an overhead I my opinion. I think using closure would be enough for your code example – Viktar Pryshchepa Feb 01 '19 at 12:53
  • Can I use closure here? I don't think so. that variable won't be consistant, as the `walk_func` will be called every time – yunzen Feb 01 '19 at 13:27
  • 1
    remove global, remove unset, change signature to `$walk_func = function(&$v, $k) use (&$stop_walk) {` and check. If you have to use global in most cases means that the code should be refactored – Viktar Pryshchepa Feb 01 '19 at 13:38
  • I didn't knew the `use` for PHP lambda before. Thanks for pointing this out. https://3v4l.org/26CNN BTW: Since which version does PHP have this? – yunzen Feb 01 '19 at 13:45
  • you can remove line 8 from your example. Looks like this was introduced with the Closure (PHP 5 >= 5.3.0, PHP 7) – Viktar Pryshchepa Feb 01 '19 at 13:49
  • I never read through all of the examples. I walways thought, PHP was getting it wrong by not telling closures and lambdas apart. But I never felt, I was missing something. In JavaScript, I use closures all the time, but in PHP? – yunzen Feb 01 '19 at 14:14
  • @ViktarPryshchepa I did this with nested anonymous function and closure: https://3v4l.org/hr6Zs – yunzen Feb 01 '19 at 14:33
  • Let me try to example of the usage. You have method that takes a callback as a parameter. And want to pass variable that would be used in/out of the callback – Viktar Pryshchepa Feb 01 '19 at 14:39