如何在数组中获取数组?

时间:2011-04-28 13:52:10

标签: php arrays multidimensional-array

我的阵列就是这样来的

 Array
    (
        [mes_id] => 1852
        [frm_id] => 376
        [network] => Array
            (
                [0] => Array
                    (
                        [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
                )

        )

    )

如何从每个阵列中获取警报标题?

请帮忙, 感谢。

3 个答案:

答案 0 :(得分:5)

更新后

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

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

print_r($titles);

答案 1 :(得分:2)

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

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

如果有多个

改变你的例子:

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

答案 2 :(得分:0)

如果那是$ a,

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