Php 如何将多维数组更改为单个数组
我有一个这样的数组Php 如何将多维数组更改为单个数组,php,arrays,Php,Arrays,我有一个这样的数组 Array ( [0] => Array ( [id] => 1 [store_id] => 2 [store_str] => Andrew Brennan [supplier_id] => 51 [supplier_str] => Dyson [brand_id] => 4 [brand_str] => Dy
Array
(
[0] => Array
(
[id] => 1
[store_id] => 2
[store_str] => Andrew Brennan
[supplier_id] => 51
[supplier_str] => Dyson
[brand_id] => 4
[brand_str] => Dyson
[category_id] => 2
[category_str] => SDA
[category_short_name] => SDA
[added_by] => 1
[inserted_on] => 1351664553
[status] => 1
[status_str] => Active
)
[1] => Array
(
[id] => 3
[store_id] => 4
[store_str] => Berney Crossan & Sons Ltd
[supplier_id] => 51
[supplier_str] => Dyson
[brand_id] => 4
[brand_str] => Dyson
[category_id] => 2
[category_str] => SDA
[category_short_name] => SDA
[added_by] => 1
[inserted_on] => 1351664553
[status] => 1
[status_str] => Active
) )
但我想将它们显示为单个数组,如$store\u arr、$sup\u arr、$cat\u arr、$brand\u arr。这意味着如果我打印$store\u arr,它应该是
Array(
[2] => Andrew Brennan
[4] => Berney Crossan & Sons Ltd
)
所有其他数组都应该是这样的。我试着
foreach($result as $k => $v) {
foreach($v as $key=>$value) {
$store_arr[$key['store_id']] = $key['store_str'];
}
}
但它给我提供了红外相关数据阵列。有人能建议我如何获得它吗?提前Thanx这是你想要的吗?你有一个额外的循环,这是不需要的
foreach ($result as $store) {
$id = $store['store_id'];
$store_arr[$id] = $store['store_str'];
$sup_arr[$id] = $store['supplier_str'];
$cat_arr[$id] = $store['category_str'];
...
}
嗯。。。。。你是说像这样的事吗
foreach ($result as $i => $arr) {
foreach ($arr as $key => $value) {
$new[$key][$i]=$value;
}
}
var_dump($new['store_str']);
结果
Array(
[0] => Andrew Brennan
[1] => Berney Crossan & Sons Ltd
)
试试这个:
foreach($result as $item) {
$store_arr[$item['store_id']] = $item['store_str'];
}
真是太棒了……工作很顺利。谢谢@Barmar