PHP multiple arrays to one associative array

你说的曾经没有我的故事 提交于 2019-12-13 19:48:35

问题


How can I merge this three arrays

$name ={"Tom", "John", "David"};
$v1 = {"Tom":100, "David":200};
$v2 = {"John":500, "Tom":400};

into one multidimensional associative array in two different ways? One way is the key order should be same as that of array "name".

$name_merged_original_order = array (
    "Tom" => Array(
        "v1" => 100,
        "v2" => 400
    ),
    "John" => Array(
        "v1" => "N/A",
        "v2" => 500
    ),
    "David" => Array(
        "v1" => 100,
        "v2" => "N/A"
    )
)

Another ways is the alphabetical of array "name":

$name_merged_asc = array (
    "David" => Array(
        "v1" => 100,
        "v2" => "N/A"
    ),
    "John" => Array(
        "v1" => "N/A",
        "v2" => 200
    ),
    "Tom" => Array(
        "v1" => 100,
        "v2" => 400
    ),
)

The tricky part is that array "v1" and "v2" is not ordered as the key of "name." They also don't have all keys as in "name." Thanks!


回答1:


It's not tested and the easiest solution:

$name_merged_original_order = array();
foreach($name as $key){
    $name_merged_original_order[$key] = array();
    if(array_key_exists($key, $v1)){
        $name_merged_original_order[$key]['v1'] = $v1[$key];
    }
    else{
        $name_merged_original_order[$key]['v1'] = 'N/A';
    }
    if(array_key_exists($key, $v2)){
        $name_merged_original_order[$key]['v2'] = $v2[$key];
    }
    else{
        $name_merged_original_order[$key]['v2'] = 'N/A';
    }
}

sort($name);
$name_merged_asc = array();
foreach($name as $key){
    $name_merged_asc[$key] = array();
    if(array_key_exists($key, $v1)){
        $name_merged_asc[$key]['v1'] = $v1[$key];
    }
    else{
        $name_merged_asc[$key]['v1'] = 'N/A';
    }
    if(array_key_exists($key, $v2)){
        $name_merged_asc[$key]['v2'] = $v2[$key];
    }
    else{
        $name_merged_asc[$key]['v2'] = 'N/A';
    }
}



回答2:


As I understand you would like something like that:

$name = array("Tom", "John", "David");
$result = array();
$v1 = array("Tom" => "200", "John" => "100", "David" => "10");
$v2 = array("Tom" => "254", "David" => "156");
$vars = array("v1", "v2");
foreach($name as $n)
{
  $result[$n] = array();
  foreach($vars as $v)
  {
    if(array_key_exists($n, ${$v}))
      $result[$n][$v] = ${$v}[$n];
  }
}

I hope $result is what you need.




回答3:


For Example, you have these arrays:

<?php
$FirstArrays = array('a', 'b', 'c', 'd');
$SecArrays = array('1', '2', '3', '4');

1)

foreach($FirstArrays as $index => $value) {
    echo $FirstArrays[$index].$SecArrays[$index];
    echo "<br/>";
}

or 2)

for ($index = 0 ; $index < count($FirstArrays); $index ++) {
  echo $FirstArrays[$index] . $SecArrays[$index];
  echo "<br/>";
}



回答4:


Assume from your comments you only want items that match in all 3 arrays:

for( $i=0; $i< count($name) ; $i++){
    if( !empty( $v1[ $name[$i]]) &&  !empty( $v2[ $name[$i]]) ){
       $newArray[$name[$i]]= array( 'v1'=> $v1[ $name[$i]], 'v2'=> $v2[ $name[$i]]):
    }
}

To sort:

  asort($newArray);


来源:https://stackoverflow.com/questions/14179374/php-multiple-arrays-to-one-associative-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!