加入2个多维数组

时间:2012-01-12 10:29:02

标签: php arrays

$items = array(
    array(
        'id' => 0,
        'name' => 'Simple Sword',
        'type' => 'weapon',
        'price' => 200,
        'value1' => 5,
        'value2' => 10,
        'value3' => 0,
        'value4' => 0,
        'value5' => 0
    ),
    array(
        'id' => 1,
        'name' => 'Iron Sword',
        'type' => 'weapon',
        'price' => 500,
        'value1' => 0,
        'value2' => 0,
        'value3' => 0,
        'value4' => 0,
        'value5' => 0
    )
);


$inventory = array(

    array(
        'item' => 0,
        'slot' => 1,
        'value1' => 0,
        'value2' => 0,
        'value3' => 0,
        'value4' => 0,
        'value5' => 0,
        'equipped' => 0
    ),

    array(
        'item' => 1,
        'slot' => 2,
        'value1' => 0,
        'value2' => 0,
        'value3' => 0,
        'value4' => 0,
        'value5' => 0,
        'equipped' => 1
    )  

);

我需要的是加入这两个多维数组,或者从“Items”数组中获取值,键等,并将它放在Inventory数组中,其中“item”id与Items数组中的id匹配。与SQL中的INNER JOIN语句类似。怎么样?我无法理解。

其次,我正在尝试打印出$ inventory数组,我尝试了以下内容,但它没有用:

foreach ($inventory as $a) {

    foreach ($a as $b) {

        echo $b['item'];

    }

}

它没有给我输出。

5 个答案:

答案 0 :(得分:1)

foreach($inventory as $key=>$val)
{
    if($val['item'] == $items[$key]['id'])
    {
        $newarr[] = array_merge($items[$key],$val);
    }
}

使用$newarr[]检查print_r($newarr),此处为输出:

Array
(
    [0] => Array
        (
            [id] => 0
            [name] => Simple Sword
            [type] => weapon
            [price] => 200
            [value1] => 0
            [value2] => 0
            [value3] => 0
            [value4] => 0
            [value5] => 0
            [item] => 0
            [slot] => 1
            [equipped] => 0
        )

    [1] => Array
        (
            [id] => 1
            [name] => Iron Sword
            [type] => weapon
            [price] => 500
            [value1] => 0
            [value2] => 0
            [value3] => 0
            [value4] => 0
            [value5] => 0
            [item] => 1
            [slot] => 2
            [equipped] => 1
        )

)

第二个问题,打印出$inventory数组:

foreach ($inventory as $a) 
{
    echo $a['item'];
    echo $a['slot'];
    echo $a['value1'];
    //...etc
}

答案 1 :(得分:1)

对你的第二个问题有点帮助:

foreach ($inventory as $a => $b) {
        echo $b['item'];
    }
}

答案 2 :(得分:1)

至于第一个问题,Zulkhaery Basrul给出了很好的答案。如果关系是一对一的话,我还会考虑使用break语句:

if($val['item'] == $items[$key]['id']) {
    $newarr[] = array_merge($items[$key],$val);
    break;
}

至于第二个问题:

foreach ($inventory as $invKey => $aInventoryItem) {
    echo $aInventoryItem['item'] . "\n";
}

答案 3 :(得分:1)

我认为你可以这样做以回应库存中的项目:

    foreach($inventory as $inv) {
        echo $inv['item'];
    }

答案 4 :(得分:0)

我相信你正在寻找这个功能:

// Join Arrays on Keys
function array_join($original, $merge, $on) {
    if (!is_array($on)) $on = array($on);
    foreach ($merge as $right) {
        foreach ($original as $index => $left) {
            foreach ($on as $from_key => $to_key) {
                if (!isset($original[$index][$from_key])
                    || !isset($right[$to_key])
                    || $original[$index][$from_key] != $right[$to_key])
                    continue 2;
            }
            $original[$index] = array_merge($left, $right);
        }
    }
    return $original;
}

您的方案的用法:

print_r(array_join($items, $inventory, array('id' => 'item')));

我已经向社群请求帮助优化此功能的LEFT JOIN版本(唯一的区别是$remove unsetarray_merge):How can I optimize my array_join (simulates a LEFT JOIN) function? < / p>