代码之家  ›  专栏  ›  技术社区  ›  Gufran Hasan

如何从两个数组创建关联数组?

  •  3
  • Gufran Hasan  · 技术社区  · 5 年前

    我有两个数组,一个用于键,另一个用于值。值数组是数组的数组。

    $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")
                  );
    

    预期产量:

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

    $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>';
    

    输出:

    Array
    (
        [clientId] => Array
            (
                [0] => 1003
            )
    
        [clientName] => Array
            (
                [0] => ccccc
            )
    
        [clientAdsress] => Array
            (
                [0] => ddddddd
            )
    
        [clientPhone] => Array
            (
                [0] => 33333333
            )
    
    )
    

    我正在获取数组的最后一个值。如何获得预期的输出?

    2 回复  |  直到 5 年前
        1
  •  7
  •   Nick    5 年前

    一种方法是使用 array_combine array_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
  •  10
  •   Xatenev    5 年前

    一个简单的 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);
    

        3
  •  5
  •   Ilmari Karonen hansTheFranz    5 年前

    建立在 Nick's answer ,一个不需要硬编码键数的简单解决方案是:

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

    ( Demo on 3v4l.org )

    $valuesArray 使用 array_map 如前所述 in this answer $keyArray 使用 array_combine 就像尼克的回答一样。

        4
  •  2
  •   Progrock    5 年前
    <?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
            )
    
    )