SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast iwiz_seo's Avatar
    Join Date
    Sep 2009
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to convert a variable to string than to an array

    Hi all,

    Could anybody tell me how to convert a variable to a string then convert string to an array.

    Thank you all.

  2. #2
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use strval to convert a variable to a string.

    How do you mean convert a string to an array? Do you mean convert "hello" into array('h', 'e', 'l', 'l', 'o')?

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Not entirely sure what you mean, but how about...
    PHP Code:
    <?php
    $string 
    'Hello World';
    $array = array();

    for(
    $pos 0$pos strlen($string); $pos++)
    {
        
    array_push($array$string[$pos]);
    }

    print_r(
        
    $array
    );
    /*
        Array
        (
            [0] => H
            [1] => e
            [2] => l
            [3] => l
            [4] => o
            [5] =>  
            [6] => W
            [7] => o
            [8] => r
            [9] => l
            [10] => d
        )
    */
    ?>
    ?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or use str_split()
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Argh! I was sure there was a function, I guess I didn't look hard enough...

    Thanks Mark!

    PHP Code:
    <?php
    print_r
    (
        
    str_split(
            
    'Hello World'
        
    )
    );
    /*
        Array
        (
            [0] => H
            [1] => e
            [2] => l
            [3] => l
            [4] => o
            [5] =>  
            [6] => W
            [7] => o
            [8] => r
            [9] => l
            [10] => d
        )
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •