-1

I have two arrays like:

array (size=4)
 0 => string '5' (length=1)
 1 => string '4' (length=1)
 2 => string '2' (length=1)
 3 => string '2' (length=1)
 3 => string '8' (length=1)

and one array more that I load from an XML file:

object(SimpleXMLElement)[1]
public 'book' => 
array (size=101)
  0 => 
    object(SimpleXMLElement)[2]
      public 'id' => string '1' (length=1)
      public 'title' => string 'p' (length=1)
  1 => 
    object(SimpleXMLElement)[3]
      public 'id' => string '2' (length=1)
      public 'title' => string 'pp' (length=2)
  2 => 
    object(SimpleXMLElement)[4]
      public 'id' => string '3' (length=1)
      public 'title' => string 'pen' (length=3)
  3 => 
    object(SimpleXMLElement)[5]
      public 'id' => string '4' (length=1)
      public 'title' => string 'lapton' (length=6)
      ......
      ......
  101 => 
    object(SimpleXMLElement)[103]
      public 'id' => string '101' (length=1)
      public 'title' => string 'title' (length=5)

I want to compare each value of key id of second array with key of first array for each value. When it's the same, I want to update value of key title of second array.

Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
ching
  • 122
  • 7
  • 2
    Possible duplicate of [PHP compare array](http://stackoverflow.com/questions/901815/php-compare-array) – Cheng Chen Dec 11 '15 at 03:45
  • I think not the same, in my case I want compare each value of each array. it's like many to many – ching Dec 11 '15 at 03:54

3 Answers3

1

Assuming your first array is $idArray and your second is $xmlArray, you could use something like this.

$result = array_map(function($xmlElement) use ($idArray) {
    if (in_array($xmlElement->id, $idArray)) {
      $xmlElement->title = 'updated value';
    }
    return $xmlElement;
}, $xmlArray);
Ben Rowe
  • 28,406
  • 6
  • 55
  • 75
0

Assumptions

  • the first array is called $array1
  • the second array is called $fromXML
  • the second array is not actually an array, it's a SimpleXMLElement with the following structure (psuedocode / JSONish syntax)
{
  'book' => {
    0 => SimpleXMLElement {
      'id' => 1,
      'title' => 'p'
    }
  }
}
  • I assume you can access the second array of elements with $fromXML['book']
  • I assume you can access an attribute of the first element with $fromXML['book'][0]['id']
  • I assume that you can set the text of the title of the first element with $fromXML['book'][0]['title'][0] = 'new title'

based on How can I set text value of SimpleXmlElement without using its parent? and PHP SimpleXML, how to set attributes? and PHP foreach change original array values

Solution

foreach($fromXML['book'] as $key => $element) {
  if(array_key_exists($element['id'], $array1)) {
    $fromXML['book'][$key]['title'][0] = $array1[$element->id];
  }
}

Caveat and troubleshooting

I didn't test this, just going off of the documentation. If I've misinterpreted the structure of your SimpleXMLElement array, try experimenting with var_dump($fromXML['some']['key']) until you find the right way to access the array/element

Note: Apparently, array_key_exists() performs better than in_array() on large arrays

Community
  • 1
  • 1
alexanderbird
  • 3,847
  • 1
  • 26
  • 35
-1

Try this for now

foreach($array1 as $arr1 => $val1){
  foreach($array2 as $arr2 =>$val2){
    if($arr1==$arr2){
        $val2['title']='update value';
    }    
  }
}
momouu
  • 711
  • 4
  • 14
  • your solution, it's match to me. just plus s.th with your answer, now it's work – ching Dec 11 '15 at 05:02
  • I was referring to the one who downvote. @ching good to know that it's working now. – momouu Dec 11 '15 at 05:07
  • Why loop over `$array1`? That means you loop over `$array2` once for every value of `$array1`, and every new element in `$array` means looping 100 more times .... Both @Ben Rowe and I used [`in_array($key, $array1)`](http://php.net/manual/en/function.in-array.php) instead - I think that's a cleaner and more descriptive way to do it. – alexanderbird Dec 11 '15 at 05:10
  • I agree that in_array is much cleaner way but does my solution really deserve a downvote? And you didn't use in_array in your solution. – momouu Dec 11 '15 at 05:24
  • You're right - but I used `array_key_exists()` instead which basically does the same thing (though [apparently it performs better on large arrays than `in_array` does](http://maettig.com/1397246220)). And I didn't downvote so I can't explain why you got the downvote – alexanderbird Dec 11 '15 at 05:29
  • Thanks for the info. Now I know some new functions. Sorry I thought that you were the one who downvoted. – momouu Dec 11 '15 at 05:31