2

I'm trying to compare two arrays, and extract some info of one of them and store it at another array (3rd). The structure of these two arrays looks like this:

Array(
[0] => stdClass Object
    (
        [Id] => 123
        [Size] => small
        [Color] => yellow
    )
[1] => stdClass Object
    (
        [Id] => 456
        [Size] => large
        [Color] => red
    )
[2] => stdClass Object
    (
        [Id] => 789
        [Size] => medium
        [Color] => blue
    )
)
Array
(
[0] => stdClass Object
    (
        [Id] => 456
        [Size] => large
        [Color] => red
        [Available] => true
    )

[1] => stdClass Object
    (
        [Id] => 123
        [Size] => small
        [Available] => false
    )
[2] => stdClass Object
    (
        [Id] => 789
        [Size] => medium
        [Color] => blue
        [Available] => true
    )
)

In the example, I need to store the property "available" only if the ID of these arrays are equal.

What I tried to do:

for (var $i = 0; $i < count($arrayA); $i++) {
    if ($ArrayA[$i]->Id == $arrayB[$i]->Id) {
        $getAvailable = $arrayB[$i]->Available;
    }
}

The problem:

Since we can have the situation where the property id in array A, position 0 is in a different position in array B, which can have a different structure, I have no idea how can I access the information I need (available), from one array comparing the ids of ArrayB and ArrayA.

I am sorry if I didn’t explain it better, because it’s a bit hard to put in words this situation, but let me know if you have any questions, and please, help me finding a solution :)

Desired array should look like this:

Array(
[0] => stdClass Object
(
    [Id] => 123
    [Size] => small
    [Color] => yellow
    [Available] => false
)
sergioviniciuss
  • 4,596
  • 3
  • 36
  • 50
  • whats the desired *new* array look like?? –  Nov 11 '16 at 03:21
  • I edited the question and added this information in the end. @Dagon – Eduardo Pereira Nov 11 '16 at 03:25
  • ah ok, that makes sense. any change of getting ID as the key to start with? if not its loop on array doing a search on the other for each id –  Nov 11 '16 at 03:27
  • http://stackoverflow.com/questions/6661530/php-multi-dimensional-array-search but it would probably be faster to loop the 2nd array first to use the Id as the key, then loop the first array –  Nov 11 '16 at 03:30
  • the problem is that if this array he mentioned has a lot more information than he put in the example, doing this loop may be too costly in terms of performance.. am I right?@Dagon – sergioviniciuss Nov 11 '16 at 03:34
  • don't use `$ArrayA[$i]` since they won't lineup as usual, just use a simple `foreach`, then make an `if` comparison on the ids inside – Kevin Nov 11 '16 at 03:36
  • Thanks for tip @Ghost ;-) – Eduardo Pereira Nov 11 '16 at 03:39

2 Answers2

1
$arr1 = array(
    0 => (object)(array(
        'Id' => '123',
        'Size' => 'small',
        'Color' => 'yellow'
    )),
    1 => (object)(array
    (
        'Id' => '456',
        'Size' => 'large',
        'Color' => 'red'
    )),
    2 => (object)(array
    (
        'Id' => '789',
        'Size' => 'medium',
        'Color' => 'blue'
    ))
);
$arr2 = array(
    0 => (object)(array
    (
        'Id' => '456',
        'Size' => 'large',
        'Color' => 'red',
        'Available' => 'true'
    )),
    1 => (object)(array
    (
        'Id' => 123,
        'Size'=> 'small',
        'Available' => 'false'
    )),
    2 => (object)(array
    (
        'Id' => '789',
        'Size' => 'medium',
        'Color' => 'blue',
        'Available' => 'true'
    ))
);

First: declare a new variable array which will later stored your new structured array

$newArr = array();

Then you loop your first array $arr1

foreach($arr1 as $row1) {
}

then inside first loop make another loop which find it's match id from the second array $arr2 just compare there id and if it is match merge them

foreach($arr2 as $row2) {
   if ($row1->Id == $row2->Id) {
       // do something
   }
}

then push them but before you merge them you must convert them into an associative array

$newArr[] = array_merge((array)$row1, (array)$row2);

into your new array. and just stop the sub loop after you find it's match id using

break;

so your foreach loop would be like this

$newArr = array();
foreach($arr1 as $row1) {
    foreach($arr2 as $row2) {
        if ($row1->Id == $row2->Id) {
            $newArr[] = array_merge((array)$row1, (array)$row2);
            break;
        }
    }
}
Beginner
  • 4,118
  • 3
  • 17
  • 26
0

This is not efficient for a large array but here you go

function getEqual($arrayA, $arrayB){

 foreach($arrayA as $aIndex => $aData){
     $id = $aData['Id'];
      $aData['Available'] = false;

     foreach($arrayB as $bIndex => $bData){
         if($id == $bData['Id']){
             $aData['Available'] = true;
         }
     }

     $arrayA[$aIndex] = $aData;
 }

 return $arrayA;

}

the_wizard
  • 477
  • 5
  • 9
  • What would be efficient then? Could you give me a hint about techniques? (btw - the last curly brace slipped out of the code block) – stewo Nov 11 '16 at 03:45