Is there a more efficent way to doing this?

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

    Is there a more efficent way to doing this?

    I need to make an associated array (all with the same key name) using values
    from another array. Is there a more efficient way to doing this in one pass
    (instead of looping)?

    I'm always looking to learn something new. :-)

    --------------------------------------------------------------------

    $fields = array('username ', 'passwd', 'firstname', 'lastname', 'email');

    foreach ($fields as $field_entry)
    {
    $list[] = array( 'field_name' => $field_entry);
    }



  • Bosconian

    #2
    Re: Is there a more efficent way to doing this?

    "Bosconian" <bosconian@plan etx.com> wrote in message
    news:upKdnS0V9o 8WoenfRVn-rA@comcast.com. ..[color=blue]
    > I need to make an associated array (all with the same key name) using[/color]
    values[color=blue]
    > from another array. Is there a more efficient way to doing this in one[/color]
    pass[color=blue]
    > (instead of looping)?
    >
    > I'm always looking to learn something new. :-)
    >
    > --------------------------------------------------------------------
    >
    > $fields = array('username ', 'passwd', 'firstname', 'lastname', 'email');
    >
    > foreach ($fields as $field_entry)
    > {
    > $list[] = array( 'field_name' => $field_entry);
    > }
    >
    >
    >[/color]

    I should add that the array $fields is actually a list of items returned
    from a object.


    Comment

    • Ken Robinson

      #3
      Re: Is there a more efficent way to doing this?


      Bosconian wrote:[color=blue]
      > "Bosconian" <bosconian@plan etx.com> wrote in message
      > news:upKdnS0V9o 8WoenfRVn-rA@comcast.com. ..[color=green]
      > > I need to make an associated array (all with the same key name)[/color][/color]
      using[color=blue]
      > values[color=green]
      > > from another array. Is there a more efficient way to doing this in[/color][/color]
      one[color=blue]
      > pass[color=green]
      > > (instead of looping)?
      > >
      > > I'm always looking to learn something new. :-)
      > >
      > >[/color][/color]
      --------------------------------------------------------------------[color=blue][color=green]
      > >
      > > $fields = array('username ', 'passwd', 'firstname', 'lastname',[/color][/color]
      'email');[color=blue][color=green]
      > >
      > > foreach ($fields as $field_entry)
      > > {
      > > $list[] = array( 'field_name' => $field_entry);
      > > }
      > >
      > >
      > >[/color]
      >
      > I should add that the array $fields is actually a list of items[/color]
      returned[color=blue]
      > from a object.[/color]

      What, exactly, are you trying to do. The above code does not give you
      what you said you wanted. Did you try it?

      Does this give you what you want:
      <?
      $fields = array('username ', 'passwd', 'firstname', 'lastname',
      'email');
      foreach ($fields as $field_entry)
      $list['field_name'][] = $field_entry;
      ?>

      Ken

      Comment

      • Kenneth Downs

        #4
        Re: Is there a more efficent way to doing this?

        Bosconian wrote:
        [color=blue]
        > I need to make an associated array (all with the same key name) using
        > values from another array. Is there a more efficient way to doing this in
        > one pass (instead of looping)?
        >
        > I'm always looking to learn something new. :-)
        >
        > --------------------------------------------------------------------
        >
        > $fields = array('username ', 'passwd', 'firstname', 'lastname', 'email');
        >
        > foreach ($fields as $field_entry)
        > {
        > $list[] = array( 'field_name' => $field_entry);
        > }[/color]

        There are a variety of interesting array functions, like array_merge, and
        array_combine. Neither does quite what you are doing here, but they may
        give you ideas.

        But what are you trying to do here? Your original array is arguably much
        easier to use than the new one you create, which buries the column names in
        an inaccessible way w/o giving any advantage. What are you gaining by
        this?

        --
        Kenneth Downs
        Secure Data Software, Inc.
        (Ken)nneth@(Sec )ure(Dat)a(.com )

        Comment

        • Bosconian

          #5
          Re: Is there a more efficent way to doing this?

          "Kenneth Downs" <knode.wants.th is@see.sigblock > wrote in message
          news:ut1fk2-liv.ln1@pluto.d ownsfam.net...[color=blue]
          > Bosconian wrote:
          >[color=green]
          > > I need to make an associated array (all with the same key name) using
          > > values from another array. Is there a more efficient way to doing this[/color][/color]
          in[color=blue][color=green]
          > > one pass (instead of looping)?
          > >
          > > I'm always looking to learn something new. :-)
          > >
          > > --------------------------------------------------------------------
          > >
          > > $fields = array('username ', 'passwd', 'firstname', 'lastname', 'email');
          > >
          > > foreach ($fields as $field_entry)
          > > {
          > > $list[] = array( 'field_name' => $field_entry);
          > > }[/color]
          >
          > There are a variety of interesting array functions, like array_merge, and
          > array_combine. Neither does quite what you are doing here, but they may
          > give you ideas.
          >
          > But what are you trying to do here? Your original array is arguably much
          > easier to use than the new one you create, which buries the column names[/color]
          in[color=blue]
          > an inaccessible way w/o giving any advantage. What are you gaining by
          > this?
          >
          > --
          > Kenneth Downs
          > Secure Data Software, Inc.
          > (Ken)nneth@(Sec )ure(Dat)a(.com )[/color]

          The first array is a list of field names returned by a class function. The
          second array associates the same key name for output to the template page
          (looping however many times the number of elements.)

          I was simply wondering if there was a more efficient way of populating the
          associated array with the contents of the first array. There won't ever be
          more than a couple dozen items in the first array so using the foreach loop
          is not expensive.


          Comment

          • coolsti

            #6
            Re: Is there a more efficent way to doing this?

            On Sat, 30 Apr 2005 19:18:50 -0700, Bosconian wrote:
            [color=blue]
            > I need to make an associated array (all with the same key name) using values
            > from another array. Is there a more efficient way to doing this in one pass
            > (instead of looping)?
            >
            > I'm always looking to learn something new. :-)
            >
            > --------------------------------------------------------------------
            >
            > $fields = array('username ', 'passwd', 'firstname', 'lastname', 'email');
            >
            > foreach ($fields as $field_entry)
            > {
            > $list[] = array( 'field_name' => $field_entry);
            > }[/color]

            Reading the various responses, I think what you want is this:

            $fields = array('username ', 'passwd', 'firstname', 'lastname', 'email');
            $list = array('field_na me' => $fields);

            The last line can be split into two, in case the $list array already
            exists and you want to add a new key (so don't do the first line of the
            next two below):

            $list = array();
            $list['field_name'] = $fields;

            This makes the $list array have an element with key 'field_name' which
            itself is an array. Then to use this in your code:

            foreach ($list['field_name'] as $field) {
            // do something with $field
            }

            If $list contains various arrays, you can loop over all of them like:

            foreach ($list as $key => $fieldarray) {
            foreach ($fieldarray as $field) {
            // do something with $field and $key
            }
            }

            - steve

            Comment

            Working...