create array from members of an array of objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Generale Cluster

    create array from members of an array of objects

    Hello,
    I have the following situation:

    $list[] is an array of MyElement objects.

    MyElement has two members: MyElement->member1; MyElement->member2;

    What I want is to get the following:

    $newlist[] so that:

    $newlist[0]=$list[0]->member2;
    $newlist[1]=$list[1]->member2;
    $newlist[2]=$list[2]->member2;
    ....

    I need to do this using A SINGLE LINE OF CODE.
    Is it possible? How to do this?

    I tried with:

    array_walk($lis t,create_functi on('$a,$b,$resu lt','$result[] =
    $a->member2;'), &$result);

    but I get the following error:

    Warning: Call-time pass-by-reference has been deprecated - argument
    passed by value; If you would like to pass it by reference, modify the
    declaration of array_walk(). If you would like to enable call-time
    pass-by-reference, you can set allow_call_time _pass_reference to true in
    your INI file. However, future versions may not support this any longer.

    Thank you
    Regards
  • Rik

    #2
    Re: create array from members of an array of objects

    Generale Cluster <alex@carraroso ftmasters.netwr ote:
    Hello,
    I have the following situation:
    >
    $list[] is an array of MyElement objects.
    >
    MyElement has two members: MyElement->member1; MyElement->member2;
    >
    What I want is to get the following:
    >
    $newlist[] so that:
    >
    $newlist[0]=$list[0]->member2;
    $newlist[1]=$list[1]->member2;
    $newlist[2]=$list[2]->member2;
    ...
    >
    I need to do this using A SINGLE LINE OF CODE.
    Is it possible? How to do this?
    Nothing _needs_ to be done in a single line of code, and for some actions
    you shouldn't even want it for readability.
    I tried with:
    >
    array_walk($lis t,create_functi on('$a,$b,$resu lt','$result[] =
    $a->member2;'), &$result);
    Tssk, single line, but a create_function ()... That's cheating :P

    $newlist = array_map(creat e_function('$v' ,'return $v->member2'),$lis t);

    --
    Rik Wasmus

    Comment

    • Jerry Stuckle

      #3
      Re: create array from members of an array of objects

      Generale Cluster wrote:
      Hello,
      I have the following situation:
      >
      $list[] is an array of MyElement objects.
      >
      MyElement has two members: MyElement->member1; MyElement->member2;
      >
      What I want is to get the following:
      >
      $newlist[] so that:
      >
      $newlist[0]=$list[0]->member2;
      $newlist[1]=$list[1]->member2;
      $newlist[2]=$list[2]->member2;
      ...
      >
      I need to do this using A SINGLE LINE OF CODE.
      Is it possible? How to do this?
      >
      I tried with:
      >
      array_walk($lis t,create_functi on('$a,$b,$resu lt','$result[] =
      $a->member2;'), &$result);
      >
      but I get the following error:
      >
      Warning: Call-time pass-by-reference has been deprecated - argument
      passed by value; If you would like to pass it by reference, modify the
      declaration of array_walk(). If you would like to enable call-time
      pass-by-reference, you can set allow_call_time _pass_reference to true in
      your INI file. However, future versions may not support this any longer.
      >
      Thank you
      Regards

      Try:

      array_walk($lis t,create_functi on('$a,$b,&$res ult','$result[] =
      $a->member2;'), $result);

      Specify the reference in the parameter list, not when passing the value.
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Jerry Stuckle

        #4
        Re: create array from members of an array of objects

        Rik wrote:
        Generale Cluster <alex@carraroso ftmasters.netwr ote:
        >
        >Hello,
        >I have the following situation:
        >>
        >$list[] is an array of MyElement objects.
        >>
        >MyElement has two members: MyElement->member1; MyElement->member2;
        >>
        >What I want is to get the following:
        >>
        >$newlist[] so that:
        >>
        >$newlist[0]=$list[0]->member2;
        >$newlist[1]=$list[1]->member2;
        >$newlist[2]=$list[2]->member2;
        >...
        >>
        >I need to do this using A SINGLE LINE OF CODE.
        >Is it possible? How to do this?
        >
        Nothing _needs_ to be done in a single line of code, and for some
        actions you shouldn't even want it for readability.
        >
        >I tried with:
        >>
        >array_walk($li st,create_funct ion('$a,$b,$res ult','$result[] =
        >$a->member2;'), &$result);
        >
        Tssk, single line, but a create_function ()... That's cheating :P
        >
        $newlist = array_map(creat e_function('$v' ,'return $v->member2'),$lis t);
        >
        --Rik Wasmus
        Rik,

        Sounds like a homework assignment to me... :-)


        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Kimmo Laine

          #5
          Re: create array from members of an array of objects

          "Generale Cluster" <alex@carraroso ftmasters.netwr ote in message
          news:eqd099$uh6 $1@newsreader.m ailgate.org...
          Hello,
          I have the following situation:
          >
          $list[] is an array of MyElement objects.
          >
          MyElement has two members: MyElement->member1; MyElement->member2;
          >
          What I want is to get the following:
          >
          $newlist[] so that:
          >
          $newlist[0]=$list[0]->member2;
          $newlist[1]=$list[1]->member2;
          $newlist[2]=$list[2]->member2;
          ...
          >
          I need to do this using A SINGLE LINE OF CODE.
          foreach($list as $foo) $newlist[] = $foo->member2;
          I tried with:
          >
          array_walk($lis t,create_functi on('$a,$b,$resu lt','$result[] =
          $a->member2;'), &$result);
          "Gloves".
          http://thedailywtf.com/Articles/The_Complicator 's_Gloves.aspx

          --
          "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
          http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
          spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


          Comment

          • Generale Cluster

            #6
            Re: create array from members of an array of objects

            Jerry Stuckle ha scritto:
            Rik wrote:
            >Generale Cluster <alex@carraroso ftmasters.netwr ote:
            >>
            >>Hello,
            >>I have the following situation:
            >>>
            >>$list[] is an array of MyElement objects.
            >>>
            >>MyElement has two members: MyElement->member1; MyElement->member2;
            >>>
            >>What I want is to get the following:
            >>>
            >>$newlist[] so that:
            >>>
            >>$newlist[0]=$list[0]->member2;
            >>$newlist[1]=$list[1]->member2;
            >>$newlist[2]=$list[2]->member2;
            >>...
            >>>
            >>I need to do this using A SINGLE LINE OF CODE.
            >>Is it possible? How to do this?
            >>
            >Nothing _needs_ to be done in a single line of code, and for some
            >actions you shouldn't even want it for readability.
            >>
            >>I tried with:
            >>>
            >>array_walk($l ist,create_func tion('$a,$b,$re sult','$result[] =
            >>$a->member2;'), &$result);
            >>
            >Tssk, single line, but a create_function ()... That's cheating :P
            >>
            >$newlist = array_map(creat e_function('$v' ,'return $v->member2'),$lis t);
            >>
            >--Rik Wasmus
            >
            Rik,
            >
            Sounds like a homework assignment to me... :-)
            >
            >
            Hi, thanks to Rik for the answer; it's exactly what I intended.
            It's not a homework assignment, I simply want to write code which is
            easy to integrate with HTML. I'm working together with another person
            who is not a programmer: he's an HTML author and I need to make the code
            structure as essential as possible. I've coded my own generic tag
            library which generates html code from data and in this particular case
            I have a function which build a "select multiple" HTML tag.
            So, in my html page I have the following line:

            <?php
            fillHTMLSelectM ultiple($aValue s,"author[]","",array_map( create_function ('$a','return
            $a->author_id;'),$ resource->aAuthors),'siz e="5"')?>

            - $aValues is an array containing the key-value couples to be displayed
            in the select.
            - "author[]" is the name and id of the HTML tag
            - array_map(...) contains an array the keys which have to appear
            preselected in the select control.
            - $resource is an object which is loaded by a self-made CRUD handler and
            contains a member variable which is an array of "MyElement" objects
            (where member2 is the authorId).

            This way, my friend, who does not know php, is able to move the select
            anywhere on the page by simply moving the php code line, and I do not
            risk to have my code corrupted by a mistake by my friend.
            I know I could build my array of selected values previously in my code,
            but I got the question in my mind and I physiologically NEEDED to have
            an answer :-)

            I don't know if this may be called "extreme programming" ?
            I'd rather call it "academic fury" :-D

            bye!














            Comment

            • Jerry Stuckle

              #7
              Re: create array from members of an array of objects

              Generale Cluster wrote:
              Jerry Stuckle ha scritto:
              >Rik wrote:
              >>Generale Cluster <alex@carraroso ftmasters.netwr ote:
              >>>
              >>>Hello,
              >>>I have the following situation:
              >>>>
              >>>$list[] is an array of MyElement objects.
              >>>>
              >>>MyElement has two members: MyElement->member1; MyElement->member2;
              >>>>
              >>>What I want is to get the following:
              >>>>
              >>>$newlist[] so that:
              >>>>
              >>>$newlist[0]=$list[0]->member2;
              >>>$newlist[1]=$list[1]->member2;
              >>>$newlist[2]=$list[2]->member2;
              >>>...
              >>>>
              >>>I need to do this using A SINGLE LINE OF CODE.
              >>>Is it possible? How to do this?
              >>>
              >>Nothing _needs_ to be done in a single line of code, and for some
              >>actions you shouldn't even want it for readability.
              >>>
              >>>I tried with:
              >>>>
              >>>array_walk($ list,create_fun ction('$a,$b,$r esult','$result[] =
              >>>$a->member2;'), &$result);
              >>>
              >>Tssk, single line, but a create_function ()... That's cheating :P
              >>>
              >>$newlist = array_map(creat e_function('$v' ,'return $v->member2'),$lis t);
              >>>
              >>--Rik Wasmus
              >>
              >Rik,
              >>
              >Sounds like a homework assignment to me... :-)
              >>
              >>
              >
              Hi, thanks to Rik for the answer; it's exactly what I intended.
              It's not a homework assignment, I simply want to write code which is
              easy to integrate with HTML. I'm working together with another person
              who is not a programmer: he's an HTML author and I need to make the code
              structure as essential as possible. I've coded my own generic tag
              library which generates html code from data and in this particular case
              I have a function which build a "select multiple" HTML tag.
              So, in my html page I have the following line:
              >
              <?php
              fillHTMLSelectM ultiple($aValue s,"author[]","",array_map( create_function ('$a','return
              $a->author_id;'),$ resource->aAuthors),'siz e="5"')?>
              >
              - $aValues is an array containing the key-value couples to be displayed
              in the select.
              - "author[]" is the name and id of the HTML tag
              - array_map(...) contains an array the keys which have to appear
              preselected in the select control.
              - $resource is an object which is loaded by a self-made CRUD handler and
              contains a member variable which is an array of "MyElement" objects
              (where member2 is the authorId).
              >
              This way, my friend, who does not know php, is able to move the select
              anywhere on the page by simply moving the php code line, and I do not
              risk to have my code corrupted by a mistake by my friend.
              I know I could build my array of selected values previously in my code,
              but I got the question in my mind and I physiologically NEEDED to have
              an answer :-)
              >
              I don't know if this may be called "extreme programming" ?
              I'd rather call it "academic fury" :-D
              >
              bye!
              >
              >
              That's the difference. I prefer code which is easily understandable.

              You had trouble getting it to work in the first place. What happens
              when you have to look at it six months or a year from now when you have
              to work on it again? You'll go through all of his again to understand
              what it does.

              If you want it to be a single line, make it a function call. But even
              if it's multiple lines he should be able to copy and paste.

              I have a similar setup for one of my customers. They maintain the
              content, I maintain the (interspaced) code. They even build entirely
              new pages by copying/pasting sections of html and code from other pages.

              Sure, once in a while they have a problem - but not often. But they're
              saving a bunch of money over having to pay me to make the frequent
              content changes.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Generale Cluster

                #8
                Re: create array from members of an array of objects

                Jerry Stuckle ha scritto:
                Generale Cluster wrote:
                >Jerry Stuckle ha scritto:
                >>Rik wrote:
                >>>Generale Cluster <alex@carraroso ftmasters.netwr ote:
                [CUT]
                >
                That's the difference. I prefer code which is easily understandable.
                >
                You had trouble getting it to work in the first place. What happens
                when you have to look at it six months or a year from now when you have
                to work on it again? You'll go through all of his again to understand
                what it does.
                [CUT]

                comment, comment, comment and comment again :-)

                bye!

















                Comment

                • Rik

                  #9
                  Re: create array from members of an array of objects

                  On Wed, 14 Feb 2007 02:12:32 +0100, Generale Cluster
                  <alex@carraroso ftmasters.netwr ote:
                  Jerry Stuckle ha scritto:
                  >Generale Cluster wrote:
                  >>Jerry Stuckle ha scritto:
                  >>>Rik wrote:
                  >>>>Generale Cluster <alex@carraroso ftmasters.netwr ote:
                  [CUT]
                  > That's the difference. I prefer code which is easily understandable. .
                  > You had trouble getting it to work in the first place. What happens
                  >when you have to look at it six months or a year from now when you have
                  >to work on it again? You'll go through all of his again to understand
                  >what it does.
                  [CUT]
                  >
                  comment, comment, comment and comment again :-)
                  Commenting in the single line?
                  Just tell them to leave everything between <?php ?alone and it should
                  be OK, no reason to forced single line statements. About that, if you're
                  worried about that, then why not just code it on the single line on normal
                  code? Linebreaks aren't actually required in PHP, so you could just leave
                  them in, and when the code needs to be updated you can whip it into some
                  kind of formatted readable shape quite fast with any competent texteditor.

                  But the main reason for me posting here in this thread is about
                  commenting: I'd like to take the oppertunity advocate the user of the /x
                  modifier for regexes. These pesky little things are often forgotten in the
                  commenting, while it's so easy.

                  Compare (not a very useful regex, just an illustration):

                  $regex = '/(a|b){2,4}([^\s]+?)c{2,4}(.*?)q/si'

                  And
                  $regex = '/{a|b}{2,4} #start matching at 2 to 4 a's or b's in a row
                  ([^\s]+?) #capture all non whitespace untill the next match
                  c{2,4} #match 2 to 4 c's
                  (.*?) #capture any character
                  q #until q
                  /six'; //case-insensite, with comments

                  Now, that will be a lot easier altered and understood by coworkers I'd
                  imagine..
                  --
                  Rik Wasmus

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: create array from members of an array of objects

                    Generale Cluster wrote:
                    Jerry Stuckle ha scritto:
                    >Generale Cluster wrote:
                    >>Jerry Stuckle ha scritto:
                    >>>Rik wrote:
                    >>>>Generale Cluster <alex@carraroso ftmasters.netwr ote:
                    [CUT]
                    >>
                    >That's the difference. I prefer code which is easily understandable.
                    >>
                    >You had trouble getting it to work in the first place. What happens
                    >when you have to look at it six months or a year from now when you
                    >have to work on it again? You'll go through all of his again to
                    >understand what it does.
                    [CUT]
                    >
                    comment, comment, comment and comment again :-)
                    >
                    bye!
                    >
                    Yes, I agree code should be commented. But it still should be easy to
                    understand.


                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Jerry Stuckle

                      #11
                      Re: create array from members of an array of objects

                      Rik wrote:
                      On Wed, 14 Feb 2007 02:12:32 +0100, Generale Cluster
                      <alex@carraroso ftmasters.netwr ote:
                      >
                      >Jerry Stuckle ha scritto:
                      >>Generale Cluster wrote:
                      >>>Jerry Stuckle ha scritto:
                      >>>>Rik wrote:
                      >>>>>Generale Cluster <alex@carraroso ftmasters.netwr ote:
                      >[CUT]
                      >> That's the difference. I prefer code which is easily understandable.
                      >> You had trouble getting it to work in the first place. What happens
                      >>when you have to look at it six months or a year from now when you
                      >>have to work on it again? You'll go through all of his again to
                      >>understand what it does.
                      >[CUT]
                      >>
                      >comment, comment, comment and comment again :-)
                      >
                      Commenting in the single line?
                      Just tell them to leave everything between <?php ?alone and it should
                      be OK, no reason to forced single line statements. About that, if you're
                      worried about that, then why not just code it on the single line on
                      normal code? Linebreaks aren't actually required in PHP, so you could
                      just leave them in, and when the code needs to be updated you can whip
                      it into some kind of formatted readable shape quite fast with any
                      competent texteditor.
                      >
                      But the main reason for me posting here in this thread is about
                      commenting: I'd like to take the oppertunity advocate the user of the /x
                      modifier for regexes. These pesky little things are often forgotten in
                      the commenting, while it's so easy.
                      >
                      Compare (not a very useful regex, just an illustration):
                      >
                      $regex = '/(a|b){2,4}([^\s]+?)c{2,4}(.*?)q/si'
                      >
                      And
                      $regex = '/{a|b}{2,4} #start matching at 2 to 4 a's or b's in a row
                      ([^\s]+?) #capture all non whitespace untill the next match
                      c{2,4} #match 2 to 4 c's
                      (.*?) #capture any character
                      q #until q
                      /six'; //case-insensite, with comments
                      >
                      Now, that will be a lot easier altered and understood by coworkers I'd
                      imagine..
                      --Rik Wasmus
                      Rik,

                      I agree - this makes a lot more sense, especially to those of us who are
                      regex-challenged. :-) I really do wish more people would comment their
                      regex's. It sometimes takes me a while to decode just what they're
                      trying to do.

                      --
                      =============== ===
                      Remove the "x" from my email address
                      Jerry Stuckle
                      JDS Computer Training Corp.
                      jstucklex@attgl obal.net
                      =============== ===

                      Comment

                      Working...