PHP - Merge two arrays (same-length) into one associative?

前端 未结 4 1837
小蘑菇
小蘑菇 2020-12-03 20:29

pretty straightforward question actually..

is it possible in PHP to combine two separate arrays of the same length to one associative array wh

4条回答
  •  旧时难觅i
    2020-12-03 21:26

    hello everybody i will show you how to merge 2 arrays in one array

    we have 2 arrays and i will make one array from them

     $data_key  = array('key1','key2');
     $data_value = array('val1','val2');
    

    lets declare the main array

    $main_array = array();
    

    now let's fill it with the 2 arrays

    foreach ($data_key as $i => $key) {
             $main_array[$key] = $data_value[$i];
    }
    

    now let's see the result by using var_dump($main_array);

    array(2) { 
    ["key1"]=> string(4) "val1"
    ["key2"]=> string(4) "val2" 
    }
    

    i hope that can help someone :)

提交回复
热议问题