在键/值上组合两个数组并使用PHP输出组合数组

时间:2014-04-15 19:19:37

标签: php mysql arrays

对此进行了大脑研究,发现了许多类似情况的例子,但解决方案似乎并不匹配。

我有两个数组是由不同数据库上的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附加一个数组。

2 个答案:

答案 0 :(得分:2)

试试这个:

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 的值存在(在您的情况下, $数据)。您实际上是在尝试将字符串与数组匹配,而不是另一个字符串。

您真正想要的只是将SKU字符串与$ data的键匹配,isset()可能是最简单的方法。

答案 1 :(得分:0)

假设您的第一个数组为$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是您想要的数组。