Serenitee Posted December 25, 2008 Share Posted December 25, 2008 I know someday I will hit my lightbulb moment and it will all make sense - until then I'm hoping for pointers either in how to, or links or proper search terms: I have 2 arrays, as example: Array1 ( [0] => Array ( [0] => Five Dollars [1] => Five Dollars [2] => Ten Dollars ) [1] => Array ( [0] => Blue [1] => Purple [2] => Red ) [2] => Array ( [0] => Socks [1] => Gloves [2] => Scarf ) ) 1 and Array2 ( [0] => Array ( [0] => Gloves [1] => Socks [2] => Scarf [3] => Shawl ) [1] => Array ( [0] => Merino [1] => Cotton [2] => [3] => Alpaca ) [2] => Array ( [0] => Aunt Sue [1] => Katie [2] => Gramma [3] => Sarah ) [3] => Array ( [0] => Fingerless [1] => Striped [2] => Cabled [3] => Lace ) ) 1 I would like to compare these arrays, array1[2] and array2[0]. With these 2 arrays, I would like to create a 3rd. When a match is found, I would like the new array to have the comparison feature as '0' and grab each line of the other arrays as well. If no match is found - just add the info from the array it is found in. Confusing? I'd like the end output to look like this: Array3 ( [0] => Array ( [0] => Gloves [1] => Socks [2] => Scarf [3] => Shawl ) [1] => Array ( [0] => Merino [1] => Cotton [2] => [3] => Alpaca ) [2] => Array ( [0] => Aunt Sue [1] => Katie [2] => Gramma [3] => Sarah ) [3] => Array ( [0] => Fingerless [1] => Striped [2] => Cabled [3] => Lace ) [4] => Array ( [0] => Five Dollars [1] => Five Dollars [2] => Ten Dollars [3] => ) [5] => Array ( [0] => Blue [1] => Purple [2] => Red [3] => ) ) 1 Noting that the order wouldn't always match - but the values are correct (ie: Fingerless Gloves for aunt sue cost $5 using blue merino - the colors/type (fingerless), etc didn't get mixed up). Any that can light the way? I have looked through various array functions on php.net ( array_intersect_assoc, array_diff, array_diff_assoc, array_merge_recursive, the list goes on) to no avail they either seem 'not quite right' or 'definitely not right'. tia! Quote Link to comment Share on other sites More sharing options...
BloodyMind Posted December 25, 2008 Share Posted December 25, 2008 try to explain more, do u want to compare those arrays on what condition ? values in the database or what coz the socks as a string has nothing to do with five dollars, if you want to merge them, you can do it with a loop make an empty array and foreach through both arrays so it inserts key and value into the new array Quote Link to comment Share on other sites More sharing options...
Serenitee Posted December 25, 2008 Author Share Posted December 25, 2008 Array1 would read: Cost to make, color of item, Item Array2 would read: Item made of material for whom with what feature The only thing they have the same would be the Item - in this example, gloves would always be blue fingerless gloves - I am just hoping to come up with array3 that puts all of the information from both arrays together when there is a match - and when there is no match, still add the item to the array but leave the other attributes blank. So, they'd be compared based on item against each other. Make more sense or am I still having clarity issues? tia! Quote Link to comment Share on other sites More sharing options...
Serenitee Posted December 25, 2008 Author Share Posted December 25, 2008 Once it is in this third array, it will then be compared to a db (not that I think that will effect how it goes into this array however). Quote Link to comment Share on other sites More sharing options...
Serenitee Posted December 26, 2008 Author Share Posted December 26, 2008 array_union perhaps (which seems to be a hobbled together function, and not a true one) - am I on the right path? tia! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted December 26, 2008 Share Posted December 26, 2008 array_union perhaps (which seems to be a hobbled together function, and not a true one) - am I on the right path? tia! array_union() is not a PHP function. Perhaps you mean array_intersect()? Quote Link to comment Share on other sites More sharing options...
Serenitee Posted December 26, 2008 Author Share Posted December 26, 2008 array_intersect does not look to be what I need as it ignores any that do not match (any that do not match I would also like added to the 3rd array). But on this page: http://php.net/array_intersect Under 'user contributed' is where I see a hobbled together 'union' which looks to perhaps be on the right path for my needs - though perhaps not 'quite' right. Yes, I am struggling here lol Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.