1

我有两个数组。

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 等代码,但没有成功。我想要一个简单的解决方案,所以如果不可能,请告诉我。

干杯!

4

1 回答 1

1

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

$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
       }
   }
}
于 2013-10-31T03:24:52.877 回答