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
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts

    Explode String Twice into an Associative Array

    I have a string that is formatted like this:

    Code:
    Tom&Riddle?Jerry&Springer?Kobe&Bryant... and so on.
    What I'm trying to do is get it to look like this:

    PHP Code:
    $array = array(Tom => RiddleJerry => SpringerKobe => Bryant); 
    I figured out how to explode the string from "?" with:

    PHP Code:
    $divide explode("?""Tom&Riddle?Jerry&Springer?Kobe&Bryant"); 
    but then I can't use explode again, since it only works with strings. I also found a foreach loop from google but I ended up getting separate arrays like:

    PHP Code:
    foreach($array as $key=>$value) { $divide[$key] = strpos($value"&") ? explode"&"$value ) : $value
    Outputs:
    PHP Code:
    array("Tom","Riddle");
    array(
    "Jerry","Springer");
    array(
    "Kobe","Braynt"); 
    Maybe there is some way to turn ^ into an associative array?

    I am completely lost?
    Last edited by RonnyNishimoto; 08-16-2012 at 09:18 PM.

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by RonnyNishimoto View Post
    But then I can't use explode again, since it only works with strings.



    PHP Code:
    <?php
    $results 
    explode("?""Tom&Riddle?Jerry&Springer?Kobe&Bryant");  

    foreach (
    $results as $value){
    $results explode("&"$value); 

    foreach (
    $results as $value){
    echo 
    "$value<br>";
    }
    }


    ?>

    Output
    Code:
    Tom
    Riddle
    Jerry
    Springer
    Kobe
    Bryant

    -------
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    RonnyNishimoto (08-15-2012)

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Here's a method to get the values into an array, as you'd originally asked... as opposed to simply printing them to the client:
    PHP Code:
    $str 'Tom&Riddle?Jerry&Springer?Kobe&Bryant';

    $arr = array();

    foreach ( 
    explode'?'$str ) as $val )
    {
        
    // we're assuming each piece will always have a `&` separating non-empty values
        
    $val explode'&'$val );
        
    $arr$val[0] ] = $val[1];
    }

    // What'd we get?
    print_r$arr ); 

  • Users who have thanked kbluhm for this post:

    RonnyNishimoto (08-15-2012)

  • #4
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    Kbluhm, perfect answer, I'm terrible with foreach statements so I tried using workarounds, but your answer works just how I wanted! Thanked and reputation added!


  •  

    Posting Permissions

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