Zona Duo:
Estoy tratando de cambiar el orden de la matriz de php matriz.
$arrKeysWithoutValues = array("id", "mobile", "stockCode");
$new_data = array();
foreach ($arrKeysWithoutValues as $key) {
//$list is array of array
$new_data[$key] = array_column($list, $key);
}
//what I received is as follows
//Array ( [id] => Array ( [0] => 4967 [1] => 4965 ) [mobile] => Array ( [0] => ****0030008 [1] => ****0030009 ) [stockCode] => Array ( [0] => sh600036 [1] => sh600036 ) )
//what I need is as follows
//Array ( [0] => Array ( [id] => 4967 [mobile] => ****0030008 [stockCode] => sh600036 ) [1] => Array ( [id] => 4965 [mobile] => ****0030009 [stockCode] => sh600036 ) )
Lo que me dieron y lo que hace falta es como en los comentarios anteriores. ¿Como puedo resolver esto?
Ejemplo de estructura para la lista es como sigue.
Array ( [0] => Array ( [id] => 4967 [stockCode] => sh600036 [mobile] => ****0030008 ) [1] => Array ( [id] => 4965 [stockCode] => sh600036 [mobile] => ****0030009 ) )
Kevin:
No toque y empujar las teclas.
$new_data[$key] = array_column($list, $key);
// ^ this will yield different results
Se utiliza la matriz orden y darles la vuelta en keys
, a continuación, array_merge
con la lista original, por lo que entonces se mantiene el orden deseado. Al igual que:
$new_data = array();
$keys = array_flip($arrKeysWithoutValues); // flip and turn it to keys
foreach ($list as $l) {
$new_data[] = array_merge($keys, $l);
// ^^ merge them
}