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.

4
  • Similar: 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? Commented Mar 31, 2016 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 Commented Mar 31, 2016 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? Commented Mar 31, 2016 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 Commented Mar 31, 2016 at 15:27

1 Answer 1

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;

}
Sign up to request clarification or add additional context in comments.

6 Comments

I see nothing as the result :)
@cocksparrer This should just show how you can use the fruid_id from your second array to get the name from your first array. If your problem is how to print the output in that format, then I can update my answer.
yes can you please give me an example output print? thank you.
@cocksparrer Just looked at your output again and is this the full expected output?
@cocksparrer I read your question wrong. I updated my answer now, so it should solve your problem
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.