php array problem
PHP中文网
PHP中文网 2017-06-10 09:47:40
0
2
733
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中文网

认证0级讲师

reply all(2)
三叔
$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);
仅有的幸福

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);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!