0

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.

cocksparrer
  • 219
  • 1
  • 6
  • 17
  • Similar: http://stackoverflow.com/q/32061254/3933332 So you just want to loop through both arrays at once, so you can use the data from both arrays for your output? – Rizier123 Mar 31 '16 at 15:07
  • @Rizier123 yes I need to looping the both of arrays for resulting in foreach which the secondary array have the ID from primary array – cocksparrer Mar 31 '16 at 15:11
  • So the relation between the two arrays is the id element? Not the position of each subArray. And if yes can it be that you have multiple subArrays with the same id? – Rizier123 Mar 31 '16 at 15:13
  • @Rizier123 The Secondary array have the ID from the Primary array. "fruit_id" is the ID of fruit name. fruit_id => 2 is the ID of Coconut – cocksparrer Mar 31 '16 at 15:27

1 Answers1

2

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;

}
Rizier123
  • 58,877
  • 16
  • 101
  • 156