-2

我有一个这样的数组

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_arr,$sup_arr,$cat_arr,$brand_arr.Means 这样的单个数组。如果我打印 $store_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'];
    }
}

但它给了我无关的数据数组。任何人都可以建议我如何得到它。提前谢谢

4

3 回答 3

2

这是你想要的吗?您有一个不需要的额外循环级别。

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'];
  ...
}
于 2013-07-04T04:54:19.453 回答
1

嗯......你的意思是这样的吗?

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
)
于 2013-07-04T04:55:32.570 回答
1

尝试这个:

foreach($result as $item) {

    $store_arr[$item['store_id']] = $item['store_str'];
}
于 2013-07-04T04:58:04.440 回答