SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2009
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    print array ...?

    Hi guys i have a problem when i want to "print_r" my variable string that i've "explode". the detail is below..

    $var = "1,2,3,4,5";
    $sat = explode(',',$var);

    echo"<pre>";
    print_r($sat);
    echo"</pre>";

    i want to have result like this...

    Array
    (
    [0] => Array
    (
    [new] => 1
    )
    [1] => Array
    (
    [new] => 2
    )
    [2] => Array
    (
    [new] => 3
    )
    [4] => Array
    (
    [new] => 4
    )
    ...
    )

    but when i'm tried my script, it was not same like above. what is wrong with my script and what should i do guys. Please help me!

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You will never get the expected result you want above as your array isn't multidimensional, in order for you to achieve the above result you would need to construct the array with the "new" keys themselves.

    PHP Code:
    $var  '1,2,3,4,5';
    $sat explode(','$var);

    foreach (
    $sat as $k => $v) {
        
    $sat[$k] = array('new' => $v);
    }

    echo 
    '<pre>';
    print_r($sat);
    echo 
    '</pre>'


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •