foreach - Array

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

    #1

    foreach - Array

    hi

    i want to modify an array in foreach loop

    foreach($array as $ele){
    $ele = $some_random_st ring;
    }

    i should see here the modified $array .

    thnaks in advance

  • David Haynes

    #2
    Re: foreach - Array

    iavian wrote:[color=blue]
    > hi
    >
    > i want to modify an array in foreach loop
    >
    > foreach($array as $ele){
    > $ele = $some_random_st ring;
    > }
    >
    > i should see here the modified $array .
    >
    > thnaks in advance
    >[/color]
    foreach( $array as &$ele ) {
    $ele = $some_random_st ring;
    }

    should work as you want it to.

    -david-

    Comment

    • Adam Plocher

      #3
      Re: foreach - Array

      foreach ($array as $key=>$val)
      {
      $arrayOut[$key] = $some_random_st ring;
      }

      $array = $arrayOut;

      that should work (not tested).

      Comment

      • Alan Little

        #4
        Re: foreach - Array

        Carved in mystic runes upon the very living rock, the last words of David
        Haynes of comp.lang.php make plain:
        [color=blue]
        > iavian wrote:[color=green]
        >> hi
        >>
        >> i want to modify an array in foreach loop
        >>
        >> foreach($array as $ele){
        >> $ele = $some_random_st ring;
        >> }
        >>
        >> i should see here the modified $array .
        >>
        >> thnaks in advance
        >>[/color]
        > foreach( $array as &$ele ) {
        > $ele = $some_random_st ring;
        > }
        >
        > should work as you want it to.[/color]

        Only in PHP5.



        "Unless the array is referenced, foreach operates on a copy of the
        specified array and not the array itself....As of PHP 5, you can easily
        modify array's elements by preceding $value with &. This will assign
        reference instead of copying the value."

        --
        Alan Little
        Phorm PHP Form Processor

        Comment

        • Marcin Dobrucki

          #5
          Re: foreach - Array

          iavian wrote:
          [color=blue]
          > i want to modify an array in foreach loop
          >
          > foreach($array as $ele){
          > $ele = $some_random_st ring;
          > }
          >
          > i should see here the modified $array .[/color]

          Alternatively, you could use the array_walk() or
          array_walk_recu rssive() functions, which will give you some more
          flexibility.

          Comment

          • Colin McKinnon

            #6
            Re: foreach - Array

            Adam Plocher wrote:
            [color=blue]
            > foreach ($array as $key=>$val)
            > {
            > $arrayOut[$key] = $some_random_st ring;
            > }
            >
            > $array = $arrayOut;
            >
            > that should work (not tested).[/color]

            but:

            foreach ($array as $key => $val) {
            $array[$key] = $some_random_st ring;
            }

            is cleaner.

            C.

            Comment

            • R. Rajesh Jeba Anbiah

              #7
              Re: foreach - Array

              Colin McKinnon wrote:
              <snip>[color=blue]
              > foreach ($array as $key => $val) {
              > $array[$key] = $some_random_st ring;
              > }
              >
              > is cleaner.[/color]

              When you have to modify the array element, use of foreach is
              discouraged because of overhead
              <http://www.phparch.com/cert/ZPCPB_sample.pd f>

              --
              <?php echo 'Just another PHP saint'; ?>
              Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

              Comment

              Working...