Reputation: 599
I have the following array:
Array (
[0] => Array
(
[example] => 'h'
)
[1] => Array
(
[example] => 'e'
)
[2] => Array
(
[example] => 'l'
)
[3] => Array
(
[example] => 'p'
)
)
And I am wondering how can I change this array to look like this instead.
Array( [example] => 'h', [example] => 'e', [example] => 'l', [example] => 'p')
I have tried using a nested foreach loop but using that I only get the values and not as an array.
Upvotes: 0
Views: 2095
Reputation: 16828
There are a couple of ways to reach the values of a multidimensional array:
$array = array(
0=>array('example'=>'h'),
1=>array('example'=>'e'),
2=>array('example'=>'l'),
3=>array('example'=>'p')
);
1, If looping through the array:
foreach($array as $key=>$value){
echo $value['example'];
}
2, Call a value directly:
echo $array[0]['example'];
echo $array[1]['example'];
echo $array[2]['example'];
echo $array[3]['example'];
It is impossible to create an array the way that you mentioned (having 4 'example' as keys). Take the following for example:
$array['example'] = 'h';
$array['example'] = 'e';
$array['example'] = 'l';
$array['example'] = 'p';
echo $array['example'];
The output would be p
because you are simply overwriting the variable $array['example']
each time.
Upvotes: 2