我根据不同数据库上的 SQL 查询结果构建了两个数组。
其中一个的格式如下:
$data = array(
[$sku] => array(
['LocalSKU'] => $sku,
['Price'] => $msrp,
['Price2'] => $wholesale,
['Price3'] => $distributor,
['Price4'] => $map
)
)
另一个数组的格式如下:
$matchme = array(
[0] => array(
['entity_id'] => $entity_id,
['sku'] => $sku,
['type_id'] => $type_id
)
)
目前,我可以通过以下方式获取个人数据进行匹配:
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'];
但是,当我尝试合并两个数组中的匹配行时,我得到一个空数组。 $data 数组包含 74 个唯一的 $sku,$matchme 是根据数据库检查这些 $sku 并返回包含 61 个元素的数组的结果。因此,组合数组应具有 61 个元素,并具有基于 $sku 的匹配定价数据。
我如何尝试构建组合数组如下,任何人都可以指出我做错了什么吗?
foreach ($matchme as $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']
);
}
}
在上面的例子中,$key的值为0,['sku']的值是匹配的。
------------------------已编辑------------------------ -
根据请求,这是 print_r($data) 因空格而被截断的结果:
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
) )
根据请求,这是 print_r($matchme) 因空格而被截断的结果:
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
) )
根据请求,这是 $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
)
)
每个匹配的 SKU 都有一个附加数组。
试试这个:
foreach ($matchme as $arrProduct) {
if (isset($data[$arrProduct['sku']])) {
$arrMerged[$arrProduct['sku']]=array_merge($arrProduct, $data[$arrProduct['sku']]);
}
}
print_r($arrMerged);
您的代码不起作用的原因如下:
if(in_array($matchme[$key]['sku'], $data)) [...]
in_array()
的作用是告诉您needle(在您的情况下为SKU字符串)是否作为数组haystack(在您的情况下为$data)的值存在。 您本质上是在尝试将字符串与数组匹配,而不是另一个字符串。
您真正想要的只是将 SKU 字符串与 $data 的键相匹配,对此
isset()
可能是最简单的方法。
假设您的第一个数组为
$first
,第二个数组为 $second
foreach ($first as $key => $each) {
foreach ($second as $secondeach) {
if($secondeach['sku'] == $key) {
$first[$key] = array_merge($first[$key], $secondeach);
// unset since you do not want LocalSKU value anymore.
unset($first[$key]['LocalSKU']);
}
}
}
$first
是你想要的数组。