Nodral Posted August 9, 2012 Share Posted August 9, 2012 Morning all I have 2 multidimentional arrays, where theoretically they can continue to the nth dimension. Is there a way (without using a horrible recursive function) I can iterate through both arrays and only return values where the keys are the same but the values are different? Quote Link to comment https://forums.phpfreaks.com/topic/266848-more-array-hell/ Share on other sites More sharing options...
MMDE Posted August 9, 2012 Share Posted August 9, 2012 What you could do is loop through each of the arrays in the array and put them all together in one large new array. You will have to check if the key you add already exists in the array, if it does, you must check if it's the same value or different. If it's different, then you can store that somewhere else (but these are the values you want to "collect", or maybe the one that already is in the "large array", so make sure you store them all and where you got them from), if it is not different you don't need to add it to the "large array" again! Sorry if there is some kind of logical error, it made sense in my tired head! Quote Link to comment https://forums.phpfreaks.com/topic/266848-more-array-hell/#findComment-1368054 Share on other sites More sharing options...
Christian F. Posted August 9, 2012 Share Posted August 9, 2012 Short answer: No. If you have an unknown number of dimensions in your arrays, you will need to use a recursive function to traverse them all. That's just the reality of it. Quote Link to comment https://forums.phpfreaks.com/topic/266848-more-array-hell/#findComment-1368100 Share on other sites More sharing options...
peipst9lker Posted August 9, 2012 Share Posted August 9, 2012 Why are recursive functions horrible? Used properly they're extremely powerful! Quote Link to comment https://forums.phpfreaks.com/topic/266848-more-array-hell/#findComment-1368103 Share on other sites More sharing options...
MMDE Posted August 9, 2012 Share Posted August 9, 2012 Short answer: No. If you have an unknown number of dimensions in your arrays, you will need to use a recursive function to traverse them all. That's just the reality of it. There isn't an unknown number of dimensions in his array. It's a 2 dimensional array, at least he states so... Meaning it's an array that contains arrays! If what he says is correct, I think what I said/thought will work. Quote Link to comment https://forums.phpfreaks.com/topic/266848-more-array-hell/#findComment-1368110 Share on other sites More sharing options...
Christian F. Posted August 9, 2012 Share Posted August 9, 2012 Read his post again, MMDE. Quote Link to comment https://forums.phpfreaks.com/topic/266848-more-array-hell/#findComment-1368112 Share on other sites More sharing options...
MMDE Posted August 9, 2012 Share Posted August 9, 2012 Read his post again, MMDE. Oh, now I get it... ' Urgh, I should just go to bed! Quote Link to comment https://forums.phpfreaks.com/topic/266848-more-array-hell/#findComment-1368113 Share on other sites More sharing options...
MMDE Posted August 10, 2012 Share Posted August 10, 2012 Would I be wrong to say that the idea behind my post would be a good idea? I mean loop through all the data, making a "map" of everything you're going through, separating and collecting the things you want to collect? I think it should be quite easy to write if it's a recursive method inside a class, because then the recursive class can store the map and the collection in a private property in the class. Quote Link to comment https://forums.phpfreaks.com/topic/266848-more-array-hell/#findComment-1368295 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.