检查PHP数组中的数组索引是否为1
这段代码拾取一个包含3个索引的数组:检查PHP数组中的数组索引是否为1,php,Php,这段代码拾取一个包含3个索引的数组: <?php var_dump($the_query1->posts); ?> array(3) { [0]=> object(WP_Post)#4451 (24) { ["ID"]=> int(19163) ["post_author"]=> string(1) "2" ["post_date"]=>
<?php var_dump($the_query1->posts); ?>
array(3) {
[0]=>
object(WP_Post)#4451 (24) {
["ID"]=>
int(19163)
["post_author"]=>
string(1) "2"
["post_date"]=>
string(19) "2018-09-28 15:42:12"
["post_date_gmt"]=>
string(19) "2018-09-28 13:42:12"
["post_content"]=>
string(0) ""
["post_title"]=>
string(16) "Global Factories"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(6) "closed"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(16) "global-factories"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2018-11-15 12:41:57"
["post_modified_gmt"]=>
string(19) "2018-11-15 11:41:57"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(48) "https://www.zeo.nl/?post_type=cases&p=19163"
["menu_order"]=>
int(0)
["post_type"]=>
string(5) "cases"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
[1]=>
object(WP_Post)#4448 (24) {
["ID"]=>
int(19167)
["post_author"]=>
string(1) "2"
["post_date"]=>
string(19) "2018-09-21 14:58:50"
["post_date_gmt"]=>
string(19) "2018-09-21 12:58:50"
["post_content"]=>
string(0) ""
["post_title"]=>
string(24) "Warmteservice Groep B.V."
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(6) "closed"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(13) "warmteservice"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2018-11-15 12:00:56"
["post_modified_gmt"]=>
string(19) "2018-11-15 11:00:56"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(48) "https://www.zeo.nl/?post_type=cases&p=19167"
["menu_order"]=>
int(0)
["post_type"]=>
string(5) "cases"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
[2]=>
object(WP_Post)#4556 (24) {
["ID"]=>
int(19157)
["post_author"]=>
string(1) "2"
["post_date"]=>
string(19) "2018-09-21 11:26:12"
["post_date_gmt"]=>
string(19) "2018-09-21 09:26:12"
["post_content"]=>
string(0) ""
["post_title"]=>
string(9) "Blauwtulp"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(6) "closed"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(11) "blauwtulp-2"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2018-11-15 12:46:48"
["post_modified_gmt"]=>
string(19) "2018-11-15 11:46:48"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(48) "https://www.zeo.nl/?post_type=cases&p=19157"
["menu_order"]=>
int(0)
["post_type"]=>
string(5) "cases"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
}
检查当前索引是否位于位置1的最佳方法是什么?我想返回一个bool来决定元素是应该变为true,还是返回true
我试过这个:
<?php var_dump($the_query1->posts[1] === true); ?>
但这是错误的三倍。我需要返回假,真,假。执行检查的最简单方法是什么?使用foreach循环数据,并在数组键为1时执行条件匹配
foreach ($the_query1->posts as $key => $value) {
if($key == 1){
echo '<div class="col-sm-3">';
} else {
echo '<div class="col-sm-4">';
}
echo '</div>';
}
你只是把你的问题发了这么多次,还是这是一个完全不同的问题?这听起来和你之前的2到3个问题完全一样。最后一个问题没有解决我的问题,所以我重新措辞了这个问题。我不想发垃圾邮件。你说当前索引位于位置1是什么意思?需要检查当前索引的代码在哪里?因此我重新措辞了问题-你应该编辑现有问题,使其清晰,而不是编写新问题。当我对$key执行var_转储时,我得到空值。还可以为$value获取NULL。在foreach中进行var_转储。您应该能够看到键的值。我在for循环中进行了var_转储,但在控制台中的任何地方都找不到日志。我还安装了WordPressPHP控制台,即使在那里,也没有日志。为什么呢?