左联接但在PHP数组中 [英] LEFT JOIN but in PHP Array

查看:38
本文介绍了左联接但在PHP数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组.

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
        )

)

我想显示这两个数组,但是第一个数组是一个类别,第二个数组具有元素. 关系在id(第一个数组)和data_id(第二个数组)之间. 在MySQL中是简单的LEFT JOIN,但是我不知道PHP中是否有类似的函数. 我只是尝试编写一些if等等的代码,但是没有成功. 我想要一个简单的解决方案,所以如果不可能的话,告诉我.

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.

干杯!

推荐答案

就像提到的jeroen一样,您可以遍历两个数组并仅获取具有连接的那些元素.

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

这篇关于左联接但在PHP数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆