Combining array inside multidimensional array with

2019-01-03 11:06发布

I have the following array:

$where = array(
    'id'=>array(
        12,
        13,
        14
    ),
    'date'=>array(
        '1999-06-12',
        '2000-03-21',
        '2006-09-31'
    )
);

Expected output:

$comb = array(
    array(12, '1999-06-12'),
    array(13, '2000-03-21'),
    array(14, '2006-09-31')
);

Any idea why I am not getting this expected output?

3条回答
我只想做你的唯一
2楼-- · 2019-01-03 11:34

As Kris Roofe stated in his deleted answer, array_column is indeed a more elegant way. Just be sure to put it into some kind of a foreach loop, similar to what Sahil Gulati showed you. For example, like this:

$result = array();

foreach($where['id'] as $k => $v)
{
  $result[] = array_column($where, $k);
}

The var_dump output of $result is exactly what you're looking for

array(3) {
  [0]=>
  array(2) {
    [0]=>
    int(12)
    [1]=>
    string(10) "1999-06-12"
  }
  [1]=>
  array(2) {
    [0]=>
    int(13)
    [1]=>
    string(10) "2000-03-21"
  }
  [2]=>
  array(2) {
    [0]=>
    int(14)
    [1]=>
    string(10) "2006-09-31"
  }
}
查看更多
该账号已被封号
3楼-- · 2019-01-03 11:42

Wanna see a fancy trick?

(php minimum version: 5.6)

If you strip the array keys (id and date) from $where you can use a variadic function and write a nice tight little one-liner! And you don't have to bother instantiating any result arrays -- no fuss. PHP is so excellent -- big fan.

Input:

$where=['id'=>[12,13,14],'date'=>['1999-06-12','2000-03-21','2006-09-31']];

Method #1: variadic array_map() with func_get_args()

$comb=array_map(function(){return func_get_args();},...array_values($where));
var_export($comb);

This method is robust as it will handle a variable number of "rows" and "columns". Here is a demo with a few examples.


Or if you are are sub-5.6, you can use this, but it is less flexible/robust (more literal to the OP's sample data):

Method #2: array_map() with two inputs

$comb=array_map(function($v1,$v2){return [$v1,$v2];},$where['id'],$where['date']);
var_export($comb);

Output from either method:

array (
  0 => 
  array (
    0 => 12,
    1 => '1999-06-12',
  ),
  1 => 
  array (
    0 => 13,
    1 => '2000-03-21',
  ),
  2 => 
  array (
    0 => 14,
    1 => '2006-09-31',
  ),
)

I find array_map() to be my favorite function for this case because it creates the result array in the same line (as opposed to returning a true/false result like array_walk(); or using a foreach loop and printing after it is done). This means you can do a true one-liner print out without declaring a result variable...

var_export(array_map(function(){return func_get_args();},...array_values($where)));

or

var_export(array_map(function($v1,$v2){return [$v1,$v2];},$where['id'],$where['date']));
查看更多
干净又极端
4楼-- · 2019-01-03 11:48

Solution 1: Hope this simple foreach to get the desired result

Try this code snippet here

<?php
ini_set('display_errors', 1);
$where = array('id'=>array(12,13,14),'date'=>array('1999-06-12','2000-03-21','2006-09-31'));

$result=array();
foreach($where["id"] as $key => $value)
{
    $result[]=array($value,$where["date"][$key]);
}

Solution 2: Here we are using array_walk to achieve the same result

Try this code snippet here

<?php
ini_set('display_errors', 1);
$result=array();
$where = array('id'=>array(12,13,14),'date'=>array('1999-06-12','2000-03-21','2006-09-31'));

array_walk($where["id"], function($value,$key) use(&$result,&$where){
    $result[]=array($value,$where["date"][$key]);
});
print_r($result);

Solution 3: Here we are using array_shift on $where["date"].

Try this code snippet here

<?php
ini_set('display_errors', 1);
$result=array();
$where = array('id'=>array(12,13,14),'date'=>array('1999-06-12','2000-03-21','2006-09-31'));

foreach($where["id"] as $value)
{   
    $result[]=array($value,  array_shift($where["date"]));
}
print_r($result);
查看更多
登录 后发表回答