Why does this foreach loop fail to work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    Why does this foreach loop fail to work?

    [PHP]
    $array = array($firstOrd erByFieldIfClau se, $groupByClause,
    $orderByClause) ;
    array_walk($arr ay, create_function ('&$a', '$a = substr($a, 0,
    strrpos(trim($a ), ",")); if ($a) $a = ", $a";'));
    print_r("firstO rderByFieldIfCl ause = $firstOrderByFi eldIfClause and
    groupByClause = $groupByClause and orderByClause = $orderByClause< P>");
    [/PHP]

    Upon execution the fields within $array are never changed (verified by
    checking code), and I don't understand why.

    firstOrderByFie ldIfClause = IF(image_creati on_date IS NULL OR
    image_creation_ date = '' OR image_creation_ date LIKE '0000%', 'x',
    NULL) AS image_creation_ date_alt, and groupByClause =
    image_creation_ date_alt, and orderByClause = image_creation_ date_alt
    desc,
    Please review and help, I'm utterly lost!

    Thanx
    Phil

  • Tim Roberts

    #2
    Re: Why does this foreach loop fail to work?

    "comp.lang. php" <phillip.s.powe ll@gmail.comwro te:
    >
    >[PHP]
    > $array = array($firstOrd erByFieldIfClau se, $groupByClause,
    >$orderByClause );
    > array_walk($arr ay, create_function ('&$a', '$a = substr($a, 0,
    >strrpos(trim($ a), ",")); if ($a) $a = ", $a";'));
    > print_r("firstO rderByFieldIfCl ause = $firstOrderByFi eldIfClause and
    >groupByClaus e = $groupByClause and orderByClause = $orderByClause< P>");
    >[/PHP]
    >
    >Upon execution the fields within $array are never changed (verified by
    >checking code), and I don't understand why.
    It certainly does. Try "var_dump($arra y);", and you'll see that all three
    strings were changed.

    Your mistake is in thinking that changes to $array(0) will also affect
    $firstOrderByFi eldIfClause.

    The array constructor makes three new strings.
    --
    - Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.

    Comment

    • comp.lang.php

      #3
      Re: Why does this foreach loop fail to work?


      Tim Roberts wrote:
      "comp.lang. php" <phillip.s.powe ll@gmail.comwro te:

      [PHP]
      $array = array($firstOrd erByFieldIfClau se, $groupByClause,
      $orderByClause) ;
      array_walk($arr ay, create_function ('&$a', '$a = substr($a, 0,
      strrpos(trim($a ), ",")); if ($a) $a = ", $a";'));
      print_r("firstO rderByFieldIfCl ause = $firstOrderByFi eldIfClause and
      groupByClause = $groupByClause and orderByClause = $orderByClause< P>");
      [/PHP]

      Upon execution the fields within $array are never changed (verified by
      checking code), and I don't understand why.
      >
      It certainly does. Try "var_dump($arra y);", and you'll see that all three
      strings were changed.
      >
      Your mistake is in thinking that changes to $array(0) will also affect
      $firstOrderByFi eldIfClause.
      >
      The array constructor makes three new strings.
      I should have figured that all complex objects that take parameters
      make clones of the parameters unless passed by reference. So that's
      what I did:

      $array = array(&$firstOr derByFieldIfCla use, &$groupByClause ,
      &$orderByClause );

      That did the trick, thanx!
      Phil
      --
      - Tim Roberts, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      Working...