The most straightforward method would seem to me to be recursively.
Have the function look at each element of the array. If the value is a user, have the function add the appropriate child array and then call itself to examine the elements of that child array.
You'll get into trouble if there are loops, though.
A more efficient data structure would be to distinguish between users and non-users and keep them separate to begin with; look up child arrays as needed.
Now, I think you have it "wrong" but essentially you are making it that via array_map and array_fill_keys
So, I tried commenting out:
Code:
$accounts = array_map(func, $accounts);
...and viola!
Your solution is exactly what I was hoping for but just couldn't mentally get to... To be honest, once I saw the do() while() with the "check bit" I had a total "duh you idiot!" moment...
I had looked briefly at array_intersect_key as well but couldn't wrap my mind around it
For the sake of learning I'll definitely have to mess around more with these "higher level" array functions...
Much appreciated for the assistance... as always =)
***If your problem has been solved, PLEASE click the RESOLVED LINK under "Thread Tools"***
Huh? Oh ... right. Nope, nothing to do with me, mate.
Happy Late Christchurch Show Day, perhaps?
/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!
dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket
Not sure you have my source array correct but it is:
Ah; well, my interpretation of your source array description had 'a', 'b', 'orange', etc. as values, and I wanted them as keys (with empty strings for the values). That's what the first line of my function did. If that's already the form it's in, then that line isn't needed.
Originally Posted by dalecosp
Happy Late Christchurch Show Day, perhaps?
Just by coincidence a video has surfaced which might be the last such documenting of the city before it changed forever.
So I've had the code in play for a few weeks now and I've found some anomalies I just can't shake.
It works 99.8%...
In some cases the number of users I get after the matrix is condensed, is less than expected, e.g. I know I start with 'x' unique users and in the end, some users are missing completely. I know that there will be loss due to duplicates being removed as part of the condensation, but, afterwards I should still have the same number of unique users.
In some cases the routine fails to merge a partially-condensed matrix into the main matrix, e.g. orphaned accounts, so I end up with one big condensed matrix and one smaller that could have been condensed above.
Now, I'm not sure if this is happening because when we assign an array of users to its found 'parent' and unset it, we are skipping the processing on the users in that array?
That said, this has been wildly successful and immensely helpful.
Any thoughts on the anomalies would be graciously appreciated.
Thanks =)
***If your problem has been solved, PLEASE click the RESOLVED LINK under "Thread Tools"***
Any thoughts on the anomalies would be graciously appreciated.
Planetary Alignment?
Sun spots?
I'm not sure if I can be serious, but these thoughts have occurred:
1. How are you enforcing uniqueness of ($key) ?
2. Are you using array_intersect_key as Weedpacket suggested? If so, what about this?
Originally Posted by PHP Manual
array_intersect_key() returns an array containing all the entries of array1 which have keys that are present in all the arguments.
Any chance that some entries have missing keys? So many of these types of bugs seem to have to do with data anomalies....
/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!
dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket
Code: that looks pretty deep. Have you ever tried Xdebug? I finally set it up last week, and actually caught a bug with it a day or so later
/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!
dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket
So in playing around with the API a bit more I am able to return a list of users that OWN other users.
So I think what I may do here is built a skeleton array that holds the base hierarchy and then go through each and attach the users underneath.
This seems a little "uglier" to me as I'll have to make an additional API call, one to get the entire user list and then one for the parents.
Then I'll have to essentially loop (several times) to build the hierarchy and then loop the final array against the entire user set again and unset all users found in the matrix to find all users outside the matrix.
Oye
***If your problem has been solved, PLEASE click the RESOLVED LINK under "Thread Tools"***
I've only recently started with Xdebug, but I found it really handy dealing with what I *think* are similar problems. As Bradgrafelman was ranting the other day in my Debugging Javascript similes thread (Post #9, if you must ask), it's kind of handing to run through things in order, step by step and hover over variables and such. You occasionally find yourself saying, "What? *That* is Null?!!" and so on, and it *can* help quite a bit at times.
/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!
dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket
Bookmarks