Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

"Flatten" ResultArray() data

R_JR_J Ex-FanboyMunich Admin

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

  • Options
    hgtonighthgtonight ∞ · New Moderator
    Answer ✓

    There are some nice functions in functions.general.php.

    You might be interested in array_column() and ConsolidateArrayValuesByKey().

    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.

Answers

  • Options

    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.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    To explain why I felt the need for some function like that, here's some code:

    public function setup() {
          $FirstArray = array(with more or less 80 values);
    
          $Sql = Gdn::SQL();
          $SecondArray = $Sql->Select(Column)
             ->From(Table)
             ->Get()
             ->ResultArray();
    

    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.

  • Options

    what is in first array? Could it be included in the query?

    grep is your friend.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    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.

  • Options
    hgtonighthgtonight ∞ · New Moderator
    Answer ✓

    There are some nice functions in functions.general.php.

    You might be interested in array_column() and ConsolidateArrayValuesByKey().

    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.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    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! :)

  • Options

    Why no just use INSERT IGNORE syntax?

    grep is your friend.

  • Options

    $Sql->Select(Column)->Options('Ignore', TRUE)...

    grep is your friend.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    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" B)

  • Options

    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.

Sign In or Register to comment.