PHP array help needed , Many many thanks if some one can show me the solution

Discussion in 'PHP' started by webmaster365, Apr 9, 2011.

  1. #1
    Hello All,

    Please help me in below problem.

    I have an string like below:

    $str="'apple','orange','mango'";

    I want to create above string into an array
    $ary=array($str);

    When I run the code it's output like below.

    print_r($ary);

    Array
    (
    [0] => 'apple','orange','mango'
    )

    But I want the output like below so i can loop my array.

    Array
    (
    [0] => apple
    [1] => oraange
    [2] => mango
    )


    Please help me on this , I have tried lot's of things but not worked for me.

    Thanks in advance .
     
    webmaster365, Apr 9, 2011 IP
  2. Sepehr

    Sepehr Peon

    Messages:
    568
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:

    
    <?php
    
    $str="'apple','orange','mango'";
    $ary=explode("','", substr($str, 1, -1));
    print_r($ary);
    
    ?>
    
    PHP:
     
    Sepehr, Apr 9, 2011 IP
  3. Envision

    Envision Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this...

    
    <?php
    
    $str="'apple','orange','mango'";
    
    //removes the tick marks from string
    $str = str_replace("'", "", $str);
    
    //splits string into array
    $myArry = explode(",", $str);
    
    print_r($myArry);
    
    ?>
    
    Code (markup):
    Hope this helps!
     
    Last edited: Apr 9, 2011
    Envision, Apr 9, 2011 IP
  4. webmaster365

    webmaster365 Greenhorn

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    Thanks a lot for your great Logic , many many thanks , you have saved me. :)
     
    webmaster365, Apr 9, 2011 IP