search

Home  >  Q&A  >  body text

php - combination of two arrays

$id=
Array
(
    [0] => 1
    [1] => 2
    [2] => 16

)
$oid=
Array
(
    [0] => 1
    [1] => 1
    [2] => 2

)

I want to combine it into an array as follows:
$data =
Array
(

[0] => 
    [id]=1
    [oid]=1
[1] => 
    [id]=2
    [oid]=1
[2] => 
    [id]=16
    [oid]=2

)

Please help me

扔个三星炸死你扔个三星炸死你2703 days ago927

reply all(5)I'll reply

  • 漂亮男人

    漂亮男人2017-06-29 10:10:34

    $data = array();
    for ($i = 0; $i < count($id); $i++) {
        $data[$i] = array(
            'id' => $id[$i],
            'oid' => $oid[$i]
        );
    }
    var_dump($data);

    reply
    0
  • 欧阳克

    欧阳克2017-06-29 10:10:34

    $data=[];
    foreach($id as $key=>$va){
      $data[]=[
       'id'=>$va,
       'oid'=>$oid[$key]
     ];
    }

    reply
    0
  • 为情所困

    为情所困2017-06-29 10:10:34

    Read more damn official documents, you don’t need to ask any master to help with these problems... (Hey, write it down casually, the $result below should be what you want...)

    $result = array_map(function($item1, $item2) {
        return ['id' => $item1, 'oid' => $item2];
    }, $id, $oid);

    reply
    0
  • 迷茫

    迷茫2017-06-29 10:10:34

    <?php
    $id=[1,2,16];
    $oid=[1,1,2];
    array_work($id, function(&$v,$k)use ($oid){

    $v=['id'=>$v,'oid'=>$oid[$k]];
    

    });
    print_r($id);

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-29 10:10:34

    $data = array();
    foreach($id as $key =>$val){
     $data[] = array(
            'id' => $val,
            'old' => $
        ); 
    }

    reply
    0
  • Cancelreply