结合使用PHP键/值和输出组合阵列两个数组 [英] Combine two arrays on key/value and output combined array with PHP

查看:127
本文介绍了结合使用PHP键/值和输出组合阵列两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

货架我对这个大脑,发现了类似的情况很多例子,不过解决方案似乎并不匹配。

Racking my brain on this, found many examples of similar situations however the solutions don't seem to match up.

我有两个数组被建成为SQL查询在不同数据库的结果。

I have two arrays being built as the result of SQL queries on different databases.

一个人来通过格式化为这样的:

One is coming through formatted as such:

$data = array([$sku] => array(['LocalSKU'] => $sku,
                              ['Price'] => $msrp,
                              ['Price2'] => $wholesale,
                              ['Price3'] => $distributor,
                              ['Price4'] => $map))

另外数组被格式化为这样的:

The other array is formatted as such:

$matchme = array([0] => array(['entity_id'] => $entity_id,
                              ['sku'] => $sku,
                              ['type_id'] => $type_id))

目前,我可以得到单独的数据通过投其所好:

Currently, I can get the individual data to match up 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'];

然而,当我试图在两个数组合并匹配的行,我得到一个空数组。 $ data数组包含74唯一$ SKU和$ matchme是检查那些$ SKU的针对数据库和61元返回一个数组的结果。因此,合并后的数组应该基于$ SKU有61元素再配定价数据。

However, when I try and merge the matching rows in both arrays, I get an empty array. The $data array contains 74 unique $sku, and $matchme is the result of checking those $sku's against a database and returning an array with 61 elements. So, the combined array should have 61 elements with matched pricing data based on the $sku.

我如何在尝试建立组合阵列的下方,任何人都可以点我对我在做什么错了?

How I am attempting to build the combined array is below, can anyone point me towards what I am doing wrong?

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']
                               );
}
}

在上面的例子中,评估$键0,['SKU']是匹配的。

In the above example, evaluate $key as 0, and the value of ['sku'] are matching.

------------------------编辑---------------------- ---

------------------------Edited-------------------------

根据要求这里是截断空间的print_r($数据)的结果是:

Per request, here is the result of print_r($data) truncated for 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
    ) ) 

根据要求在这里的print_r($ matchme)的结果截断空间:

Per request, here is the result of print_r($matchme) truncated for 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
    ) ) 

根据要求这里是$ matched_luggage的期望的结果:

Per request, here is the 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))

每个SKU匹配一个额外的数组。

with an additional array per matched sku.

推荐答案

试试这个:

foreach ($matchme as $arrProduct) {
    if (isset($data[$arrProduct['sku']])) {
        $arrMerged[$arrProduct['sku']]=array_merge($arrProduct, $data[$arrProduct['sku']]);
    }
}

print_r($arrMerged);

您code不工作的原因是在这里:

The reason your code doesn't work is here:

if(in_array($matchme[$key]['sku'], $data)) [...]

什么 in_array()所做的就是告诉你无论你的的(你的情况SKU字符串)形式存在数组值的草垛的(在你的情况,$数据)。你基本上是试图将字符串匹配到一个数组,而不是另一个字符串。

What in_array() does is tell you whether your needle (in your case the SKU string) exists as a value of array haystack (in your case, $data). You are essentially trying to match a string to an array, rather than another string.

你真正想要的仅仅是相匹配的SKU字符串的$数据的关键,为此,使用isset()可能是最简单的方法。

What you really want is just to match the SKU string to the key of $data, for which isset() is probably the simplest approach.

这篇关于结合使用PHP键/值和输出组合阵列两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆