使用2个单个数组创建维数组

时间:2012-11-26 13:05:23

标签: php arrays

我有2个数组,我想创建2个数组用于创建mysql记录

      Array
      (
           [0] => a
           [1] => b
           [2] => c
       )
       Array
       (
           [0] => 1
           [1] => 2
           [2] => 3
         )

我希望2将它们合并为二维数组,如此

      Array
(
[0] => Array
    (
        [designation_id] => 1
        [judge_name] => a
    )

[1] => Array
    (
        [designation_id] => 2
        [judge_name] => b
    )

[2] => Array
    (
        [designation_id] => 3
        [judge_name] => c
    )

 )

我使用array_merge_recursive并生成像这样的结果

  Array
 (
     [0] => a
     [1] => b
     [2] => c
     [3] => 1
     [4] => 2
     [5] => 3
  )

7 个答案:

答案 0 :(得分:3)

假设$array1中的值总是与$array2中的值相同。

$array1 = Array("a","b","c");
$array2 = Array(1,2,3);
$newArray = Array();

foreach($array1 as $key => $arr1Val){
    $newArray[$key]['designation_id'] = $array2[$key];
    $newArray[$key]['judge_name'] = $array1[$key];
}

当然,您必须根据自己的需要更改$array1$array2,但您了解基本想法。检查here

答案 1 :(得分:2)

假设$array1judge_name$array2designation_id

$newArray = array();

for($i=0; $i<count($array1); $i++)
{
    $newArray[] = array(
        'designation_id' => $array2[$i],
        'judge_name' => $array1[$i]
    );
}

<强> Codepad Demo

<强>输出

Array
(
    [0] => Array
        (
            [designation_id] => 1
            [judge_name] => a
        )

    [1] => Array
        (
            [designation_id] => 2
            [judge_name] => b
        )

    [2] => Array
        (
            [designation_id] => 3
            [judge_name] => c
        )

)

答案 2 :(得分:1)

简单如地狱

$array1 = array('a', 'b', 'c');
$array2 = array(1,2,3);

$merged = array();
foreach($array1 as $key => $value)
{
    $merged[$key] = array(
        'judge_name' => $value,
        'designation_id' => array_key_exists($key, $array2) ? $array2[$key] : null
    );
}

答案 3 :(得分:1)

假设两个阵列的大小相同

$length = count($array1);
$finalArray = array();

for ($i = 0; $i < $length; $i++) {
    $temp = array();
    $temp['designation_id']  = $array1[$i];
    $temp['judge_name']  = $array2[$i];
    $finalArray[$i] = $temp;
}

答案 4 :(得分:1)

如果您有PHP&gt; = 5.3,则可以使用MultipleIterator来实现此目的:

$designations = array(1, 2, 3);
$judges = array('a', 'b', 'c');

$mt = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$mt->attachIterator(new ArrayIterator($designations), "designation_id");
$mt->attachIterator(new ArrayIterator($judges), "judge_name");

$final = iterator_to_array($mt, false);
print_r($final);

Demo

迭代多个数组,从每个迭代器的每个数组中获取一个值;您可以为将用于形成单个数组项的每个数组分配一个键。

然后使用iterator_to_array()将结果转换为数组。

答案 5 :(得分:1)

$a = array('a', 'b', 'c');
$b = array(1,2,3);
$output = array_map(function($i, $j){
    return array(
        'judge_name'     => $j,
        'designation_id' => $i
    );
}, $a, $b);

var_dump($output);

输出

array(3) {
  [0]=>
  array(2) {
    ["judge_name"]=>
    int(1)
    ["designation_id"]=>
    string(1) "a"
  }
  [1]=>
  array(2) {
    ["judge_name"]=>
    int(2)
    ["designation_id"]=>
    string(1) "b"
  }
  [2]=>
  array(2) {
    ["judge_name"]=>
    int(3)
    ["designation_id"]=>
    string(1) "c"
  }
}

答案 6 :(得分:0)

$new1=array("a","b","c");
$new2=array("1","2","3");
$req=array();
$d=0;
foreach($new1 as $value1)
{
    foreach($new2 as $value2)
    {
        $req[$d]["designation_id"]=$value1;
        $req[$d]["judge_name"]=$value2;
        $d++;
    }
}

echo "<pre>";
print_r($req);