简体   繁体   中英

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(
 [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); 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 ($res as $key => $value) {
 $result[] = implode(',', $value);
}

and output of 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()

$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() :

$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. Eg: https://3v4l.org/ThSLb

A simple foreach loop will suffice

$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] );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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