Jump to content

[SOLVED] Having problem adding value to associative array


Recommended Posts

OK, this code is a bit complicated ... 

 

<?php
// Do this if user has NOT selected BOTH the start and end date
if ($_POST['start_date'] == "" || $_POST['end_date'] == "") {
echo "You somehow managed to not enter a date.  I didn't think it was possible!  Congrats, now enter some dates.";
}
// Do this if user has selected BOTH a start and end date
else {
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
// Get deadline, and create a number with it via array
$start_temp = str_split($start_date);
$end_temp = str_split($end_date);
$start_clean = array();
$end_clean = array();
foreach($start_temp as $value){
	if(is_numeric($value)){
		$start_clean [] = $value;
	}
}
foreach($end_temp as $value){
	if(is_numeric($value)){
		$end_clean [] = $value;
	}
}
$start_date = implode($start_clean);
$end_date = implode($end_clean);

// Start date is AFTER the end date
if ($start_date - $end_date >= 0) {
	echo "You must select a start date that comes BEFORE an end date.";
}
// Start date is before end date
else {
	$databaseConnect = mysqli_connect("localhost", "username", "pass", "db")
				Or die("Unable to connect to the database.");

	$query = "SELECT * FROM bonanza_weeks";
	$result = mysqli_query($databaseConnect, $query);
	$row = mysqli_fetch_assoc($result);

	$weekID_array = array();

	// Get all week_id's within range
	while ($row) {
		// Alter Deadline to fit correct format
		$weekend_start = $row['weekend_start'];
		$weekend_temp = str_split($weekend_start);
		$weekend_clean = array();
		foreach($weekend_temp as $value){
			if(is_numeric($value)){
				$weekend_clean [] = $value;
			}
		}
		$weekend_start = implode($weekend_clean);

		// Date is inside range
		if ($weekend_start - $start_date >= 0 && $weekend_start - $end_date <= 0) {
			$weekID_array[] = $row['week_id'];
		}

		$row = mysqli_fetch_assoc($result);
	}
	mysqli_free_result($result);

	$query = "SELECT user_id FROM bonanza_rankings";
	$result = mysqli_query($databaseConnect, $query);
	$row = mysqli_fetch_assoc($result);

	$userID_array = array();

	// Get all user_id's who have played at least 1 week
	while ($row) {
		$userID_array[] = $row['user_id'];
		$row = mysqli_fetch_assoc($result);
	}
	mysqli_free_result($result);

	$average_array = array();
	$holdover_average_array = array();
	$opener_average_array = array();
	$weeks_predicted_array = array();
	// Go through each user_id and get scores
	foreach ($userID_array as $current_user_id) {
		$average = 0;
		$holdover_average = 0;
		$opener_average = 0;
		$weeks_predicted = 0;
		$total_weeks = 0;
		// This will go through each week id
		foreach ($weekID_array as $week_id) {
			$query = "SELECT * FROM bonanza_actuals WHERE week_id = $week_id";
			$result = mysqli_query($databaseConnect, $query);
			// Week in range hasn't finished
			if (mysqli_num_rows($result) == 0) {
				// Do nothing
				mysqli_free_result($result);
			}
			// Week has had scores calculated
			else {
				mysqli_free_result($result);

				$query = "SELECT * FROM bonanza_predictions WHERE user_id = $current_user_id AND week_id = $week_id";
				$result = mysqli_query($databaseConnect, $query);
				// If the user has no prediction for that week
				if (mysqli_num_rows($result) == 0) {
					$total_weeks++;
				}
				// User has a prediction
				else {
					$row = mysqli_fetch_assoc($result);

					if ($row['score'] == NULL) {
						// Do nothing
					}
					else {
						$average += $row['score'];
					}

					if ($row['holdover_score'] == NULL) {
						// Do nothing
					}
					else {
						$holdover_average += $row['holdover_score'];
					}

					if ($row['opener_score'] == NULL) {
						// Do nothing
					}
					else {
						$opener_average += $row['opener_score'];
					}
					$weeks_predicted++;
					$total_weeks++;
				}

				mysqli_free_result($result);
			}
		}
		$average = $average / $total_weeks;
		$average = round($average, 3);
		$holdover_average = $holdover_average / $total_weeks;
		$holdover_average = round($holdover_average, 3);
		$opener_average = $opener_average / $total_weeks;
		$opener_average = round($opener_average, 3);

		$average_array["$current_user_id"] = $average;
		$holdover_average_array["$current_user_id"] = $holdover_average;
		$opener_average_array["$current_user_id"] = $opener_average;
		$weeks_predicted_array["$current_user_id"] = $weeks_predicted;
		print_r($average_array);
	}
	mysqli_close($databaseConnect);
}
}
?>

 

The basic jist of it is that I am trying to create an array with $user_id => $average, etc.  But the output is of the print_r statement above is:

 

 Array ( [2] => 58.038 ) Array ( [2] => 58.038 [11] => 55.99 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 [615] => 28.295 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 [615] => 28.295 [2517] => 55.378 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 [615] => 28.295 [2517] => 55.378 [2809] => 25.267 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 [615] => 28.295 [2517] => 55.378 [2809] => 25.267 [2830] => 23.905 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 [615] => 28.295 [2517] => 55.378 [2809] => 25.267 [2830] => 23.905 [3200] => 84.528 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 [615] => 28.295 [2517] => 55.378 [2809] => 25.267 [2830] => 23.905 [3200] => 84.528 [3323] => 25.932 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 [615] => 28.295 [2517] => 55.378 [2809] => 25.267 [2830] => 23.905 [3200] => 84.528 [3323] => 25.932 [4150] => 74.702 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 [615] => 28.295 [2517] => 55.378 [2809] => 25.267 [2830] => 23.905 [3200] => 84.528 [3323] => 25.932 [4150] => 74.702 [4156] => 83.031 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 [615] => 28.295 [2517] => 55.378 [2809] => 25.267 [2830] => 23.905 [3200] => 84.528 [3323] => 25.932 [4150] => 74.702 [4156] => 83.031 [4317] => 24.836 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 [615] => 28.295 [2517] => 55.378 [2809] => 25.267 [2830] => 23.905 [3200] => 84.528 [3323] => 25.932 [4150] => 74.702 [4156] => 83.031 [4317] => 24.836 [6035] => 80.994 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 [615] => 28.295 [2517] => 55.378 [2809] => 25.267 [2830] => 23.905 [3200] => 84.528 [3323] => 25.932 [4150] => 74.702 [4156] => 83.031 [4317] => 24.836 [6035] => 80.994 [6131] => 24.307 ) Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 [35] => 80.444 [49] => 26.707 [66] => 84.334 [79] => 53.419 [89] => 51.245 [103] => 48.34 [105] => 28.55 [110] => 26.69 [113] => 26.672 [126] => 29.363 [133] => 28.553 [149] => 27.837 [166] => 27.486 [182] => 23.992 [187] => 27.946 [189] => 26.17 [275] => 80.967 [280] => 51.776 [476] => 23.373 [509] => 50.015 [539] => 48.316 [578] => 28.933 [607] => 28.346 [609] => 25.953 [615] => 28.295 [2517] => 55.378 [2809] => 25.267 [2830] => 23.905 [3200] => 84.528 [3323] => 25.932 [4150] => 74.702 [4156] => 83.031 [4317] => 24.836 [6035] => 80.994 [6131] => 24.307 [9574] => 27.391 )

 

So as you can see, it is printing an array within an array, within an array, etc.  I can't figure it out.  Each user ID should only show up once, but this code is nesting arrays within arrays. Help?

No, you do not have a nested array. from the looks of it, your results are what you want. The problem is that you are using print_r() on each loop.

 

So, on the first itteration of the loop you have:

Array ( [2] => 58.038 )

 

On the second  itteration of the loop you get the first item and the second item:

Array ( [2] => 58.038 [11] => 55.99 )

 

On the 3rd itteration you get the first 2 items and the 3rd item:

Array ( [2] => 58.038 [11] => 55.99 [33] => 84.26 )

 

And so on...

 

So, just move your print_r() so it comes after the foreach loop and you should see that the array is exactly how you want it to be.

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.