简体   繁体   English

在 PHP 中的多个数组的相同键下创建新数组

[英]Create new Array under same key from multiple Array in PHP

I have the following array.我有以下数组。

$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/02/2019");
$c = array("08/01/2020", "08/02/2019");

print_r($a);
print_r($b);
print_r($b);

and the output is输出是

Array(
    [0] => Algebra
    [1] => Arithmetic
  )

Array(
    [0] => 08/01/2020
    [1] => 08/01/2019
  )

Array(
    [0] => 08/02/2020
    [1] => 08/02/2019
  )

And I want Array in the following structure.我想要以下结构中的 Array 。

Array(
 [0] => Algebra,08/01/2020,08/02/2020
 [1] => Arithmetic,08/01/2019,08/02/2019
)

I have tried $results = array_merge_recursive($a, $b, $c);我试过$results = array_merge_recursive($a, $b, $c); but its not giving desire output.但它没有给出欲望输出。

Thanks for help in advance.提前感谢您的帮助。

You don't get anything built-in for this purpose.您没有为此目的内置任何内容。 You need to build a custom function for this.您需要为此构建一个自定义函数。 You can try this-你可以试试这个——

<?php
$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/01/2019");
$c = array("08/02/2020", "08/02/2019");

function mergeAssoc()
{
    // You can get variable number of arguments|array by this.
    $args = func_get_args();

    $master = array();

    foreach ($args as $arg)
    {
        foreach ($arg as $i => $v)
        {
            $master[$i][] = $v;
        }
    }

    return $master;
}

$res = mergeAssoc($a, $b, $c);

print_r($res);

Note: It will return a multidimensional array.注意:它将返回一个多维数组。 Not an array of comma-separated values.不是逗号分隔值的数组。

Output:输出:

Array
(
    [0] => Array
        (
            [0] => Algebra
            [1] => 08/01/2020
            [2] => 08/02/2020
        )

    [1] => Array
        (
            [0] => Arithmetic
            [1] => 08/01/2019
            [2] => 08/02/2019
        )

)

and if we use foreach then our desire output will be there with array separated by comma.如果我们使用foreach那么我们想要的输出将在那里用逗号分隔的数组。

foreach ($res as $key => $value) {
 $result[] = implode(',', $value);
}

and output of print_r($result);print_r($result);输出print_r($result); is

Array
 (
  [0] => Algebra,08/01/2020,08/02/2020
  [1] => Arithmetic,08/01/2019,08/02/2019
)

Firstly, when you find yourself using sequentially-named variables it almost always means that they should actually be an array:首先,当您发现自己使用顺序命名的变量时,几乎总是意味着它们实际上应该是一个数组:

$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/02/2019");
$c = array("08/01/2020", "08/02/2019");

$better_array = [$a, $b, $c];
var_dump($better_array);

Output:输出:

array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(7) "Algebra"
    [1]=>
    string(10) "Arithmetic"
  }
  [1]=>
  array(2) {
    [0]=>
    string(10) "08/01/2020"
    [1]=>
    string(10) "08/02/2019"
  }
  [2]=>
  array(2) {
    [0]=>
    string(10) "08/01/2020"
    [1]=>
    string(10) "08/02/2019"
  }
}

Once they're in an proper array you can use array_column()一旦它们位于正确的数组中,您就可以使用array_column()

$out = [];
for($i=0, $c=count($better_array[0]); $i < $c; ++$i) {
    $out[] = array_column($better_array, $i);
}
var_dump($out);

Output:输出:

array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(7) "Algebra"
    [1]=>
    string(10) "08/01/2020"
    [2]=>
    string(10) "08/01/2020"
  }
  [1]=>
  array(3) {
    [0]=>
    string(10) "Arithmetic"
    [1]=>
    string(10) "08/02/2019"
    [2]=>
    string(10) "08/02/2019"
  }
}

And if that comma-delimited string is what you actually want, then use implode() :如果那个逗号分隔的字符串是你真正想要的,那么使用implode()

$out = [];
for($i=0, $c=count($better_array[0]); $i < $c; ++$i) {
    $out[] = implode(',', array_column($better_array, $i));
}
var_dump($out);

Output:输出:

array(2) {
  [0]=>
  string(29) "Algebra,08/01/2020,08/01/2020"
  [1]=>
  string(32) "Arithmetic,08/02/2019,08/02/2019"
}

Lastly, you should avoid print_r() as it tends to produce misleading output.最后,您应该避免使用print_r()因为它往往会产生误导性的输出。 Eg: https://3v4l.org/ThSLb例如: https : //3v4l.org/ThSLb

A simple foreach loop will suffice一个简单的 foreach 循环就足够了

$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/02/2019");
$c = array("08/01/2020", "08/02/2019");

foreach( $a as $i=>$v ) {
    $new[] = sprintf( '%s,%s,%s', $v, $b[$i], $c[$i] );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM