php中组合数组形成多维数组

发布于 2024-10-04 21:33:53 字数 846 浏览 0 评论 0原文

我知道有很多答案,但我似乎无法得到正确的答案。我有以下数组和我尝试过的内容:

$a = array ( 0 => '1421' , 1 => '2241' );
$b = array ( 0 => 'teststring1' , 1 => 'teststring2' );
$c = array ( 0 => 'teststring3' , 1 => 'teststring4' );
$d = array ( 0 => 'teststring5' , 1 => 'teststring6' );

$e = array_combine($a, array($b,$c,$d) );

但是这样我得到了错误 array_combine() [function.array-combine]: 两个参数应该具有相同数量的元素

我知道这是因为 $a 的数组值不是键。这就是为什么我来这里是为了看看我是否可以得到一些帮助,找到一个可以帮助我使它看起来像这样的答案:

array(2) {

  [1421]=>array( [0] => teststring1
                 [1] => teststring3
                 [2] => teststring5
                )

  [2241]=>array( [0] => teststring2
                 [1] => teststring4
                 [2] => teststring6
               )


}

I know there's a ton of answers but I can't seem to get it right. I have the following arrays and what I've tried:

$a = array ( 0 => '1421' , 1 => '2241' );
$b = array ( 0 => 'teststring1' , 1 => 'teststring2' );
$c = array ( 0 => 'teststring3' , 1 => 'teststring4' );
$d = array ( 0 => 'teststring5' , 1 => 'teststring6' );

$e = array_combine($a, array($b,$c,$d) );

But with this I get the error array_combine() [function.array-combine]: Both parameters should have an equal number of elements.

I know it's because the $a's array values aren't keys. That's why I'm coming here to see if I could get some help with an answer that can help me make it look something like this:

array(2) {

  [1421]=>array( [0] => teststring1
                 [1] => teststring3
                 [2] => teststring5
                )

  [2241]=>array( [0] => teststring2
                 [1] => teststring4
                 [2] => teststring6
               )


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埋葬我深情 2024-10-11 21:33:53

如果您可以控制创建数组,则应该像这样创建它们:

$a = array ('1421' ,'2241');
$b = array ('teststring1', 'teststring3', 'teststring5');
$c = array ('teststring2', 'teststring4', 'teststring6');

$e = array_combine($a, array($b,$c) );

如果没有,则必须循环它们:

$result = array();
$values = array($b, $c, $d);

foreach($a as $index => $key) {
    $t = array();
    foreach($values as $value) {
        $t[] = $value[$index];
    }
    $result[$key]  = $t;
}

DEMO

If you have control over creating the arrays, you should create them like:

$a = array ('1421' ,'2241');
$b = array ('teststring1', 'teststring3', 'teststring5');
$c = array ('teststring2', 'teststring4', 'teststring6');

$e = array_combine($a, array($b,$c) );

If not, you have to loop over them:

$result = array();
$values = array($b, $c, $d);

foreach($a as $index => $key) {
    $t = array();
    foreach($values as $value) {
        $t[] = $value[$index];
    }
    $result[$key]  = $t;
}

DEMO

满身野味 2024-10-11 21:33:53

这是函数式编码风格的一行代码。调用 array_map()一个 null 函数参数后跟“values”数组将生成所需的子数组结构。 array_combine() 进行 key=>value 关联。

代码(演示

var_export(array_combine($a, array_map(null, $b, $c, $d)));

输出:

array (
  1421 => 
  array (
    0 => 'teststring1',
    1 => 'teststring3',
    2 => 'teststring5',
  ),
  2241 => 
  array (
    0 => 'teststring2',
    1 => 'teststring4',
    2 => 'teststring6',
  ),
)

超级干净,对吧?我知道。当您无法控制初始数组生成步骤时,这是一个有用的小技巧。

Here is a one-liner in a functional coding style. Calling array_map() with a null function parameter followed by the "values" arrays will generate the desired subarray structures. array_combine() does the key=>value associations.

Code (Demo)

var_export(array_combine($a, array_map(null, $b, $c, $d)));

Output:

array (
  1421 => 
  array (
    0 => 'teststring1',
    1 => 'teststring3',
    2 => 'teststring5',
  ),
  2241 => 
  array (
    0 => 'teststring2',
    1 => 'teststring4',
    2 => 'teststring6',
  ),
)

Super clean, right? I know. It's a useful little trick when you don't have control of the initial array generation step.

私藏温柔 2024-10-11 21:33:53

这是 array_merge_recursive 的新版本,它将处理整数键。让知道进展如何。

$a = array ( 0 => '1421' , 1 => '2241' );
$b = array ( 0 => 'teststring1' , 1 => 'teststring2' );
$c = array ( 0 => 'teststring3' , 1 => 'teststring4' );
$d = array ( 0 => 'teststring5' , 1 => 'teststring6' );

$e = array_combine($a, array_merge_recursive2($b, $c, $d));
echo "<pre>";
print_r($e);



function array_merge_recursive2() {
    $args = func_get_args();
    $ret = array();

    foreach ($args as $arr) {
        if(is_array($arr)) {
            foreach ($arr as $key => $val) {
                $ret[$key][] = $val;
            }
        }
    }
    return $ret;
}

Here's a new version of array_merge_recursive which will handle integer keys. Let know how it goes.

$a = array ( 0 => '1421' , 1 => '2241' );
$b = array ( 0 => 'teststring1' , 1 => 'teststring2' );
$c = array ( 0 => 'teststring3' , 1 => 'teststring4' );
$d = array ( 0 => 'teststring5' , 1 => 'teststring6' );

$e = array_combine($a, array_merge_recursive2($b, $c, $d));
echo "<pre>";
print_r($e);



function array_merge_recursive2() {
    $args = func_get_args();
    $ret = array();

    foreach ($args as $arr) {
        if(is_array($arr)) {
            foreach ($arr as $key => $val) {
                $ret[$key][] = $val;
            }
        }
    }
    return $ret;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文