<?php // https://stackoverflow.com/questions/28728084/merge-2-arrays-by-index
/**
* Driving table
*/
$array2 = array("yahoo", "com");
/**
* Asume that corresponding index in 'Driving' table matches with this table
*
* i.e. 'yahoo' => index 0 matches this: array1[0]
*
* 'com' => index 1 matches this: array1[1]
*/
$array1 = array( array(77, 87),
array(23, 45));
/**
* required output
*/
$reqd = Array(
0 => Array(
0 => 'yahoo',
1 => 77,
2 => 87
),
1 => Array(
0 => 'com',
1 => 23,
2 => 45
),
);
/**
* now we need an 'understandable' method that gives us a useful result...
*/
$result = array();
// drive off 'array2'...
while (current($array2)) { // use the array iterator
$combined = array(); // store the current merged array in here
$combined[] = current($array2);
$idxArray2 = key($array2);
foreach($array1[$idxArray2] as $value) { // append the associated entries
$combined[] = $value;
}
// now we have the required complete entry - add it to the output.
$result[] = $combined;
next($array2);
}
/**
* show the required input and the actual output
*/
echo '<br />required:<pre>';
print_r($reqd);
echo '</pre><br />';
echo '<br />actual:<pre>';
print_r($result);
echo '</pre><br />';
if (serialize($reqd) == serialize($result)) {
echo 'all ok';
}
else {
echo 'no match<br />';
}