How to create an associative array from two arrays?

时间:2019-03-06 11:41:42

标签: php arrays

I have two arrays one for keys and another for values. The values array is an array of array.

Keys array:

$keyArray = array("clientId","clientName","clientAdsress","clientPhone");

Values array:

$valuesArray = array(
                "0"=>array("1001","aaaaa","ddddddd","22222222"),
                "1"=>array("1002","bbbbbb","ddddddd","11111111"),
                "2"=>array("1003","ccccc","ddddddd","33333333")
              );

Expected Output:

$finalData = array(
                    "clientId"=>array("1001","1002","1003"),
                    "clientName"=>array("aaaaa","bbbbbb","ccccc"),
                    "clientAdsress"=>array("ddddddd","ddddddd","ddddddd"),
                    "clientPhone"=>array("22222222","11111111","33333333")
                  );

I have tried this code:

$finalData = array();
for($i=0;$i<count($keyArray);$i++){

    for($j=0;$j<count($valuesArray);$j++){
        $rowArray = $valuesArray[$j];
       $finalData[$keyArray[$i]] = array($rowArray[$i]);
    }
}

echo '<pre>';print_r($finalData);echo '</pre>';

Output:

Array
(
    [clientId] => Array
        (
            [0] => 1003
        )

    [clientName] => Array
        (
            [0] => ccccc
        )

    [clientAdsress] => Array
        (
            [0] => ddddddd
        )

    [clientPhone] => Array
        (
            [0] => 33333333
        )

)

I am getting the last values of arrays. How can I get my expected output?

4 个答案:

答案 0 :(得分:9)

一个简单的foreach解决方案:

https://3v4l.org/gbBad

<?php

$keyArray = array("clientId", "clientName", "clientAdsress", "clientPhone");
$valuesArray = array(
    "0" => array("1001", "aaaaa", "ddddddd", "22222222"),
    "1" => array("1002", "bbbbbb", "ddddddd", "11111111"),
    "2" => array("1003", "ccccc", "ddddddd", "33333333")
);
$expected = array(
    "clientId" => array("1001", "1002", "1003"),
    "clientName" => array("aaaaa", "bbbbbb", "ccccc"),
    "clientAdsress" => array("ddddddd", "ddddddd", "ddddddd"),
    "clientPhone" => array("22222222", "11111111", "33333333")
);

$result = [];
foreach ($keyArray as $key => $keyName) {
    foreach ($valuesArray as $value) {
        $result[$keyName][] = $value[$key];
    }
}

var_dump($result === $expected);

7.1.25-7.3.2的输出

  

bool(true)

答案 1 :(得分:7)

一种方法是使用array_combinearray_column

$finalData = array_combine($keyArray, array(array_column($valuesArray, 0),
                                            array_column($valuesArray, 1),
                                            array_column($valuesArray, 2),
                                            array_column($valuesArray, 3)));
print_r($finalData);

输出:

Array ( 
    [clientId] => Array ( [0] => 1001 [1] => 1002 [2] => 1003 )
    [clientName] => Array ( [0] => aaaaa [1] => bbbbbb [2] => ccccc )
    [clientAdsress] => Array ( [0] => ddddddd [1] => ddddddd [2] => ddddddd )
    [clientPhone] => Array ( [0] => 22222222 [1] => 11111111 [2] => 33333333 ) 
)

Demo on 3v4l.org

答案 2 :(得分:5)

Nick's answer的基础上,不需要硬编码键数的简单解决方案是:

$finalData = array_combine($keyArray, array_map(null, ...$valuesArray));

Demo on 3v4l.org

基本上,这首先按照array_map所述,使用in this answer转换二维$valuesArray,将列更改为行,反之亦然,然后将结果与{{1} },就像尼克的答案一样使用array_combine

答案 3 :(得分:2)

<?php
$keyArray = array(
    "clientId","clientName","clientAdsress","clientPhone"
);

$valuesArray = array(
    "0"=>array("1001","aaaaa","ddddddd","22222222"),
    "1"=>array("1002","bbbbbb","ddddddd","11111111"),
    "2"=>array("1003","ccccc","ddddddd","33333333")
);
foreach($keyArray as $keyIndex => $keyName)
    $finalData[$keyName] = array_column($valuesArray, $keyIndex);

print_r($finalData);

输出:

Array
(
    [clientId] => Array
        (
            [0] => 1001
            [1] => 1002
            [2] => 1003
        )

    [clientName] => Array
        (
            [0] => aaaaa
            [1] => bbbbbb
            [2] => ccccc
        )

    [clientAdsress] => Array
        (
            [0] => ddddddd
            [1] => ddddddd
            [2] => ddddddd
        )

    [clientPhone] => Array
        (
            [0] => 22222222
            [1] => 11111111
            [2] => 33333333
        )

)