Reputation: 223
I have an array from my database like this:
Array
(
[0] => Array
(
[A] => Array
(
[0] => A
[1] => 0
[2] => 0
[3] => 0
)
)
[1] => Array
(
[C] => Array
(
[0] => C
[1] => 6.9167
[2] => 0
[3] => 0
)
)
[2] => Array
(
[D] => Array
(
[0] => D
[1] => 6.9167
[2] => 0
[3] => 0
)
)
)
How can I remove the first key ( [0] => A, [0] => C, [0] => D )
from this multidimensional array?
This is my desired output of the array:
Array
(
[0] => Array
(
[A] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
)
[1] => Array
(
[C] => Array
(
[0] => 6.9167
[1] => 0
[2] => 0
)
)
[2] => Array
(
[D] => Array
(
[0] => 6.9167
[1] => 0
[2] => 0
)
)
)
Upvotes: 2
Views: 3259
Reputation: 2993
use unset
$array = array(
array(
'A' => array(
'A', 0, 0, 0
)
),
array(
'C' => array(
'C', 0, 0, 0
)
)
);
foreach ($array as $key => $value) {
$erase = key($value);
unset($array[$key][$erase][0]);
// this resets the ordinal sequence from 1,2,3 to 0,1,2
$a[$key][$erase] = array_values($array[$key][$erase]);
}
Upvotes: 0
Reputation: 1673
This is terrible but should work for you. It uses the level 2 key as a value of the element to delete on the next level
$array = array(
array('A' => array('A', 100, 200)),
array('B' => array('B', 100, 200)),
);
foreach($array as $key => $subarray){
foreach($subarray as $letter_key => $target_array){
$index_to_delete = array_search($letter_key, $target_array);
unset($array[$key][$letter_key][$index_to_delete]);
}
}
print_r($array);
Upvotes: 0
Reputation: 78994
At it's simplest (I don't have time for a non loop one):
foreach($array as $k1 => $inner) {
foreach($inner as $k2 => $value) {
unset($array[$k1][$k2][0]);
}
}
Upvotes: 1