Zone Duo :
I am trying to change the order of php array of array.
$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 ) )
What I received and what I needed is as in above comments. How can I solve this?
Sample structure of the list is as follows.
Array ( [0] => Array ( [id] => 4967 [stockCode] => sh600036 [mobile] => ****0030008 ) [1] => Array ( [id] => 4965 [stockCode] => sh600036 [mobile] => ****0030009 ) )
Kevin :
Don't touch and push the keys.
$new_data[$key] = array_column($list, $key);
// ^ this will yield different results
You use the order array and flip them into keys
, then array_merge
it with the original list, so then you keep the desired order. Like so:
$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
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=293802&siteId=1