Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    213
    Thanks
    15
    Thanked 5 Times in 5 Posts

    explode a string into 2 arrays

    I'm wondering how i can change a string to an array using a character between.
    Something like this:
    PHP Code:
    $string "tree=papaer|plastic=bottle";

    foreach(
    explode('|',$string) as $variabe){
    //Splits it to "tree=paper" and "plastic=bottle" in $variable

    //How do i change $variable to '$source => $outcome'?

    So in the example it would split each part of the string separated with pipes into another string with a looping function (to do for each one). Then it processes that and splits it again with another special character (an equal sign in this case) and splits it into either an array or two separate strings.

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    PHP Code:
    <?php


    $string 
    "tree=papaer|plastic=bottle|metal=wire";



    $piped explode("|"$string);

    foreach(
    $piped as $equals)
    {
        
    $a[] = explode("="$equals);
        foreach(
    $a as $b)
        {
            
    $data[$b[0]] = $b[1];
        }
    }



    print_r($data);


    ?>
    PHP Code:
    Array
    (
        [
    tree] => papaer
        
    [plastic] => bottle
        
    [metal] => wire


  • Users who have thanked Adee for this post:

    Dubz (11-21-2011)

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    213
    Thanks
    15
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Adee View Post
    PHP Code:
    <?php


    $string 
    "tree=papaer|plastic=bottle|metal=wire";



    $piped explode("|"$string);

    foreach(
    $piped as $equals)
    {
        
    $a[] = explode("="$equals);
        foreach(
    $a as $b)
        {
            
    $data[$b[0]] = $b[1];
        }
    }



    print_r($data);


    ?>
    PHP Code:
    Array
    (
        [
    tree] => papaer
        
    [plastic] => bottle
        
    [metal] => wire

    Yeah I just figured it out but I think I like your way better than mine so I might use it. Thanks!

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Eh, that solution is overly complex.

    It is almost a query string, so simply convert it to a query string and parse it with parse_str():
    PHP Code:
    $string "tree=papaer|plastic=bottle|metal=wire";

    $string str_replace'|''&'$string );

    parse_str$string$data );

    print_r$data ); 
    Last edited by kbluhm; 11-22-2011 at 01:07 AM.


  •  

    Posting Permissions

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