PHP为Foreach循环组合阵列

时间:2016-03-31 15:05:20

标签: php arrays foreach

如何为foreach循环组合两个数组。

我有两个数组可以产生foreach循环。

先谢谢你的帮助。

Primary Array:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Grape
            [date_created] => 2016-03-30 14:19:12
        )

    [1] => Array
        (
            [id] => 2
            [name] => Coconut
            [date_created] => 2016-03-30 14:22:54
        )

-

Secondary Array:

Array
(
    [0] => Array
        (
            [id] => 1
            [fruit_id] => 1
            [item_id] => 1
            [ppk] => 0
            [ppo] => 2342420
            [image] => 6450983014191211.jpg 
            [url] => 
        )

    [1] => Array
        (
            [id] => 2
            [fruit_id] => 1
            [item_id] => 10
            [ppk] => 343353
            [ppo] => 0
            [image] => 64509830141912110.jpg 
            [url] => http://yahoo.com
        )

    [2] => Array
        (
            [id] => 3
            [fruit_id] => 2
            [item_id] => 1
            [date_created] => 2016-03-30 14:22:54
            [date_last_change] => 2016-03-30 14:14:48
            [ppk] => 0
            [ppo] => 2323120
            [image] => 6450983014225421.jpg 
            [url] => 
        )

    [3] => Array
        (
            [id] => 4
            [fruit_id] => 2
            [item_id] => 11
            [date_created] => 2016-03-30 14:22:54
            [date_last_change] => 2016-03-30 14:14:48
            [ppk] => 232342000
            [ppo] => 0
            [image] => 64509830142254211.jpg 
            [url] => http://msn.com
        )

    [4] => Array
        (
            [id] => 5
            [fruit_id] => 2
            [item_id] => 12
            [date_created] => 2016-03-30 14:22:54
            [date_last_change] => 2016-03-30 14:14:48
            [ppk] => 34343400
            [ppo] => 0
            [image] => 64509830142254212.jpg 
            [url] => http://fussball.com
        )

注意:

字段“fruit_id”取自主数组中“id”的字段

结果:

//When I'm doing foreach loop, it should must result like this:

ID: 1
Fruit Name: Grape
Item ID: 1|10
PPK: 0|343353
PPO: 2342420|0
Image: 6450983014191211.jpg|64509830141912110.jpg
URL: ""|http://yahoo.com

------------------------------------------------------------------------

ID: 2
Fruit Name: Coconut
Item ID: 1|11|12
PPK: 0|232342000|232342000
PPO: 2323120|0|0
Image: 6450983014225421.jpg|64509830142254211.jpg|64509830142254212.jpg
URL: ""|http://msn.com|http://fussball.com

请帮忙。

先谢谢你。

1 个答案:

答案 0 :(得分:2)

因此,您需要使用一些不同的东西来获得预期的输出。

要从第二个数组中获取第一个数组的每个id的所有相关数组,可以使用array_filter()过滤掉那些subArrays。

然后,当打印出相关数组中的数据时,您可以使用array_column()获取要从每个subArray和implode()显示的特定数据,以将其转换为字符串。

现在,如果您希望所有空值都显示为"",您可以使用array_map()快速遍历要打印的数据,然后替换它。

对于分隔符,您可以检查它是否是最后一个元素,如果没有打印出分隔符。

$last = count($firstArray) - 1;
foreach($firstArray as $k => $v){

    $related = array_filter($secondArray, function($value)use($v){
        return $value["fruit_id"] == $v["id"];
    });

    echo "ID: " . $v["id"] . PHP_EOL;
    echo "Fruit Name: " . $v["name"] . PHP_EOL;
    echo "Item ID: " . implode("|", array_column($related, "item_id")) . PHP_EOL;
    echo "PPK: " . implode("|", array_column($related, "ppk")) . PHP_EOL;
    echo "PPO: " . implode("|", array_column($related, "ppo")) . PHP_EOL;
    echo "Image: " . implode("|", array_column($related, "image")) . PHP_EOL;
    echo "Url: " . implode("|", array_map(function($v){return $v == "" ? '""' : $v;}, array_column($related, "url"))) . PHP_EOL;


    if($k != $last)
        echo PHP_EOL . "------------------------------------------------------------------------" . PHP_EOL . PHP_EOL;

}