mysql - Combine two arrays on key/value and output combined array with PHP -
racking brain on this, found many examples of similar situations solutions don't seem match up.
i have 2 arrays being built result of sql queries on different databases.
one coming through formatted such:
$data = array([$sku] => array(['localsku'] => $sku, ['price'] => $msrp, ['price2'] => $wholesale, ['price3'] => $distributor, ['price4'] => $map)) the other array formatted such:
$matchme = array([0] => array(['entity_id'] => $entity_id, ['sku'] => $sku, ['type_id'] => $type_id)) currently, can individual data match via:
echo $matchme[0]['sku']; echo $matchme[0]['entity_id']; echo $matchme[0]['type_id']; echo $data[$matchme[0]['sku']]['price']; echo $data[$matchme[0]['sku']]['price2']; echo $data[$matchme[0]['sku']]['price3']; echo $data[$matchme[0]['sku']]['price4']; however, when try , merge matching rows in both arrays, empty array. $data array contains 74 unique $sku, , $matchme result of checking $sku's against database , returning array 61 elements. so, combined array should have 61 elements matched pricing data based on $sku.
how attempting build combined array below, can point me towards doing wrong?
foreach($matchme $key){ if(in_array($matchme[$key]['sku'], $data)){ $matched_luggage[$matchme[$key]['sku']][] = array( 'sku' => $matchme[$key]['sku'], 'entity_id' => $matchme[$key]['entity_id'], 'type_id' => $matchme[$key]['type_id'], 'msrp' => $data[$matchme[$key]['sku']]['price'], 'wholesale' => $data[$matchme[$key]['sku']]['price2'], 'distributor' => $data[$matchme[$key]['sku']]['price3'], 'map' => $data[$matchme[$key]['sku']]['price4'] ); } } in above example, evaluate $key 0, , value of ['sku'] matching.
------------------------edited-------------------------
per request, here result of print_r($data) truncated space:
array ( [12pk-titanium-cr123a] => array ( [localsku] => 12pk-titanium-cr123a [price] => 11.76 [price2] => 10.32 [price3] => 0 [price4] => 0 ) [aa-clamshell] => array ( [localsku] => aa-clamshell [price] => 0.25 [price2] => 0 [price3] => 0 [price4] => 0 ) [aaa-clamshell] => array ( [localsku] => aaa-clamshell [price] => 0.25 [price2] => 0 [price3] => 0 [price4] => 0 ) [ae-ael280pi] => array ( [localsku] => ae-ael280pi [price] => 0 [price2] => 0 [price3] => 0 [price4] => 0 ) ) per request, here result of print_r($matchme) truncated space:
array ( [0] => array ( [entity_id] => 693 [sku] => 12pk-titanium-cr123a [type_id] => simple ) [1] => array ( [entity_id] => 2596 [sku] => ae-ael480hl [type_id] => simple ) [2] => array ( [entity_id] => 2597 [sku] => ae-ael600-t6 [type_id] => simple ) [3] => array ( [entity_id] => 2598 [sku] => ae-aewl2 [type_id] => simple ) ) per request, here desired result of $matched_luggage:
$matched_luggage = array( [12pk-titanium-cr123a] => array([sku] => 12pk-titanium-cr123a, [entity_id] => 693, [type_id] => simple, [price] => 11.76, [price2] => 10.32, [price3] => 0, [price4] => 0)) with additional array per matched sku.
try this:
foreach ($matchme $arrproduct) { if (isset($data[$arrproduct['sku']])) { $arrmerged[$arrproduct['sku']]=array_merge($arrproduct, $data[$arrproduct['sku']]); } } print_r($arrmerged); the reason code doesn't work here:
if(in_array($matchme[$key]['sku'], $data)) [...] what in_array() tell whether needle (in case sku string) exists value of array haystack (in case, $data). trying match string array, rather string.
what want match sku string key of $data, isset() simplest approach.
Comments
Post a Comment