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? 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! 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. 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! 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. 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. 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! 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. Link to comment https://forums.phpfreaks.com/topic/266848-more-array-hell/#findComment-1368295 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.