如何组合多维数组值和键?

时间:2015-08-07 05:25:23

标签: php arrays multidimensional-array

我有以下数组

array (
  1 => 
  array (
    't' => 
    array (
      0 => 't1',
      1 => 't2',
      2 => 't3',
    ),
    'c' => 
    array (
      0 => 'c1',
      1 => 'c2',
      2 => 'c3',
    ),
    'd' => 
    array (
      0 => 'd1',
      1 => 'd2',
      2 => 'd3',
    ),
  ),
  2 => 
  array (
    'j' => 
    array (
      0 => 'j1',
      1 => 'j2',
      2 => 'j3',
    ),
    'm' => 
    array (
      0 => 'm1',
      1 => 'm2',
      2 => 'm3',
    ),
    'n' => 
    array (
      0 => 'n1',
      1 => 'n2',
      2 => 'n3',
    ),
  ),
)

我需要输出

array(
   1=>array(
      't-j'=>array('t1-j1','t2-j2','t3-j3'),
      'c-m'=>array('c1-m1','c2-m2','c3-m3'),
      'd-n'=>array('d1-n1','d2-n2','d3-n3')
      )
);

而我所做的就是

$i = 0;
$res = [];
foreach($arr[1] as $key => $value){
    foreach($arr[2] as $k => $v){
        $res[$key.'-'.$k][] = $value[$i].'-'.$v[$i];
    }
        $i++;
}

但这给了我这样的东西?

Array
(
    [t-j] => Array
        (
            [0] => t1-j1
        )

    [t-m] => Array
        (
            [0] => t1-m1
        )

    [t-n] => Array
        (
            [0] => t1-n1
        )

    [c-j] => Array
        (
            [0] => c2-j2
        )

    [c-m] => Array
        (
            [0] => c2-m2
        )

    [c-n] => Array
        (
            [0] => c2-n2
        )

    [d-j] => Array
        (
            [0] => d3-j3
        )

    [d-m] => Array
        (
            [0] => d3-m3
        )

    [d-n] => Array
        (
            [0] => d3-n3
        )

)

所以我在这里搜索了许多链接,并尝试了至少大部分可以由我做的事情。但我无法充分利用它

6 个答案:

答案 0 :(得分:5)

这对我有用:

$array1 = $arr[1];
$array2 = $arr[2];

$result = combineArray($array1, $array2);

var_dump($result);

function combineArray($array1, $array2)
{
    $res = [];

    foreach ($array1 as $key => $value)
    {
        $otherArray = array_splice($array2, 0, 1 );
        $otherKey   = array_keys($otherArray)[0];

        $smallerResult = array();
        foreach ($value as $smallerKey => $smallerValue)
        {
            $smallerResult[] = $smallerValue . '-' . $otherArray[$otherKey][$smallerKey];
        }
        $res[$key . '-' . $otherKey] = $smallerResult;
    }

    return $res;
}

我得到的输出如下:

array(3) {
  't-j' =>
  array(3) {
    [0] =>
    string(5) "t1-j1"
    [1] =>
    string(5) "t2-j2"
    [2] =>
    string(5) "t3-j3"
  }
  'c-m' =>
  array(3) {
    [0] =>
    string(5) "c1-m1"
    [1] =>
    string(5) "c2-m2"
    [2] =>
    string(5) "c3-m3"
  }
  'd-n' =>
  array(3) {
    [0] =>
    string(5) "d1-n1"
    [1] =>
    string(5) "d2-n2"
    [2] =>
    string(5) "d3-n3"
  }
}

答案 1 :(得分:4)

您所要做的就是遍历其中一个子数组并从两个数组中获取相应的键,然后使用带有回调的array_map来映射包含叶子的数组。

<?php

// define a callback for use with array_map
$callback = function($v1, $v2){ return $v1."-".$v2; };

// Reset the array pointers
reset($arr[0]);
reset($arr[1]);
$res = [];

// while I'm not at the end of the first array
while (current($arr[0]) !== false){

    // combine the keys from each of the subarrays
    $key = key($arr[0])."-".key($arr[1]);

    //use array_map to mash up the child arrays from each sub array
    $value = array_map($callback, current($arr[0]), current($arr[1]));
    $res[$key] = $value;

    // move the pointers to the next element
    next($arr[0]);
    next($arr[1]);
}

print_r($res);

要查看一个简单的测试用例,请查看here

答案 2 :(得分:2)

这是使用for-statement的另一种方法..

function array_config($arr1, $arr2)
{
    $temp_arr = array();

    $arr_k1 = array_keys($arr1);

    $arr_k2 = array_keys($arr2);

    for ($i = 0; $i < count($arr1); $i++)
    {
        $k1 = $arr_k1[$i];

        $k2 = $arr_k2[$i];

        $key = $k1."-".$k2;

        for ($j = 0; $j < count($arr1[$k1]);$j++)
        {
            $temp_arr[$key][] = $arr1[$k1][$j]."-".$arr2[$k2][$j];
        }
    }
    return $temp_arr;
}

var_dump(array_config($arr[1], $arr[2]));

输出:

enter image description here

干杯! :)

答案 3 :(得分:2)

可能不是您正在寻找的答案,但它可能有所帮助。我简化了两个数组。

小提琴:http://ideone.com/M5RQbv

<?php

$arr1 = array (
    't' => array ('t1','t2','t3'),
    'c' => array ('c1','c2','c3'),
    'd' => array ('d1','d2','d3'),
);

$arr2 = array (
    'j' => array ('j1','j2','j3'),
    'm' => array ('m1','m2','m3'),
    'n' => array ('n1','n2','n3'),
);

$res = [];
foreach($arr1 as $key => $value){
    foreach($arr2 as $k => $v){
        for($i = 0 ; $i<count($v); $i++){
            $res[$key.'-'.$k][] = $value[$i].'-'.$v[$i];
        }
    }
}

print_r($res);

输出:

Array
(
    [t-j] => Array
        (
            [0] => t1-j1
            [1] => t2-j2
            [2] => t3-j3
        )

    [t-m] => Array
        (
            [0] => t1-m1
            [1] => t2-m2
            [2] => t3-m3
        )

    [t-n] => Array
        (
            [0] => t1-n1
            [1] => t2-n2
            [2] => t3-n3
        )

    [c-j] => Array
        (
            [0] => c1-j1
            [1] => c2-j2
            [2] => c3-j3
        )

    [c-m] => Array
        (
            [0] => c1-m1
            [1] => c2-m2
            [2] => c3-m3
        )

    [c-n] => Array
        (
            [0] => c1-n1
            [1] => c2-n2
            [2] => c3-n3
        )

    [d-j] => Array
        (
            [0] => d1-j1
            [1] => d2-j2
            [2] => d3-j3
        )

    [d-m] => Array
        (
            [0] => d1-m1
            [1] => d2-m2
            [2] => d3-m3
        )

    [d-n] => Array
        (
            [0] => d1-n1
            [1] => d2-n2
            [2] => d3-n3
        )

)

答案 4 :(得分:1)

很晚才为许多元素工作而不管2

<?php
//merge keys
$aKeysMerged = array();
$aValuesMerged = array(); //merge values
foreach ($aArray as $iKey=>$aValue){    

    $sKeys = array_keys($aValue);
    $aValues = array_values($aValue);

    if(empty($aKeysMerged)){
        $aKeysMerged = $sKeys; //initialize 
        $aValuesMerged = $aValues; //initialize
    }
    else{

        //merge keys
        foreach($aKeysMerged as $iKey1=>$sKey){
            $aKeysMerged[$iKey1] = $sKey.'-';
            if(isset($sKeys[$iKey1]))
                $aKeysMerged[$iKey1] .= $sKeys[$iKey1];
        }   

        //merge values
        for($i=0; $i<count($aValuesMerged); $i++){
            $a11  = $aValuesMerged[$i];
            $a12 = $aValues[$i];
            $aMerge = array();
            foreach ($a11 as $ikey2=>$sVal){
                $aMerge[$ikey2] = $sVal.'-'.$a12[$ikey2];
            }           
            $aValuesMerged[$i] = $aMerge;
        }       
    }
}

//combine the keys and values here
$aResult = array_combine($aKeysMerged, $aValuesMerged);

print_r($aResult);

答案 5 :(得分:0)

这会对你有帮助,

function request_access($this){
    console.log("button clicked");
    var request_data = $this.id;
    console.log("data: " + request_data)
    $.post( "request_access",{ request_data: request_data},function(json) {
         $("#request-access").hide();
         console.log("requested access complete");
    })
}