All In One Script



PHP,HTLM,CSS,Jquery,AJAX,Javascript and etc doubts and sample codes

  • Home
  • Javascript
  • PHP
  • CSS
  • SQL/MYSQL

How can I merge PHP arrays?

by Blogger 4:27:00 AM array merge PHP

How can I merge PHP arrays?

I have two arrays of animals (for example).
$array = array(
    array(
        'id' => 1,
        'name' => 'Cat',
    ),
    array(
        'id' => 2,
        'name' => 'Mouse',
    )
);

$array2 = array(
    array(
        'id' => 2,
        'age' => 321,
    ),
    array(
        'id' => 1,
        'age' => 123,
    )
);
How can I merge the two arrays into one by the ID?

Answer:

This does what Erik suggested (id no. as array key) and merges vlaues in $array2 to $results.
$results = array();

foreach($array as $subarray)
{
    $results[$subarray['id']] = array('name' => $subarray['name']);
}

foreach($array2 as $subarray)
{
    if(array_key_exists($subarray['id'], $results))
    {
        // Loop through $subarray would go here if you have extra 
        $results[$subarray['id']]['age'] = $subarray['age'];
    }
}






READ MORE
SHARE :

Search This Blog

Followers

  • Popular
  • Recent
  • Comments
    How to efficiently iterate over each Entry in a Map?
    Serving XHTML and self-closing tags
    How to avoid Java code in JSP files?
    Get first key in a (possibly) associative array?
    Does finally always execute in Java?
    Solved : curl_init() function not working in Ubuntu
    Why does this code using random strings print “hello world”?
    In Java, difference between default, public, protected, and private
    How can I make the cursor a hand when a user hovers over a list item?
    Length of a JavaScript object

Instagram

About

Popular Posts

  • How to efficiently iterate over each Entry in a Map?
    How to efficiently iterate over each Entry in a Map? If I have an object implementing the  Map  interface in Java and I wish to iterate...
  • Serving XHTML and self-closing tags
    Serving XHTML and self-closing tags I am trying to follow the xhtml 1.0 strict standard as I am creating my website. Right now, validat...
  • How to avoid Java code in JSP files?
    How to avoid Java code in JSP files? I'm new to Java EE and I know that something like the following three lines <%= x + 1 %...
  • Get first key in a (possibly) associative array?
    Get first key in a (possibly) associative array? What's the best way to determine the first key in a possibly associative array? My...
  • Does finally always execute in Java?
    Does finally always execute in Java? I have a try/catch block with  return s inside it. Will the finally block be called? For example...
  • Solved : curl_init() function not working in Ubuntu
    Solved : curl_init() function not working in Ubuntu  Here solved the error  Fatal error: Call to undefined function curl_init() ...
  • Why does this code using random strings print “hello world”?
    Why does this code using random strings print “hello world”? The following print statement would print "hello world". Could a...
  • In Java, difference between default, public, protected, and private
    In Java, difference between default, public, protected, and private In Java , are there clear rules on when to use each of access modifi...
  • How can I make the cursor a hand when a user hovers over a list item?
    How can I make the cursor a hand when a user hovers over a list item? I've got a list, and I have a click handler for its items: ...
  • Length of a JavaScript object
    Length of a JavaScript object If I have a JavaScript object, say var myObject = new Object (); myObject [ "firstname" ] ...

statcounter



statcounter



Template Created By ThemeXpose & Blogger Templates