search

Home  >  Q&A  >  body text

php array problem

array(2) {
  [0]=>
  int(2)
  [1]=>
  int(1)
}

How to assemble the above array into the following array corresponding to the value [0]=>
int(2) [1]=>
int(1) as a column of g_num

Assemble arrays in sequence

array(2) {
  [0]=>
  array(14) {
    ["id"]=>
    string(1) "7"
    ["m_id"]=>
    string(1) "1"
    ["g_name"]=>
    string(6) "苹果"
   
  }
  [1]=>
  array(14) {
    ["id"]=>
    string(1) "8"
    ["m_id"]=>
    string(1) "1"
    ["g_name"]=>
    string(6) "梨子"
    
  }
}

After the transformation, it looks like this. Thank you, God

array(2) {
  [0]=>
  array(14) {
    ["id"]=>
    string(1) "7"
    ["m_id"]=>
    string(1) "1"
    ["g_name"]=>
    string(6) "苹果"
    ["g_num"]=>
    string(1) "2"
  }
  [1]=>
  array(14) {
    ["id"]=>
    string(1) "8"
    ["m_id"]=>
    string(1) "1"
    ["g_name"]=>
    string(6) "梨子"
    ["g_num"]=>
    string(1) "1"
  }
}
PHP中文网PHP中文网2772 days ago784

reply all(2)I'll reply

  • 三叔

    三叔2017-06-10 09:49:40

    $array1 = array(2, 1);
    $array2 = array(
        array(
            'id' => '7',
            'm_id' => '1',
            'g_name' => '苹果'
        ),
        array(
            'id' => '8',
            'm_id' => '1',
            'g_name' => '梨子'
        ),
    );
    foreach ($array2 as $key => &$val) {
        $val['g_num'] = $array1[$key];
    }
    var_dump($array2);

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-10 09:49:40

    Thanks for the invitation!

        $arr = array (
                array (
                      'id' => '7',
                      'm_id' => '1',
                      'g_name' => '苹果'
                      ),
                array (
                      'id' => '8',
                      'm_id' => '1',
                      'g_name' => '梨子'
                      ),
            );
            
        $add = array(2, 1);
        for($i = 0;$i < count($arr); $i++) {
            $arr[$i]['g_num'] = $add[$i];
        }
        
        var_dump("<pre>", $arr);

    reply
    0
  • Cancelreply