Jump to content

Spllitting array and changing keys.


Zugzwangle

Recommended Posts

hello. i have the following array called $newParts which cycles through game data:

[0] => Control 90m + 5s, rated    <--start of game 1
[1] => Site Room 1     
[2] => Date 2010.08.03 
[3] => Round ? 
[4] => WhiteIs D r A G ó N 
[5] => BlackIs Spike_and_Butch 
[6] => Result 0-1      <--end of game 1
[7] => Control 90m + 5s, rated    <--start of game 2
[8] => Site Room 1     
[9] => Date 2010.08.03 
[10] => Round ? 
[11] => WhiteIs Öschi 
[12] => BlackIs Shreki 
[13] => Result 1/2-1/2     <--end of game 2
--->   continues with next game data...

 

The games all start with "Control " and end with "Result "

Firstly, I would like to split the array into seperate game arrays.  How would I go about doing this?

 

Secondly, I would like to use the first word, of the values in the new arrays, as the Key.  So the arrays would become:

[site] => Room 1 
[Date] => 2010.08.03 
[Round] => ? 
[WhiteIs] => D r A G ó N 
[blackIs] => Spike_and_Butch 
[Result] => 0-1 

Any help would be greatly appreciated.

Thanks

Link to comment
Share on other sites

Try this script.

 

<?php 

$game_data = array(
'Control 90m + 5s, rated',
'Site Room 1',
'Date 2010.08.03 ',
'Round ?',
'WhiteIs D r A G ó N',
'BlackIs Spike_and_Butch',
'Result 0-1',
'Control 90m + 5s, rated',
'Site Room 1',
'Date 2010.08.03',
'Round ?',
'WhiteIs Öschi',
'BlackIs Shreki',
'Result 1/2-1/2'
);

$new_array = array();
$game_data_count = count($game_data);

if($game_data_count)
{
for($i = 7; $i <= $game_data_count; $i += 7)
{
	$new_array[] = array(
		'control' => $game_data[$i-7],
		'site' => $game_data[$i-6],
		'date' => $game_data[$i-5],
		'round' => $game_data[$i-4],
		'whiteis' => $game_data[$i-3],
		'blackis' => $game_data[$i-2],
		'result' => $game_data[$i-1]
		);
}
}

print_r($new_array);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.