"Flatten" ResultArray() data
When I query a table for just one column that I know will have unique results, I would like to get a result from ResultArray() like that: array(1,2,3,4,5). What I get is array(array('ColumnName' => 1), array('ColumnName' => 2), ...
So when I want to check if a value is in that ResultArray, I have to loop through that array. Using in_array would be way more comfortable. After hours and hours of searching I found that solution:
$UserList = array_reduce($Users, function ($Result, $Item) { $Result[$Item['Name']] = $Item['UserID']; return $Result; }, array() );
That was okay, but now I'm facing the exact problem again and so I was wondering if there is any Garden function that either returns a flat array when querying db or at least a function like MakeThatRecordSetFlat()?
Best Answers
-
hgtonight MVP
There are some nice functions in
functions.general.php
.You might be interested in
array_column()
andConsolidateArrayValuesByKey()
.Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
2
Answers
Well it is returning a result set, rows with columns. It is only couple of lines to make a lookup. You could just simply loop through the values. it shouldn't take you a couple hours to work that out lol.
grep is your friend.
To explain why I felt the need for some function like that, here's some code:
Then I wanted to do an array_diff($FirstArray, $SecondArray).
Well, I could have done a foreach FirstArrayValue, foreach SecondArrayValue, if FirstArrayValue = SecondArrayValue delete FirstArrayValue from array. Since it would only be done in the plugin setup, performance wouldn't be a concern. In contrast to a simple array_diff($FirstArray, $SecondArray) that foreachloopifthen spaghetti is a monster.
But if there is no built in way there is none. That would be an answer that is perfectly okay for me.
what is in first array? Could it be included in the query?
grep is your friend.
I want to prefill the Tag table. So if the plugin has been installed before or some of those 80 tags already exist, they do not have to be inserted, but otherwise every single array item should be inserted into the tag table.
There are some nice functions in
functions.general.php
.You might be interested in
array_column()
andConsolidateArrayValuesByKey()
.Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Sure
ConsolidateArrayValuesByKey
would workgrep is your friend.
array_column didn't work on my home server, so I haven't looked further into that, but ConsolidateArrayValuesByKey is exactly what I was missing!
Why no just use
INSERT IGNORE
syntax?grep is your friend.
$Sql->Select(Column)->Options('Ignore', TRUE)...
grep is your friend.
Mannomann, I thought I knew SQL but I've never seen insert ignore before - many thanks for that!!!
I also haven't seen the Options() function anywhere before and I will take a deeper look at that.
I'm not sure if it will work because I insert Name and 'InsertUserID' => '1', 'DateInserted' => $Now, 'CategoryID' => '-1' and it might be that the tag name already exists but it surely hasn't been inserted $Now and maybe was inserted by someone else. But I am happy to test that with my new toy "insert ignore"
Ok I think you best option is go with what you got. There are pure SQL methods but there isn't really much of an advantage of using them in this case.
grep is your friend.