Voici un défi intéressant. J'ai un tableau de sous-tableaux au format suivant:
array
(
a => array ( id = 20, name = chimpanzee )
b => array ( id = 40, name = meeting )
c => array ( id = 20, name = dynasty )
d => array ( id = 50, name = chocolate )
e => array ( id = 10, name = bananas )
f => array ( id = 50, name = fantasy )
g => array ( id = 50, name = football )
)
Et je voudrais le grouper dans un nouveau tableau basé sur le champ id dans chaque sous-tableau.
array
(
10 => array
(
e => array ( id = 10, name = bananas )
)
20 => array
(
a => array ( id = 20, name = chimpanzee )
c => array ( id = 20, name = dynasty )
)
40 => array
(
b => array ( id = 40, name = meeting )
)
50 => array
(
d => array ( id = 50, name = chocolate )
f => array ( id = 50, name = fantasy )
g => array ( id = 50, name = football )
)
)
Merci!
$arr = array();
foreach ($old_arr as $key => $item) {
$arr[$item['id']][$key] = $item;
}
ksort($arr, SORT_NUMERIC);
foreach($array as $key => $value){
$newarray[$value['id']][$key] = $value;
}
var_dump($newarray);
part de gâteau ;)
Le code suivant adapte le code de @Tim Cooper pour atténuer les erreurs Undefined index: id
dans le cas où l’un des tableaux internes ne contient pas de id:
$arr = array();
foreach($old_arr as $key => $item)
{
if(array_key_exists('id', $item))
$arr[$item['id']][$key] = $item;
}
ksort($arr, SORT_NUMERIC);
Cependant, il abandonnera les tableaux internes sans identifiant.
Par exemple.
$old_arr = array(
'a' => array ( 'id' => 20, 'name' => 'chimpanzee' ),
'b' => array ( 'id' => 40, 'name' => 'meeting' ),
'c' => array ( 'id' => 20, 'name' => 'dynasty' ),
'd' => array ( 'id' => 50, 'name' => 'chocolate' ),
'e' => array ( 'id' => 10, 'name' => 'bananas' ),
'f' => array ( 'id' => 50, 'name' => 'fantasy' ),
'g' => array ( 'id' => 50, 'name' => 'football' ),
'h' => array ( 'name' => 'bob' )
);
lâchera complètement le tableau 'h'.
<?php
$array = array(
'a' => array ( 'id' => 20, 'names' => 'chimpanzee' ),
'b' => array ( 'id' => 40, 'name' => 'meeting' ),
'c' => array ( 'id' => 20, 'name' => 'dynasty' ),
'd' => array ( 'id' => 50, 'name' => 'chocolate' ),
'e' => array ( 'id' => 10, 'name' => 'bananas' ),
'f' => array ( 'id' => 50, 'name' => 'fantasy' ),
'g' => array ( 'id' => 50, 'name' => 'football' )
);
$result = array();
foreach($array as $key => $value){
$id = $value['id'];
if(!isset($result[$id])) $result[$id] = array();
$result[$id] = array($key => $value);
}
var_dump($result);
?>
Vous pouvez aussi utiliser Arrays :: gorupBy form ouzo-goodies :
$groupBy = Arrays::groupBy($array, Functions::extract()->id);
print_r($groupBy);
Et résultat:
Array
(
[20] => Array
(
[0] => Array
(
[id] => 20
[name] => chimpanzee
)
[1] => Array
(
[id] => 20
[name] => dynasty
)
)
[40] => Array
(
[0] => Array
(
[id] => 40
[name] => meeting
)
)
[50] => Array
(
[0] => Array
(
[id] => 50
[name] => chocolate
)
[1] => Array
(
[id] => 50
[name] => fantasy
)
[2] => Array
(
[id] => 50
[name] => football
)
)
[10] => Array
(
[0] => Array
(
[id] => 10
[name] => bananas
)
)
)