Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting an array into multiple arrays 2

Status
Not open for further replies.

kaptlid

Technical User
Nov 26, 2006
86
US
I have an array that I need to split into multiple arrays inside one big array based on the first letter of a value in the array.

The current array looks like:

array (
[0] => 4/02,
[1] => 4/08,
[2] => 4/12,
[3] => 5/07,
[4] => 6/03,
[5] => 6/17 )

I need it to look like:

array (
array ( [0] => 4/02,
[1] => 4/08,
[2] => 4/12 )
array ( [0] => 5/07 )
array ( [0] => 6/03,
[1] => 6/07 )
)

Help is appreciated. Regards,
 
There are several ways to tackle this, all require you to cycle through the array.

Code:
for($i=0;$i<=count($myarray)-1;$i++){
if($myarray[$i][0]==4){[green]//checks the first character of the current position in the array[/green]
$array4[]=$myarray[$i][$counter1]; [green]\\insert it into ne array[/green[
$counter1++;
}
if($myarray[$i][0]==6){
$array6[]=$myarray[$i][$counter2];
$counter2++;
}
.
.
.

}

Then just put the arrays into another array.
$outerarray[0]=$4array

Of course this requires to know the possible numbers that can exist in the first character of each value.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
so I have to make a counter for each number (month)?
 
Basically you need to cycle through your array, the counter is juts to give it a position in the new array. Looking back at my code you don't really need the counter as you only want the first position of the value. so no need.

But you will have to check for the numbers it can either be IF's or a switch.

switch ($myarray[$i][0]){
case 1: .... break;
case 2: .... break;

}



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I was hoping to avoid 12 if statements, thank you for your help, because the way I am doing it right now uses 12 if statements.
 
how about this
Code:
<?
$input = array (
		"4/02",
		"4/08",
		"4/12",
		"5/07",
		"6/03",
		"6/17" );

foreach($input as $bday){
	$d = explode("/", $bday);
	$array[$d[0]][] = $bday;
}

echo "<pre>";
print_r($array);
echo "</pre>";
?>
 
Thanks I skinned the cat, saved about 50 lines of code. I put each month's rows into its own array inside of the games array, games[1], games[2] etc.. Then just ran one for loop to process all the arrays inside $games.

I was trying to seperate each month's games into a seperate div box based on the first or first and second character of the month string.

Thanks for everyone's help. :-D

Might as well show it here:

Code:
$query = "select gamenum, date, hteam, ateam from schedule where hteam = '$_GET[name]' or ateam = '$_GET[name]'"; $runquery = mysql_query($query); while ($row = mysql_fetch_assoc($runquery)) { 

if (substr($row[date],1,1) == "1") { $month1[] = $row; $games[1] = $month1; } 
if (substr($row[date],1,1) == "2") { $month2[] = $row; $games[2] = $month2; } 
if (substr($row[date],1,1) == "3") { $month3[] = $row; $games[3] = $month3; } 
if (substr($row[date],1,1) == "4") { $month4[] = $row; $games[4] = $month4; } 
if (substr($row[date],1,1) == "5") { $month5[] = $row; $games[5] = $month5; } 
if (substr($row[date],1,1) == "6") { $month6[] = $row; $games[6] = $month6; } 
if (substr($row[date],1,1) == "7") { $month7[] = $row; $games[7] = $month7; } 
if (substr($row[date],1,1) == "8") { $month8[] = $row; $games[8] = $month8; } 
if (substr($row[date],1,1) == "9") { $month9[] = $row; $games[9] = $month9; } 
if (substr($row[date],0,-3) == "10") { $month10[] = $row; $games[10] = $month10; }
if (substr($row[date],0,-3) == "11") { $month11[] = $row; $games[11] = $month11; }
if (substr($row[date],0,-3) == "12") { $month12[] = $row; $games[12] = $month12; }

} 

for ($i = 1; $i <= 12; $i++) { 

$url = $_SERVER[PHP_SELF].'?name='.$_GET[name].'&amp;gameaction='.$_GET[gameaction].'&amp;game=';
if ($games[$i] == $null) { echo $null; } else {
echo '<div style="float:left; border: 1px solid; margin:5px; padding:5px">';
foreach ($games[$i] as $value) { echo substr($value[date],1).' <a href="'.$url.$value[gamenum].'">'.ucfirst($value[hteam]).' vs. '.ucfirst($value[ateam]).'</a><br>'; } echo '</div>'; }
} // for bracket
 
here is a shorter code snip doing what you have been doing. it makes use of variable variables.

as a point of style, you should explicitly enquote the keys associative arrays (e.g. in the for loop).

Code:
<?
$query = "select gamenum, date, hteam, ateam from schedule where hteam = '$_GET[name]' or ateam = '$_GET[name]'"; 
$runquery = mysql_query($query); 
while ($row = mysql_fetch_assoc($runquery)) {
	$date = explode($row['date']);
	$month{$date[0]}[] = $row; 
}


for ($i = 1; $i <= 12; $i++) {
	$url = $_SERVER['PHP_SELF'].'?name='.$_GET['name'].'&amp;gameaction='.$_GET['gameaction'].'&amp;game=';
	if (!isset($month{$i}) { 
		echo $null; 
	} else {
		echo '<div style="float:left; border: 1px solid; margin:5px; padding:5px">';

		foreach ($month{$i} as $value) { 
			echo substr($value['date'],1).' <a href="'.$url.$value['gamenum'].'">'.ucfirst($value['hteam']).' vs. '.ucfirst($value['ateam']).'</a><br>'; } echo '</div>'; 
		}
	} //if bracket
} // for bracket
?>
 
I fixed jpadie's code a bit, the displaying of the months above september needed some additional code to display properly. Thank you everyone for your help.

Code:
$query = "select gamenum, date, hteam, ateam from schedule where hteam = '$_GET[name]' or ateam = '$_GET[name]'";
$runquery = mysql_query($query); echo mysql_error();
while ($row = mysql_fetch_assoc($runquery)) {
  
//the if statements accomodate for the months above 9 (sept)
   if (($row[date]{0} == 1) and (($row[date]{1} == 0) or ($row[date]{1} == 1) or ($row[date]{1} == 2))) {$date = explode(" ", substr($row['date'],0,-3)); } else {
   $date = explode(" ", substr($row['date'],1,1)); } 
    $month{$date[0]}[] = $row;
    
}

$url = $_SERVER['PHP_SELF'].'?name='.$_GET['name'].'&amp;gameaction='.$_GET['gameaction'].'&amp;game=';
$div = '<div style="float:left; border: 1px solid; margin:5px; padding:5px">';

for ($i = 1; $i <= 12; $i++) { if (!isset($month{$i})) { echo $null; } else 
    
{ echo $div;
foreach ($month{$i} as $value)  {
if ($i > 9) { echo substr($value['date'],0); } else {echo substr($value['date'],1); } //accomodates the months after sep

echo' <a href="'.$url.$value['gamenum'].'">'.ucfirst($value['hteam']).' vs. '.ucfirst($value['ateam']).'</a><br>'; } //foreach bracket
echo '</div>';
    
    } //if bracket
} // for bracket
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top