my code is:
I have set of data in two different variables like an below.
Array 1:
Array ( [0] => 15-3 [1] => 16-3 [2] => 15-4 [3] => 16-4 [4] => 15-3 [5] => 16-3 [6] => 15-4 [7] => 16-4 [8] => 15-3 [9] => 16-3 [10] => 15-4 [11] => 16-4 [12] => 15-3 [13] => 16-3 [14] => 15-4 [15] => 16-4 )
Array 2:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 11 [5] => 22 [6] => 33 [7] => 44 [8] => 111 [9] => 222 [10] => 333 [11] => 444 [12] => 1111 [13] => 2222 [14] => 3333 [15] => 4444 )
In that how can i combine the above two array into an single one array like an below
$final_array=Array ( [15-3] => 1 [16-3] => 2 [15-4] => 3 [16-4] => 4 [15-3] => 11 [16-3] => 22 [15-4] => 33 [16-4] => 44 [15-3] => 111 [16-3] => 222 [15-4] => 333 [16-4] => 444 [15-3] => 1111 [16-3] => 2222 [15-4] => 3333 [16-4] => 4444)
Do a foreach():
$final = array();
foreach($attri_ids_all as $key=>$value){
$final[$value] = $inventory_code_qty[$key];
}
You can use array_combine
, you can read about this here: http://php.net/manual/en/function.array-combine.php
Basically you need use:
$final_array = array_combine($attri_ids_all, $inventory_code_qty);
It's not complicated at all, juste loop on 1 array...
if (count($attri_ids_all) != count($inventory_code_qty)) {
print 'arrays lenght must be equals';
return;
}
$final_array = array();
for($i=0 ; $i<count($attri_ids_all) ; $i++) {
$final_array[$attri_ids_all[$i]] = $inventory_code_qty[$i];
}
Hello try this code you need to foreach loop
$final_array = array();
foreach($attri_ids_all as $key=>$value){
$final_array[$value] = $inventory_code_qty[$key];
}
and print $final_array array
using multidimensional array
$result_array = array();
foreach($attri_ids_all as $key=>$value){
$result_array[$key][$value] = $inventory_code_qty[$key];
}
function foo($key, $val) {
return array($key=>$val);
}
$arrResult = array_map('foo', $attri_ids_all, $inventory_code_qty);
echo "<pre>";
print_r($arrResult);
echo "</pre>";