eits Posted August 4, 2009 Share Posted August 4, 2009 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??? Link to comment https://forums.phpfreaks.com/topic/168783-merge-two-arrays-so-value-from-array2-goes-at-the-end-of-array1/ Share on other sites More sharing options...
Bjom Posted August 4, 2009 Share Posted August 4, 2009 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']) Link to comment https://forums.phpfreaks.com/topic/168783-merge-two-arrays-so-value-from-array2-goes-at-the-end-of-array1/#findComment-890489 Share on other sites More sharing options...
eits Posted August 4, 2009 Author Share Posted August 4, 2009 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? Link to comment https://forums.phpfreaks.com/topic/168783-merge-two-arrays-so-value-from-array2-goes-at-the-end-of-array1/#findComment-890497 Share on other sites More sharing options...
Bjom Posted August 4, 2009 Share Posted August 4, 2009 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'] Link to comment https://forums.phpfreaks.com/topic/168783-merge-two-arrays-so-value-from-array2-goes-at-the-end-of-array1/#findComment-890500 Share on other sites More sharing options...
eits Posted August 4, 2009 Author Share Posted August 4, 2009 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 ) ) ) ) Link to comment https://forums.phpfreaks.com/topic/168783-merge-two-arrays-so-value-from-array2-goes-at-the-end-of-array1/#findComment-890508 Share on other sites More sharing options...
Bjom Posted August 4, 2009 Share Posted August 4, 2009 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... Link to comment https://forums.phpfreaks.com/topic/168783-merge-two-arrays-so-value-from-array2-goes-at-the-end-of-array1/#findComment-890519 Share on other sites More sharing options...
eits Posted August 4, 2009 Author Share Posted August 4, 2009 An array within an array within an array... Yes... I see whats happened sort of! Thankyou for your help, I'll need an hour or so to see if I can get this to work lol.. I'll keep you posted! Again, Many thanks! Link to comment https://forums.phpfreaks.com/topic/168783-merge-two-arrays-so-value-from-array2-goes-at-the-end-of-array1/#findComment-890527 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.