1

I have two arrays.

Array
(
    [0] => Array
        (
            [id] => 12
            [user_id] => 1
            [date] => 2013-10-21 23:01:52
            [type] => 1
            [name] => Wypłata UNDICOM
            [quantity] => 0
            [value] => 1700
        )

    [1] => Array
        (
            [id] => 13
            [user_id] => 1
            [date] => 2013-10-21 23:01:52
            [type] => 0
            [name] => Rata (gwarancja MacBook Air)
            [quantity] => 0
            [value] => 90
        )

    [2] => Array
        (
            [id] => 16
            [user_id] => 1
            [date] => 2013-10-21 23:01:52
            [type] => 0
            [name] => TESCO (zakupy)
            [quantity] => 0
            [value] => 0
        )

)
Array
(
    [0] => Array
        (
            [id] => 3
            [data_id] => 16
            [name] => Coca-Cola
            [quantity] => 2
            [value] => 5
        )

    [1] => Array
        (
            [id] => 4
            [data_id] => 16
            [name] => Pizza
            [quantity] => 1
            [value] => 10
        )

)

I want to display this two arrays, but first array is a category and second have elements. The relations are between id (first array) and data_id (second array). In MySQL is simple LEFT JOIN, but i have no idea if is function like that in PHP. I just tried code some if's etc. but without success. I want a simple solution, so if it's impossibile then tell me that.

Cheers!

4
  • 2
    Where are you getting the data from? I would try to fix it there, but if you can't, I would loop once through the first array to generate an array where the key is the ID. That would make looking up values extremely easy. Commented Oct 31, 2013 at 1:32
  • @Grzegorz Can you tell us what's the end result you're expecting Commented Oct 31, 2013 at 1:38
  • I want to do a mathematical operations on this arrays, but as You can see, there is a Group (first array) and optional with element lists (array second). I need to assigned all, like Group -> Elements, or if null elements or dosnt exists, get only Group Commented Oct 31, 2013 at 1:42
  • I has changed arrays, so if there is a items for category it create in category array a new array named bills. But how to displays arrays with some filtering? I want to display all arrays with 'type'=>1 ? Commented Oct 31, 2013 at 1:55

1 Answer 1

1

Like jeroen mentioned, you can loop through both arrays and get only those elements which have a join.

$len1 = count(array1);
$len2 = count(array2);

for($i = 0 ; $i < len1 ; $i++)
{
   for($j = 0 ; $j < len2 ; $j++)
   {
       if($array1[$i]['id'] == $array2[$j]['data_id'])
       {
            //some processing which you want to do with this data
       }
   }
}
Sign up to request clarification or add additional context in comments.

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.