0

my array is coming like this

 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
                )

        )

    )

how can i fetch alerttitle from each array?

please help, thanks.

3 Answers 3

5

After your update

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

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

print_r($titles);
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

please see my modified content
added a new example where i walk through the array recursive to get all alerttitles.
0

If that's $a,

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.