1

I have two arrays I want to merge, but leave out elements from the 2nd array that match a value. (id and otherId)

I know how to achieve this with a 2nd loop inside a first loop, but I was wondering if this could be achieved using a single loop, or with a specific php function?

This is an example of what I'm doing now:

foreach ($array1 as $item)
{
    $id = $item['id'];

    foreach ($array2 as $key => $item)
    {
        if ($item['otherId'] == $id)
        {
            unset($array2[$key]);
        }
    }
}

$output = array_merge($array1, $array2);

There will be a lot of elements in both arrays, so I'm looking for the most efficient way of doing this.

Example of arrays:

$array1 = [
    [
        'id' => 1
        'name' => 'item 1'
    ],
    [
        'id' => 2
        'name' => 'item 2'
    ],
    [
        'id' => 3
        'name' => 'item 3'
    ],
    [
        'id' => 4
        'name' => 'item 4'
    ]
];

$array2 = [
    [
        'id' => 1,
        'otherId' => 2,
        'name' => 'item 2'
    ],
    [
        'id' => 2,
        'otherId' => 3,
        'name' => 'item 3'
    ],
    [
        'id' => 3,
        'otherId' => null,
        'name' => 'item 4'
    ],
    [
        'id' => 4,
        'otherId' => null,
        'name' => 'item 5'
    ]
];


$output = [
    [
        'id' => 1
        'name' => 'item 1'
    ],
    [
        'id' => 2
        'name' => 'item 2'
    ],
    [
        'id' => 3
        'name' => 'item 3'
    ],
    [
        'id' => 4
        'name' => 'item 4'
    ],
    [
        'id' => 3,
        'otherId' => null,
        'name' => 'item 4'
    ],
    [
        'id' => 4,
        'otherId' => null,
        'name' => 'item 5'
    ]
];
4

2 回答 2

2

Do you means this:

<?php

$array1 = array(
    array(
        'id' => 1,
        'name' => 'item 1'
    ),
    array(
        'id' => 2,
        'name' => 'item 2'
    ),
    array(
        'id' => 3,
        'name' => 'item 3'
    ),
    array(
        'id' => 4,
        'name' => 'item 4'
    )
);

$array2 = array(
    array(
        'id' => 1,
        'otherId' => 2,
        'name' => 'item 2'
    ),
    array(
        'id' => 2,
        'otherId' => 3,
        'name' => 'item 3'
    ),
    array(
        'id' => 3,
        'otherId' => null,
        'name' => 'item 4'
    ),
    array(
        'id' => 4,
        'otherId' => null,
        'name' => 'item 5'
    )
);

$output = $array1;
for($i=0,$c=sizeof($array2);$i<$c;$i++){
  if(is_null($array2[$i]['otherId']))
      array_push($output, $array2[$i]);
}

var_dump($output);

Live example see here

Also you can do array_push() directly into $array1 to reduce memory usage. Example here

于 2013-10-24T11:03:15.170 回答
0

这个功能怎么样:

function searchForId($id, $array) {
foreach ($array as $key => $val) {
   if ($val['uid'] === $id) {
       return $key;
    }
   }
   return null;
}

您可以使用它来搜索数组。为避免任何混淆,我只是将其粘贴以便更容易查看解决方案。感谢@Jakub Truneček,可以在https://stackoverflow.com/a/6661561/1238227找到完整的解决方案

于 2013-10-24T11:30:58.450 回答