Just some lines of php



  • I just ran into this code written by an ex-employee of the company I currently work for:

    foreach ($this->result as $content){
       $listId[ ] = $content['id'];
       $listName[ ] = $content['label'];
    }
    return array_combine($listId,$listName);

     

    mod: fixed code format -dh

    [ Fixed hideous typo in title. -TheShadowMod ]



  • Re: Juste some lines of php

     So what's wrong with this... it works just as intended. Just uses some more memory.



  • Re: Juste some lines of php

    Learning basic PHP: free

    Learning one PHP array function: free

    Learning how to use foreach: free

    Not bothering to examine the rest of the array API: priceless!

    For serious code, there's money. For everything else, use PHP.



  • @mariushm said:

     So what's wrong with this... it works just as intended. Just uses some more memory.

    It works as intended, but I just can't imagine why anyone would do it that way.

    more "a bit funny" than "real wtf".



  • I would probably do it that way if I'm not sure the id is UNIQUE (primary key or auto increment or whatever in the database) and I want to do some processing on the results before the array is returned

    Then again... values are overwritten resulting in an array with unique ids each with the last value in the array for that id...  I don't know, seems pointless but either way doesn't seem to do any harm.

     



  • @Rhaban said:

    It works as intended, but I just can't imagine why anyone would do it that way.

    more "a bit funny" than "real wtf".

    How about because "$this->result" contains a metric (as opposed to imperial) crapload of irrelevant stuff that the author decided that they didn't want to carry around in further processing?



  • @OzPeter said:

    @Rhaban said:
    It works as intended, but I just can't imagine why anyone would do it that way.

    more "a bit funny" than "real wtf".

    How about because "$this->result" contains a metric (as opposed to imperial) crapload of irrelevant stuff that the author decided that they didn't want to carry around in further processing?
     

    Then what is wrong with:

    $r = array();
    foreach ($this->result as $content){
       $r[$content['id']] = $content['label']; // Hopefully CS doesn't screw with these square brackets too much
    }
    return $r;

    If $this->result is too much stuff then it sounds like it came from a SELECT * FROM table...



  • @Zemm said:

    @OzPeter said:

    @Rhaban said:
    It works as intended, but I just can't imagine why anyone would do it that way.

    more "a bit funny" than "real wtf".

    How about because "$this->result" contains a metric (as opposed to imperial) crapload of irrelevant stuff that the author decided that they didn't want to carry around in further processing?
     

    Then what is wrong with:

    $r = array();
    foreach ($this->result as $content){
       $r[$content['id']] = $content['label']; // Hopefully CS doesn't screw with these square brackets too much
    }
    return $r;

    If $this->result is too much stuff then it sounds like it came from a SELECT * FROM table...

    I originally thought php would have an array_ function to deal with this kind of thing, but can find none apart from perhaps array_filter and a dedicated callback, so the code you present would be simpler.



  • @Zemm said:

    If $this->result is too much stuff then it sounds like it came from a SELECT * FROM table...

    Well, it might be a larger query and in that bit, the author didn't want the extra data, and judged that doing this waltz would be better that hitting the db again.



  • @Adriano said:

    For serious code, there's money. For everything else, use PHP.

    +1



  • @Zemm said:

    If $this->result is too much stuff then it sounds like it came from a SELECT * FROM table...
    Totally agree, but it may be that the original author can't control the query that returns the data (ie stored procedure), so this may be a pragmatic approach. But yes, I would consider this to be optimisation at the wrong level if that was the case.



  • Re: Juste some lines of php

     Was $result[$content['id']] = $content['label']; too simple?



  • @morbiuswilters said:

     Was $result[$content['id']] = $content['label']; too simple?

    Obviously, that violates the programming law that you should never nest square brackets. Don't you know ANYTHING about programming??



  • @toth said:

    @morbiuswilters said:

     Was $result[$content['id']] = $content['label']; too simple?

    Obviously, that violates the programming law that you should never nest square brackets.


    It does violate the one about not posting a comment which was already posted by Zemm five posts earlier.



  • @Adriano said:

    @toth said:
    @morbiuswilters said:

     Was $result[$content['id']] = $content['label']; too simple?

    Obviously, that violates the programming law that you should never nest square brackets.


    It does violate the one about not posting a comment which was already posted by Zemm five posts earlier.

    He's Australian.  I couldn't understand his accent.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.