2

我有 2 个数组,我想要 2 个创建 2D 数组来创建 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
  )
4

7 回答 7

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但您了解基本思想。在这里检查。

于 2012-11-26T13:09:17.900 回答
2

假设$array1judge_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

简单得要命

$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
    );
}
于 2012-11-26T13:09:22.537 回答
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 回答
1

如果你有 PHP >= 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);

演示

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

之后,您使用 . 将结果转换为数组iterator_to_array()

于 2012-11-26T13:11:57.307 回答
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"
  }
}
于 2012-11-26T13:13:12.577 回答
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 回答