2

私は 2 つの配列を持っており、mysql レコードを作成するために 2 つの 2D 配列を作成したい

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

このように2次元配列に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
  )
4

7 に答える 7

2

$array1とが であるjudge_nameと仮定する$array2designation_id

$newArray = array();

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

コードパッドのデモ

出力

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
        )

)
于 2012-11-26T13:08:42.117 に答える
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;
}
于 2012-11-26T13:10:41.050 に答える
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);
于 2012-11-26T13:15:16.963 に答える