2个数组使用PHP将数据匹配为1个数组


2 arrays match data into 1 array with PHP

我有两个数组,第一个数组有我的项目的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中的第一个数组中。

我应该做些什么来匹配(例如rgInventory中的id)与(例如$backpack = array();)同一数组中rgDescriptions中的name

向您致以问候。

试试这个:

<?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
                )
        )
)

希望这能有所帮助。

您可以使用函数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)
  }
}