English 中文(简体)
加入2个多层面阵列
原标题:Join 2 multidimensional array
  • 时间: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
    )  

);

我需要的是加入这2个多层面阵列,或从“项目”阵列中取出价值、钥匙等,并将其放在“项目”与项目阵列相匹配的库存阵列中。 Similiar to a INNER JOIN statement inkou. 如何? 我可以这样说。

第二,我正试图印出通风室,但我尝试了以下工作:

foreach ($inventory as $a) {

    foreach ($a as $b) {

        echo $b[ item ];

    }

}

这使我没有产出。

问题回答

a little help with your second problem:

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

关于第一个问题,Zulkhaery Basrul作了很好的回答。 如果关系是一对一:

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

关于第二个问题:

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

我认为,为了从库存中重复这一项目,你可以这样做:

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

<$newarr[] using print_r($newarr), here the production :

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
}

我认为,这一职能是你回顾的:

// 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 )));

I ve 要求社区帮助优化LEFT JOIN这一功能版本(唯一的差别是:_removearray_joint:)。 我如何能够优化我的阵列——职务(模拟LEFT JOIN)?





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签