Help With Arrays, Please?

Discussion in 'PHP' started by ColorWP.com, May 16, 2010.

  1. #1
    Hello.

    I have this function:
    foreach($cast as $member){
    	$member=explode("...",$member,2); // so member now contains $member[0] and $member[1];
    	// for example, right now $member[0] contains actor name like Arnold Scwarzeneger and $member[1] contains the name of the characters he player (e.g. The Terminator)
    }
    PHP:
    Note: In case it makes a difference, the $cast variable originally looks like this:
    array(
    "Arnold Scharzeneger...The Terminator",
    "Daniel Radcliffe...Harry Potter",
    // etc etc...
    );
    PHP:
    How do I insert the values of $member[0] and $member[1] to a new array so in the end it I have an array that looks like this:
    array(
    "Arnold Schwarzeneger"->"The Terminator",
    "Daniel Radcliffe"->"Harry Potter",
    // etc, etc...
    );
    PHP:
    Essentially, I want to start with a string that is in this format: "Name...Character (double space here) Name...Character (double space here) Name...Character" and end with an array with Name as array key, and Character as array value. To do this I first explode() using the double space as a separator, then for each "exploded" item I explode() again, but using "..." as a separator. But I don't know where to go from here.

    Regards!
    Rep will be given.
     
    Last edited: May 16, 2010
    ColorWP.com, May 16, 2010 IP
  2. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #2
    
    $array = array();
    foreach($cast as $member){
        $member=explode("...",$member,2); // so member now contains $member[0] and $member[1];
        $array[$member[0]] = $member[1];
    }
    print_r($array);
    
    PHP:
     
    crazyryan, May 16, 2010 IP
  3. lorkan

    lorkan Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the print_r($array); is just for you to see the array afterwards, since it prints it out in the format you asked for...
     
    lorkan, May 16, 2010 IP
  4. phpSiteMinder

    phpSiteMinder Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Or you could so something like this

    
    $array = array();
    foreach($cast as $member){
        $member=explode("...",$member,2); // so member now contains $member[0] and $member[1];
        $array[] = array('actor'=>$member[0], 'movie' => $member[1]);
    }
    print_r($array);
    
    PHP:
    which would give you something like
    
    array(
     [0] -> array('actor'->"Arnold Schwarzeneger",
                        'movie'->"The Terminator")
     [1] -> array('actor'->"Arnold Schwarzeneger",
                        'movie'->"Total Recall")
     [3] -> array('actor->'"Daniel Radcliffe",
                       'movie'->"Harry Potter")
    // etc, etc...
    );
    
    PHP:
    Which would allow for actors to be in more than one movie.

    You could get the records as such:

    
    foreach($array as $record){
        echo $record['actor'] ." stars in ". $record['movie'] . "<br/>";
    }
    
    PHP:
     
    phpSiteMinder, May 16, 2010 IP