【问题标题】:2 arrays match data into 1 array with PHP2个数组用PHP将数据匹配到1个数组中
【发布时间】:2016-03-26 14:50:12
【问题描述】:

我有 2 个数组,第一个数组有例如我的项目的 ItemID,第二个数组有关于我的项目的描述。我想将数据匹配到 1 个数组中。

看起来像:

[rgInventory] => Array
    (
        [1234567890] => Array
            (
                [id] => 1234567890
                [classid] => 123456789
                [instanceid] => 987654321
                [amount] => 1
                [pos] => 1
            )
    )

[rgDescriptions] => Array
    (
        [192837465_918273645] => Array
            (
                [appid] => 730
                [name] => Something
            )
    )

数组中的项与ID的值不同,但它们的顺序相同,所以:

rgInventory 中第一个项目的描述位于 rgDescriptions 内的第一个数组中。

我应该怎么做才能匹配来自rgInventoryid与来自rgDescriptionsname在同一数组中,例如$backpack = array();

向你问好。

【问题讨论】:

  • 你可以使用array_merge()
  • @Drudge 但array_merge()rgInventory 中的第一项与rgDescriptions 中的第一项不匹配。
  • 如何将rgInventory 中的第一项与rgDescription 中的第一项匹配?没有什么共同点。
  • 我不知道,这就是我在这里问的原因。也许我可以使用for(); 并按顺序从rgInventory 添加值,然后以相同的顺序添加来自rgDescriptions 的值?但是该怎么做呢?
  • 您能告诉我们您尝试过的代码吗??

标签: php arrays json


【解决方案1】:

试试这个:

<?php
$array1 = array('rgInventory' => 
             array(
                    '1234567890' => array(
                                    'id' => 1234567890,
                                    'classid' => 123456789,
                                    'instanceid' => 987654321,
                                    'amount' => 1,
                                    'pos' => 1
                                    )
                        )
                );

$array2 = array(
               'rgDescriptions' => array(
                     '192837465_918273645' => array(
                            'appid' => 730, 'name' => 'Something')
                                    )
               );

创建新函数将两个数组合并为一个数组:

function array_sum_recursive($data1, $data2) {
       if (!is_array($data1) && !is_array($data2)) {
        return $data1 + $data2;
    }
    // deepest array gets precedence
    if (!is_array($data2)) {
        return $data1;
    }
    if (!is_array($data1)) {
        return $data2;
    }
     //merge and remove duplicates
    $keys = array_unique(array_merge(array_keys($data1), array_keys($data2)));
    foreach ($keys as $key) {
        if (isset($data1[$key]) && isset($data2[$key])) {
            $result[$key] = array_sum_recursive($data1[$key], $data2[$key]); 
        } else if (isset($data1[$key])) {
            $result[$key] = $data1[$key];
        } else {
            $result[$key] = $data2[$key];
        }
    }
    if(empty($result)){
        echo "no result";
        die();
    }else{
        return $result;
    }
}

将两个数组合为一个数组$newarray

$newonearray = array_sum_recursive($array1, $array2);
echo '<pre>';
print_r($newonearray);

?>

你会得到这个:

Array
(
    [rgInventory] => Array
        (
            [1234567890] => Array
                (
                    [id] => 1234567890
                    [classid] => 123456789
                    [instanceid] => 987654321
                    [amount] => 1
                    [pos] => 1
                )

        )

    [rgDescriptions] => Array
        (
            [192837465_918273645] => Array
                (
                    [appid] => 730
                    [name] => Something
                )

        )

)

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您可以使用函数each 获取两个数组的每个元素,然后将其与array_merge 合并并将这个新项目保存到备份数组。

    试试这样的

    <?php
    
    $rgInventory = ['firstInv' => ['invId' => 1], 'secondInv' => ['invId' => 2]];
    $rgDescriptions = ['firstDesc' => ['descId' => 1], 'secondDesc' => ['descId' => 2]];
    
    if (count($rgInventory) && count($rgInventory) == count($rgDescriptions)) {
        $backpack = [];
    
        while($inventory = each($rgInventory)) {
            $description = each($rgDescriptions);
            $item = array_merge($inventory['value'], $description['value']);
            $backpack[] = $item;
        }
    
        var_dump($backpack);
    }
    

    输出将是:

    array(2) {
      [0]=>
      array(2) {
        ["invId"]=>
        int(1)
        ["descId"]=>
        int(1)
      }
      [1]=>
      array(2) {
        ["invId"]=>
        int(2)
        ["descId"]=>
        int(2)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-18
      • 2020-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 2012-10-17
      相关资源
      最近更新 更多