need to assign/edit values in multidimensional associative arrays

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

    need to assign/edit values in multidimensional associative arrays

    I am finding that altering and affecting values in elements in
    multidimensiona l arrays is a huge pain in the ass. I cannot seem to find a
    consistent way to assign values to arrays. Foreach would clearly be the most
    efficient way to do it, but it only works on a copy of the original array
    and not the original (which is counter intuitive in my estimation). Using
    each doesn't work consistently either. Not only that, it's unduly complex
    for multidimensiona l arrays that are more than 3 dimensions. For example,
    here's an array $data:

    0 => Array (8)
    id => 8
    name => test entry3 3 2fsdfsdfs sdf sdf
    address => empty
    city => empty
    state => 66
    closingdate => empty
    loanamount => 0
    date => empty
    1 => Array (8)
    id => 9
    name => test entry 2
    address => empty
    city => empty
    state => 66
    closingdate => empty
    loanamount => 0
    date => empty
    2 => Array (8)
    id => 10
    name => empty
    address => empty
    city => empty
    state => 66
    closingdate => empty
    loanamount => 0
    date => empty

    I would like to iterate through this array and add an element to each called
    url which will be assigned based on a function of id. In pseudocode:

    foreach ($data as $array) {
    $array['url'] = SomeCoolFunctio n("$array['id']");
    }

    How can I do this? Assigning values to elements and working with existing
    arrays in Perl is extremely easy. This appears to be a weakness in the
    design of PHP. Thoughts?

    TIA

    GN


  • Pedro Graca

    #2
    Re: need to assign/edit values in multidimensiona l associative arrays

    Golf Nut wrote:
    (snip)[color=blue]
    > I would like to iterate through this array and add an element to each called
    > url which will be assigned based on a function of id. In pseudocode:
    >
    > foreach ($data as $array) {
    > $array['url'] = SomeCoolFunctio n("$array['id']");
    > }
    >
    > How can I do this? Assigning values to elements and working with existing
    > arrays in Perl is extremely easy. This appears to be a weakness in the
    > design of PHP. Thoughts?[/color]

    Try

    foreach ($data as $k=>$array) {
    $data[$k]['url'] = SomeCoolFunctio n($array['id']);
    }



    Or, if you don't like it (I don't), use the array_walk() function
    Apply a user supplied function to every member of an array



    function AnotherCoolFunc tion(&$element, $key) {
    $element['url'] = SomeCoolFunctio n($element['id']);
    }
    array_walk($dat a, 'AnotherCoolFun ction');


    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Andy Hassall

      #3
      Re: need to assign/edit values in multidimensiona l associative arrays

      On Thu, 27 May 2004 19:55:49 GMT, "Golf Nut"
      <trippsathyperc oncom-golfnut@yahoo.c om> wrote:
      [color=blue]
      >I am finding that altering and affecting values in elements in
      >multidimension al arrays is a huge pain in the ass. I cannot seem to find a
      >consistent way to assign values to arrays. Foreach would clearly be the most
      >efficient way to do it, but it only works on a copy of the original array
      >and not the original (which is counter intuitive in my estimation).[/color]

      Fair point there.
      [color=blue]
      >For example, here's an array $data:
      >
      >0 => Array (8)
      > id => 8[/color]
      [snip]
      [color=blue]
      >I would like to iterate through this array and add an element to each called
      >url which will be assigned based on a function of id. In pseudocode:
      >
      >foreach ($data as $array) {
      > $array['url'] = SomeCoolFunctio n("$array['id']");
      >}
      >
      >How can I do this? Assigning values to elements and working with existing
      >arrays in Perl is extremely easy. This appears to be a weakness in the
      >design of PHP. Thoughts?[/color]

      Depends how you look at it - possibly it's more that Perl has some outstanding
      syntax in this area; processing data structures can be done very concisely with
      things like map and hash slices that don't have equivalents in many other
      languages.

      Since you can't get a reference to the original array element out of a plain
      PHP foreach loop, one idiom is to get the key and reference the array again in
      the loop.

      foreach ($data as $key => $array) {
      $data[$key]['url'] = SomeCoolFunctio n($array['id']);
      }

      If you still want something Perl-ish there's always array_map or array_walk
      but anonymous sub syntax in PHP isn't as neat as Perl either. Something like:

      array_walk($dat a,
      create_function ('&$a', '$a["url"] = SomeCoolFunctio n($a["id"]);'));

      Yuk :-(

      --
      Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
      http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

      Comment

      • Golf Nut

        #4
        Re: need to assign/edit values in multidimensiona l associative arrays

        Thanks so much both of you who responded - I could have sworn that I tried
        this particular idiom, but apparently not!

        A thought for php is perhaps to modify it so that if you're performing an
        assignment on an array within a foreach construct (or any construct, for
        that matter), that it performs it on the original. I don't know if this
        would add any overhead to the code; the compiler would only do this in case
        of assignments, so normal foreach use and other iterations wouldn't impact
        performance at all. Thoughts?

        Thanks again!

        GN

        "Pedro Graca" <hexkid@hotpop. com> wrote in message
        news:2hn0caFeqr brU1@uni-berlin.de...[color=blue]
        > Golf Nut wrote:
        > (snip)[color=green]
        > > I would like to iterate through this array and add an element to each[/color][/color]
        called[color=blue][color=green]
        > > url which will be assigned based on a function of id. In pseudocode:
        > >
        > > foreach ($data as $array) {
        > > $array['url'] = SomeCoolFunctio n("$array['id']");
        > > }
        > >
        > > How can I do this? Assigning values to elements and working with[/color][/color]
        existing[color=blue][color=green]
        > > arrays in Perl is extremely easy. This appears to be a weakness in the
        > > design of PHP. Thoughts?[/color]
        >
        > Try
        >
        > foreach ($data as $k=>$array) {
        > $data[$k]['url'] = SomeCoolFunctio n($array['id']);
        > }
        >
        >
        >
        > Or, if you don't like it (I don't), use the array_walk() function
        > http://www.php.net/array_walk
        >
        >
        > function AnotherCoolFunc tion(&$element, $key) {
        > $element['url'] = SomeCoolFunctio n($element['id']);
        > }
        > array_walk($dat a, 'AnotherCoolFun ction');
        >
        >
        > --
        > USENET would be a better place if everybody read: : mail address :
        > http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
        > http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
        > http://www.expita.com/nomime.html : to 10K bytes :[/color]


        Comment

        • Andy Hassall

          #5
          Re: need to assign/edit values in multidimensiona l associative arrays

          On Thu, 27 May 2004 21:09:04 GMT, "Golf Nut"
          <trippsathyperc oncom-golfnut@yahoo.c om> wrote:
          [color=blue]
          >A thought for php is perhaps to modify it so that if you're performing an
          >assignment on an array within a foreach construct (or any construct, for
          >that matter), that it performs it on the original. I don't know if this
          >would add any overhead to the code; the compiler would only do this in case
          >of assignments, so normal foreach use and other iterations wouldn't impact
          >performance at all. Thoughts?[/color]

          Or perhaps as:

          foreach ($array as $value) // work on a copy

          foreach ($array as &$value) // reference to the actual value

          Avoids breaking backwards compatibility that way.

          --
          Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
          http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

          Comment

          • Philipp Kern

            #6
            Re: need to assign/edit values in multidimensiona l associative arrays

            On 2004-05-27, Andy Hassall <andy@andyh.co. uk> wrote:[color=blue]
            > foreach ($array as &$value) // reference to the actual value[/color]

            I would be in favour of this one. But adding the key to the foreach construct
            and refer to the original array by the key isn't that hard either, it's just
            a bit uncommon.
            AFAIK in PHP5 all variables are references, so I hope this will be automagically
            implemented there and the comments at php.net for `foreach' seem to prove this.

            Bye,
            phil
            --
            Please send replys (not followups) to the address set in Reply-To.
            Philipp Kern - PK2186-RIPE - http://www.philkern.de

            Comment

            Working...