我怎样才能有效地拆分成数组的关联数组的关键? [英] How can I efficiently split an array into its associative key arrays?

查看:166
本文介绍了我怎样才能有效地拆分成数组的关联数组的关键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何能我分裂单个阵列到它的子键?

  $ ARR =阵列(
             0 =>阵列(
                        '富'=> '1',
                        '酒吧'=> '一个'
                       )
             1 =>阵列(
                        '富'=> '2',
                        '酒吧'=> B
                       )
             2 =>阵列(
                        '富'=> '3',
                        '酒吧'=> 'C'
                       )
            );

什么是返回foo的数组,并分别酒吧最有效的方式?

我需要在这里:

  $富=阵列('1','2','3');
$巴=阵列('A','B','C');

我希望有一个聪明的办法做到这一点使用 array_map 或类似的东西。任何想法?

或者我通​​过有循环,并建立每个阵列呀?是这样的:

 的foreach($改编为V $){
    $ foo的[] = $ V ['富'];
    $栏[] = $ V ['酒吧'];
}


解决方案

在机缘巧合之下,我需要做几乎同样的事情在今天早些时候。您可以使用 array_map()结合 array_shift()

  $富= array_map('array_shift',&安培; $ ARR);
$巴= array_map('array_shift',&安培; $ ARR);

注意 $改编通过引用传递!如果你不这样做,那么每次它会返回 $改编的内容[<索引>] ['富'] 。然而,同样是因为参考 - 你将不能再使用 $改编,所以如果你需要做的 - 首先将其复制

缺点是,你的数组键需要以同样的方式在你的例子可以订购,因为 array_shift()实际上并不知道这个键是什么。它不会下阵列上工作:

  $ ARR =阵列(
    0 =>阵列(
        '富'=> '1',
        '酒吧'=> '一个'
    )
    1 =>阵列(
        '酒吧'=> B,
        '富'=> '2'
    )
    2 =>阵列(
        '富'=> '3',
        '酒吧'=> 'C'
    )
);

更新:

阅读意见后,很明显,我的解决方案触发 E_DE preCATED 警告呼叫时间传递通过引用。这里由@Baba,所建议的(并接受作为一个答案)替代这需要两个所需的密钥是第二维阵列的第一个和最后一个元件的优点是:

  $富= array_map('array_shift',$ ARR);
$巴= array_map('array_pop',$ ARR);

How can I split a single array into it's sub-keys?

$arr = array(
             0 => array(
                        'foo' => '1',
                        'bar' => 'A'
                       ),
             1 => array(
                        'foo' => '2',
                        'bar' => 'B'
                       ),
             2 => array(
                        'foo' => '3',
                        'bar' => 'C'
                       )
            );

What is the most efficient way to return an array of foo and bar separately?

I need to get here:

$foo = array('1','2','3');
$bar = array('A','B','C');

I'm hoping there's a clever way to do this using array_map or something similar. Any ideas?

Or do I have to loop through and build each array that way? Something like:

foreach ($arr as $v) {
    $foo[] = $v['foo'];
    $bar[] = $v['bar'];
}

解决方案

In a lucky coincidence, I needed to do almost the exact same thing earlier today. You can use array_map() in combination with array_shift():

$foo = array_map('array_shift', &$arr);
$bar = array_map('array_shift', &$arr);

Note that $arr is passed by reference! If you don't do that, then each time it would return the contents of $arr[<index>]['foo']. However, again because of the reference - you won't be able to reuse $arr, so if you need to do that - copy it first.

The downside is that your array keys need to be ordered in the same way as in your example, because array_shift() doesn't actually know what the key is. It will NOT work on the following array:

$arr = array(
    0 => array(
        'foo' => '1',
        'bar' => 'A'
    ),
    1 => array(
        'bar' => 'B',
        'foo' => '2'
    ),
    2 => array(
        'foo' => '3',
        'bar' => 'C'
    )
);

Update:

After reading the comments, it became evident that my solution triggers E_DEPRECATED warnings for call-time-pass-by-reference. Here's the suggested (and accepted as an answer) alternative by @Baba, which takes advantage of the two needed keys being the first and last elements of the second-dimension arrays:

$foo = array_map('array_shift', $arr);
$bar = array_map('array_pop', $arr);

这篇关于我怎样才能有效地拆分成数组的关联数组的关键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆