Jump to content

Merge two Arrays so value from array2 goes at the end of array1


eits

Recommended Posts

Hi, I have two arrays:

array1 = name, version, rating

array2 = name, version, rating

 

within both arrays are the same variants of name and version except the rating will be different. I am looking for a way to be able to merge these arrays and create a new array which would look like this:

newarray[] = name, version, rating1, rating2

 

Can anybody please help???

are those simple arrays each with 3 values only, or are there arrays in the arrays?

 

For the former: it would make things easier if you could rename the keys inside the array:

array1 = name, version, rating1

array2 = name, version, rating2

 

then all you need to do is:

array_replace($array1, $array2);

 

If that is not an option:

$array_combo = array('name'=>$array1['name'], 'version'=>$array1['version'], 'rating1'=>$array1['rating'], 'rating2'=>$array2['rating'])

Hi, I create both arrays like so (within a foreach loop):

 	$array[] =
					array("Name" => $name, 
					array("Version" => $version, 
					array("Rating" => $rating)));

	}

 

Also, I am getting array_replace is an unrecognised function? Is this redundant in newer versions of PHP?

No it's new. php 5.3+ (see manual). So it might be better to stick with solution 2.

 

Btw: If the key names for the rating do not matter THAT much an easier solution would be to simply add the second rating like so:

 

$array1['rating2'] = $array2['rating']

 

 

Hi, I'm having troubles with that, but I think its because I can only display by 'name' not by anything deeper in?

 

For example:

foreach($array as $user) {
echo $user['name']. "<br />";
}

will print all names, but if I try to do:

foreach($array as $rating) {
echo $rating['rating']. "<br />";
}

 

It doesn't?

 

 

When I print_r on the array it displays like this:

Array
(
    [0] => Array
        (
            [Name] => SimpleXMLElement Object
                (
                    [0] => Marcus
                )

            [0] => Array
                (
                    [Version] => SimpleXMLElement Object
                        (
                            [0] => 14.03
                        )

                    [0] => Array
                        (
                            [Rating] => SimpleXMLElement Object
                                (
                                    [0] => 17
                                )

                        )

                )

        )

foreach will return the items inside the arry that are on the same hierarchical level. Now have a look at the var_dump:

 

inside the outer array is 1 element with the key 0 which is an array itself and has the keys [Name] and 0

 

So the foreach will put this array into the variable you provide ($user and $rating respectively). Since there does not exist any key [rating]....no value....

 

The key [rating] exists in an array that is nested deeper inside:

 

$array[0][0][0]['rating]

 

You messed your array up a bit, methinks  ;)

 

Its structure is not as simple as it would seem from this:

array1 = name, version, rating

 

 

rather this:

$array[] =array("Name" => $name, array("Version" => $version, array("Rating" => $rating)));

produces an array within an array within an array within an array...

What for?

 

simply do this:

$array = ("Name" => $name, "Version" => $version, "Rating" => $rating);

 

That's in accord with your first post's layout...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.