我正在尝试组织一组通过数据库查询获得的结果。以下是我用来执行此操作的代码:
$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 ) )
如果说完了,我得到了这个结果:
数组([2014-01-10] =&gt; stdClass对象([route_date] =&gt; 2014-01-10 [名称] =&gt; 0 [开头] =&gt; 1 [约会] =&gt; 0 )[2014-01-11] =&gt; stdClass对象([route_date] =&gt; 2014-01-11 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014- 01-12] =&gt; stdClass对象([route_date] =&gt; 2014-01-12 [name] =&gt; 0 [开头] =&gt; 1 [约会] =&gt; 0)[2014-01-13] =&gt; stdClass对象([route_date] =&gt; 2014-01-13 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-14] =&gt; stdClass对象([route_date] =&gt; 2014-01-14 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-15] =&gt; stdClass对象([route_date] ] =&gt; 2014-01-15 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-16] =&gt; stdClass对象([route_date] =&gt; 2014-01-16 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-17] =&gt; stdClass对象([route_date] =&gt; 2014-01- 17 [name] =&gt; 0 [opening] =&gt; 1 [appoi ntments] =&gt; 0)[2014-01-18] =&gt; stdClass对象([route_date] =&gt; 2014-01-18 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-19] =&gt; stdClass对象([route_date] =&gt; 2014-01-19 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-20] =&gt; stdClass对象([route_date] =&gt; 2014-01-20 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-21] =&gt; stdClass对象([route_date] =&gt; 2014-01-21 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-22] =&gt; stdClass对象([route_date] =&gt; 2014-01-22 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-23] =&gt; stdClass对象([route_date] =&gt; 2014-01-23 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-24] =&gt; stdClass对象([route_date] =&gt; 2014-01-24 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-25] =&gt; stdClass对象([route_date] =&gt; 2014-01-25 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-26] =&gt; stdClass对象([route_date] =&gt; 2014-01-26 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-27] =&gt; stdClass对象([route_date] =&gt; 2014-01-27 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-28] =&gt; stdClass对象([route_date] =&gt; 2014-01-28 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-29] =&gt; stdClass对象([route_date] =&gt; 2014-01-29 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-30] =&gt; stdClass对象([route_date] =&gt; 2014-01-30 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-01-31] =&gt; stdClass对象([route_date] =&gt; 2014-01-31 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-01] =&gt; stdClass对象([route_date] =&gt; 2014-02-01 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-02] =&gt; stdClass对象([route_date] =&gt; 2014-02-02 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-03] =&gt; stdClass对象([route_date] =&gt; 2014-02-03 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-04] =&gt; stdClass对象([route_date] =&gt; 2014-02-04 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-05] =&gt; stdClass对象([route_date] =&gt; 2014-02-05 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-06] =&gt; stdClass对象([route_date] =&gt; 2014-02-06 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-07] =&gt; stdClass对象([route_date] =&gt; 2014-02-07 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-08] =&gt; stdClass对象([route_date] =&gt; 2014-02-08 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-09] =&gt; stdClass对象([route_date] =&gt; 2014-02-09 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-10] =&gt; stdClass对象([route_date] =&gt; 2014-02-10 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-11] =&gt; stdClass对象([route_date] =&gt; 2014-02-11 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-12] =&gt; stdClass对象([route_date] =&gt; 2014-02-12 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-13] =&gt; stdClass对象([route_date] =&gt; 2014-02-13 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-14] =&gt; stdClass对象([route_date] =&gt; 2014-02-14 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-15] =&gt; stdClass对象([route_date] =&gt; 2014-02-15 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-16] =&gt; stdClass对象([route_date] =&gt; 2014-02-16 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-17] =&gt; stdClass对象([route_date] =&gt; 2014-02-17 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-18] =&gt; stdClass对象([route_date] =&gt; 2014-02-18 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-19] =&gt; stdClass对象([route_date] =&gt; 2014-02-19 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-20] =&gt; stdClass对象([route_date] =&gt; 2014-02-20 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-21] =&gt; stdClass对象([route_date] =&gt; 2014-02-21 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-22] =&gt; stdClass对象([route_date] =&gt; 2014-02-22 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-23] =&gt; stdClass对象([route_date] =&gt; 2014-02-23 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-24] =&gt; stdClass对象([route_date] =&gt; 2014-02-24 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-25] =&gt; stdClass对象([route_date] =&gt; 2014-02-25 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-26] =&gt; stdClass对象([route_date] =&gt; 2014-02-26 [name] =&gt; 0 [opening] =&gt; 1 [约会] =&gt; 0)[2014-02-27] =&gt; stdClass对象([route_date] =&gt; 2014-02-27 [name] =&gt; 0 [开头] =&gt; 1 [约会] =&gt; 0))
您会注意到只有第一个对象的第一个实例被添加到新数组$openings
。
更新:
我刚刚意识到我需要一个多维数组,但是当我尝试添加第二个维度时,我收到以下错误:
Cannot use object of type stdClass as array
我在这里做错了什么?我想要一个数组$openings[route_date][number]
,但我无法让它工作。任何帮助都会很棒。
答案 0 :(得分:2)
这是因为array_push()
适用于数组,而不是对象。您应该将stdClass
转换为数组。尝试使用:
$ts = (array) $ts;
答案 1 :(得分:0)
在array_push($openings[$ts->route_date][$ts->name], $ts);
中,第一个参数似乎不是数组。如果第一个参数不是数组,array_push()将发出警告。
This should be array_push($openings, $ts);
我做了一个很好的例子。
//I'm using mysql and I will switch to MySQLi or PDO_MySQL later)
$query = "SELECT * FROM events";
$result = mysql_query($query);
$openings = array();
while ($row = mysql_fetch_object($result)) {
array_push($openings, $row);
}
print_r($openings);
//output
Array
(
[0] => stdClass Object
(
[idevent] => 1
[event] => Event1
[event_date] => 2014-01-06
)
[1] => stdClass Object
(
[idevent] => 2
[event] => Event2
[event_date] => 2014-01-07
)
)
答案 2 :(得分:0)
事实证明我需要做一些事情。这是我需要的代码:
$time_slots = $wpdb->get_results($query);
echo("<pre>");
//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("Inserting: ");
echo ("<pre>");
print_r($ts);
echo("</pre>");
echo ("<pre>");
$openings[$ts->route_date][$ts->name] = $ts; <--- Needed to add the [$ts->name]
echo("</pre>");
echo("contents of Opening: ");
echo ("<pre>");
print_r($openings);
echo ("</pre>");
}
elseif (array_key_exists($ts->route_date, $openings))
{
echo("Same Day");
echo("Inserting: ");
echo ("<pre>");
print_r($ts);
echo("</pre>");
$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("Inserting: ");
echo ("<pre>");
print_r($ts);
echo("</pre>");
echo ("<pre>");
$openings[$ts->route_date][$ts->name] = $ts;
echo("</pre>");
echo("contents of Opening: ");
echo ("<pre>");
print_r($openings);
echo ("</pre>");
}