0

Data is coming from 2 differents SQL request, so i have 2 arrays like :

FIRST ARRAY
array(6) {
  [0]=>
  array(2) {
    ["edible"]=>
    string(3) "600"
    ["Food"]=>
    string(4) "Fruit"
  }
  [1]=>
  array(2) {
    ["edible"]=>
    string(3) "500"
    ["Food"]=>
    string(6) "Vegetables"
  }
  [2]=>
  array(2) {
    ["edible"]=>
    string(4) "1000"
    ["Food"]=>
    string(3) "meat"
  }
  ...


 SECOND ARRAY
array(5) {
  [0]=>
  array(2) {
    ["out-of-date"]=>
    string(3) "17"
    ["Food"]=>
    string(4) "Fruit"
  }
  [1]=>
  array(2) {
    ["out-of-date"]=>
    string(3) "54"
    ["Food"]=>
    string(3) "Vegetables"
  }
  [2]=>
  array(2) {
    ["out-of-date"]=>
    string(2) "60"
    ["Food"]=>
    string(3) "meat"
  }
 ...  

What i would like is to merge the two arrays, but not with a function like array_merge or array_merge_recursive. Because i would like to just add a row (key + value) like

["out-of-date"]=>
    string(3) "17""

in the first array if we have the same Food (key)

output result desired :

array(6) {
  [0]=>
  array(3) {
    ["out-of-date"]=>
    string(3) "17"
    ["edible"]=>
    string(3) "600"
    ["Food"]=>
    string(4) "Fruit"
  }
...
2
  • why not write a join in sql itself ? don't you have any relation between both the tables? Commented Jun 6, 2016 at 12:37
  • I don't really know how i should do, because i have differents where clauses. Commented Jun 6, 2016 at 12:42

2 Answers 2

1

You need to make a loop inside loop and verify the food key, if is equal merge the values to another array, Try this code:

<?php

$array1 = array();

$array1[] = array('food' => 'Fruit', 'edible' => '600');
$array1[] = array('food' => 'Vegetables', 'edible' => '500');
$array1[] = array('food' => 'meat', 'edible' => '700');

$array2 = array();
$array2[] = array('food' => 'Fruit', 'out-of-date' => '17');
$array2[] = array('food' => 'Vegetables', 'out-of-date' => '15');

$array_merged = array();


foreach ($array1 as $key => $value) {
    $array_merged[$key] = $value;
    foreach ($array2 as $ke => $val) {
        if ($val['food'] == $value['food']) {
            $array_merged[$key]['out-of-date'] = $val['out-of-date'];
        }
    }
}

print_r($array_merged);
Sign up to request clarification or add additional context in comments.

3 Comments

It works like a charm ! Thanks you :) I was trying a thing like this, but i failed at $array_merged[$key]['out-of-date'] = $val['out-of-date']; .
@sleakerz It's nothing, please, mark the answer as useful :D
i realized something : if if ($val['food'] == $value['food']) is false, then i lose my data. I should do an "else" with a push, to push the line no ?
1

I'm not sure about structure of your database table. But let's you have two tables in your MySql database:

First - edible | Food

Second - out_of_date | Food

So you can query your array by using join mysql operation. You need sql like below:

SELECT * 
FROM First
LEFT JOIN Second ON First.Food=Second.Food

Also, you can use additional conditions for all tables like:

SELECT * 
FROM First
LEFT JOIN Second ON First.Food=Second.Food
WHERE First.edible = 1000 AND Second.out_of_date = 10

You can find more information here: http://dev.mysql.com/doc/refman/5.7/en/join.html

Comments

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.