cocksparrer
cocksparrer

Reputation: 219

PHP Combine Arrays For Foreach Loop

How to combine two arrays for foreach loop.

I have two arrays for to be resulted in foreach loop.

Thank you in advanced for your help.

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
        )

Notes:

field "fruit_id" is taken from field of "id" in Primary Array

And the result:

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

Please help.

Thank you in advanced.

Upvotes: 0

Views: 130

Answers (1)

Rizier123
Rizier123

Reputation: 59681

So there are a few different things you need to use to get your expected output.

To get all related arrays from your second array for each id of your first array, you can use array_filter() to filter out exactly those subArrays.

Then when it comes down to printing out the data from the related arrays, you can use array_column() to get the specific data which you want to show from each subArray and implode() to convert it into a string.

Now if you want all empty values to be shown as "" you can quickly loop through the data which you want to print out with array_map() and just replace that.

And for the separator you can just check if it's the last element or not and if not print out the separator.

$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;

}

Upvotes: 2

Related Questions