Я пытаюсь организовать набор результатов, полученных с помощью запроса к базе данных. Вот код, который я использую для этого:
$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);
Вот как выглядит один результат, когда я print_r
$ts
:
stdClass Object ( [route_date] => 2014-01-10 [name] => 2 [openings] => 1 [appointments] => 0 )
Вот как выглядит одна петля. Вы заметите, что логика работает, и все идет туда, куда должно идти, но добавление 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 ) )
Когда все сказано и сделано, я получаю такой результат:
Вы заметите, что в новый массив
$openings
добавляется только первый экземпляр первого объекта.
ОБНОВИТЬ:
Я только что понял, что для этого мне нужен многомерный массив, но когда я пытаюсь добавить второе измерение, я получаю следующую ошибку:
Что я здесь делаю неправильно? Мне нужен один массив $openings[route_date][number]
, но я не могу заставить его работать. Любая помощь будет здорово.
Cannot use object of type stdClass as array
Это потому, что
$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);
работает с массивами, а не с объектами. Вы должны преобразовать свой print_r
в массив. Попробуй использовать: