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.
$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:
the print_r($array); is just for you to see the array afterwards, since it prints it out in the format you asked for...
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: