1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

is this valid construction?

Discussion in 'PHP & Perl' started by Mutikasa, Jan 30, 2012.

  1. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    574
    Likes Received:
    207
    PHP:
    foreach($many as $one
    name[] = (($number[] = thisFun()) => thatFun()); 
    so i can create two arrays at the same time
     
  2. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,884
    Likes Received:
    1,932
    name should have a $ at the beginning. You should also declare $name before using $name[] as this can lead to issues with certain settings.
     
  3. BlueZero

    BlueZero Power Member

    Joined:
    Jul 6, 2011
    Messages:
    500
    Likes Received:
    257
    Occupation:
    Webdeveloper, Project Manager
    Location:
    Byte in the Net
    Home Page:
    I dont think that it's valid.
    But why dont you try it?
     
  4. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    574
    Likes Received:
    207
    I did and It's not.
    But I was thinking more conceptual, is such thing possible?
    In the same time assign array and key in array using functions.
     
  5. inviz

    inviz Newbie

    Joined:
    Jun 15, 2010
    Messages:
    45
    Likes Received:
    5
    Could you try to be a bit more specific? The code you posted in the first thread doesnt tell me anything. What are you trying to do there?
     
  6. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    574
    Likes Received:
    207
    yes
    thisFun and thatFun are functions which return value.
    $name[] and $number[] are arrays.
    I want to use foreach loop
    and in every loop want to assign value to arrays in that way that $number[] is the array of keys for the $name[] array and at the same time they get their values from thisFun for $number[] and thatFun for $name[].

    The long code will be
    PHP:
    $i=0;
    foreach(
    $many as $one) {
    $number[$i] = thisFun();
    $name[$number[$i]] = thatFun();
    $i++; }
     
  7. inviz

    inviz Newbie

    Joined:
    Jun 15, 2010
    Messages:
    45
    Likes Received:
    5
    Gotcha! you could do this using the the php implode() function, or the alias join().

    Check it out on php.net.
     
  8. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    574
    Likes Received:
    207
    how? implide() returns string, i need array
     
  9. inviz

    inviz Newbie

    Joined:
    Jun 15, 2010
    Messages:
    45
    Likes Received:
    5
    Ah, sorry..

    Copied from php.net

    Code:
    Example #1 array_merge() PHP 5 example
    <?php
    $beginning = 'foo';
    $end = array(1 => 'bar');
    $result = array_merge((array)$beginning, (array)$end);
    print_r($result);
    ?>