开发者

How to fetch array within array?

开发者 https://www.devze.com 2023-03-01 13:19 出处:网络
my array is coming like this Array ( [mes_id] => 1852 [frm_id] => 376 [network] => Array ( [0] => Array

my array is coming like this

 Array
    (
        [mes_id] => 1852
        [frm_id] => 376
        [network] => Array
            (
                [0] => Array
                    (
             开发者_StackOverflow中文版           [alerttitle] => enter_nv
                        [alertImageUrl] => photos/952a7253eda21b936489d0f7b35f953bth.jpeg
                        [alertDescription] => (1) Network Invitation(s)
                        [alertType] => Network
                        [count] => 1
                        [id] => 376
                    )

            )
[msg] => Array
        (
            [0] => Array
                (
                    [alerttitle] => Raven Lexy
                    [alertImageUrl] => photos/81951b37ad01c4aa65662956f178214eth.jpeg
                    [alertDescription] => (1) New Message(s)
                    [alertType] => New Message
                    [count] => 1
                    [id] => 51
                )

            [1] => Array
                (
                    [alerttitle] => Raven Lexy
                    [alertImageUrl] => photos/81951b37ad01c4aa65662956f178214eth.jpeg
                    [alertDescription] => (1) New Message(s)
                    [alertType] => New Message
                    [count] => 1
                    [id] => 51
                )

        )

    )

how can i fetch alerttitle from each array?

please help, thanks.


After your update

$titles = array();
$titles[] = $array['network'][0]['alerttitle'];

foreach ($array['msg'] as $item) {
  $titles[] = ['alerttitle'];
}

print_r($titles);


$array['network'][0]['alerttitle'];

or

for ($i=0; $i < count($array['network']); $i++) { 
  echo $array['network'][$i]['alerttitle'];
}

if there is more than one

to your changed example:

$array = array(
'foobar' => array(
    'foo' => 'afoasfd',
    'alerttitle' => 'fasfdasf'
  ), 
  'alerttitle' => 'foobar'
);

$output = array();

array_walk_recursive($array, 'foo', &$output);
function foo($val, $key, $obj){
  if($key == 'alerttitle') array_push($obj, $val);
}
print_r($output);


If that's $a,

$alertTitle = $a['network'][0]['alerttitle'];
0

精彩评论

暂无评论...
验证码 换一张
取 消