I am trying to organize a set of results that obtained through a database query. Here is the code I'm using to do this:
$time_slots = $wpdb->get_results($query);
print_r($time_slots);
echo ("<br/><br/><br/><br/>");
/*organize slots into array*/
$openings = array();
foreach($time_slots as $ts)
{
if(empty($openings))
{
echo("Empty Array: ");
echo ("<br/>");
echo ("<br/>");
echo("Inserting: ");
print_r($ts);
echo ("<br/>");
echo ("<br/>");
$openings[$ts->route_date] = $ts;
echo("contents of Opening: ");
echo ("<br/>");
echo ("<br/>");
print_r($openings);
echo ("<br/><br/><br/><br/>");
}
elseif (array_key_exists($ts->route_date, $openings))
{
echo("Same Day");
echo ("<br/>");
echo ("<br/>");
echo("Inserting: ");
print_r($ts);
echo ("<br/>");
echo ("<br/>");
array_push($openings[$ts->route_date][$ts->name], $ts);
echo("contents of Opening: ");
echo ("<br/>");
echo ("<br/>");
print_r($openings);
echo ("<br/><br/><br/><br/>");
}
else
{
echo("New Day : ");
echo ("<br/>");
echo ("<br/>");
echo("Inserting: ");
print_r($ts);
echo ("<br/>");
echo ("<br/>");
$openings[$ts->route_date] = $ts;
echo("contents of Opening: ");
echo ("<br/>");
echo ("<br/>");
print_r($openings);
echo ("<br/><br/><br/><br/>");
}
}
/*return results*/
$result['openings'] = $openings;
$result['time'] = $time_slots;
$result['begin'] = $begin;
$result['end'] = $end;
$result['query'] = $query;
$result['type'] = "success";
$result = json_encode($result);
print_r($openings);
Here is what a single result looks like when I print_r the $ts:
stdClass Object ( [route_date] => 2014-01-10 [name] => 2 [openings] => 1 [appointments] => 0 )
Here is what one loop looks like. You will notice that the logic works, and that everything is going where it should go, but the adding t:
> Empty Array:
Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) )
Same Day
Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 1 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) )
Same Day
Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 2 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) )
Same Day
Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 3 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) )
Same Day
Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 4 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) )
New Day :
Inserting: stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-11] => stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 ) )
Same Day
Inserting: stdClass Object ( [route_date] => 2014-01-11 [name] => 1 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-11] => stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 ) )
When it is all said and done, I get this result:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-11] => stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-12] => stdClass Object ( [route_date] => 2014-01-12 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-13] => stdClass Object ( [route_date] => 2014-01-13 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-14] => stdClass Object ( [route_date] => 2014-01-14 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-15] => stdClass Object ( [route_date] => 2014-01-15 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-16] => stdClass Object ( [route_date] => 2014-01-16 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-17] => stdClass Object ( [route_date] => 2014-01-17 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-18] => stdClass Object ( [route_date] => 2014-01-18 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-19] => stdClass Object ( [route_date] => 2014-01-19 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-20] => stdClass Object ( [route_date] => 2014-01-20 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-21] => stdClass Object ( [route_date] => 2014-01-21 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-22] => stdClass Object ( [route_date] => 2014-01-22 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-23] => stdClass Object ( [route_date] => 2014-01-23 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-24] => stdClass Object ( [route_date] => 2014-01-24 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-25] => stdClass Object ( [route_date] => 2014-01-25 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-26] => stdClass Object ( [route_date] => 2014-01-26 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-27] => stdClass Object ( [route_date] => 2014-01-27 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-28] => stdClass Object ( [route_date] => 2014-01-28 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-29] => stdClass Object ( [route_date] => 2014-01-29 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-30] => stdClass Object ( [route_date] => 2014-01-30 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-31] => stdClass Object ( [route_date] => 2014-01-31 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-01] => stdClass Object ( [route_date] => 2014-02-01 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-02] => stdClass Object ( [route_date] => 2014-02-02 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-03] => stdClass Object ( [route_date] => 2014-02-03 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-04] => stdClass Object ( [route_date] => 2014-02-04 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-05] => stdClass Object ( [route_date] => 2014-02-05 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-06] => stdClass Object ( [route_date] => 2014-02-06 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-07] => stdClass Object ( [route_date] => 2014-02-07 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-08] => stdClass Object ( [route_date] => 2014-02-08 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-09] => stdClass Object ( [route_date] => 2014-02-09 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-10] => stdClass Object ( [route_date] => 2014-02-10 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-11] => stdClass Object ( [route_date] => 2014-02-11 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-12] => stdClass Object ( [route_date] => 2014-02-12 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-13] => stdClass Object ( [route_date] => 2014-02-13 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-14] => stdClass Object ( [route_date] => 2014-02-14 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-15] => stdClass Object ( [route_date] => 2014-02-15 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-16] => stdClass Object ( [route_date] => 2014-02-16 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-17] => stdClass Object ( [route_date] => 2014-02-17 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-18] => stdClass Object ( [route_date] => 2014-02-18 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-19] => stdClass Object ( [route_date] => 2014-02-19 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-20] => stdClass Object ( [route_date] => 2014-02-20 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-21] => stdClass Object ( [route_date] => 2014-02-21 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-22] => stdClass Object ( [route_date] => 2014-02-22 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-23] => stdClass Object ( [route_date] => 2014-02-23 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-24] => stdClass Object ( [route_date] => 2014-02-24 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-25] => stdClass Object ( [route_date] => 2014-02-25 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-26] => stdClass Object ( [route_date] => 2014-02-26 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-27] => stdClass Object ( [route_date] => 2014-02-27 [name] => 0 [openings] => 1 [appointments] => 0 ) )
You will notice that only the first instance of the first object is added to the new array $openings.
UPDATE:
I just realized that I need a multidimensional array for this, but when I try add the second dimension, I get the following error:
Cannot use object of type stdClass as array
What Am I doing wrong here? I want a single array $openings[route_date][number] but I can't get it work. Any help would be great.
echo "<pre>"before the output andecho "</pre>"after it..