在数组中存储变量-PHP
我的网站上有一些PHP,其中包含以下代码部分:在数组中存储变量-PHP,php,arrays,wordpress,function,variables,Php,Arrays,Wordpress,Function,Variables,我的网站上有一些PHP,其中包含以下代码部分: 'choices'=>数组('london'=>'london','paris'=>'paris',), 当前此列表是静态的-我手动添加到其中,但是我希望动态生成列表 我正在使用以下代码从WordPress动态创建数组,并将其存储在变量中: function locations() { query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'post_type' =&
'choices'=>数组('london'=>'london','paris'=>'paris',),
当前此列表是静态的-我手动添加到其中,但是我希望动态生成列表
我正在使用以下代码从WordPress动态创建数组,并将其存储在变量中:
function locations() {
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'post_type' => 'location'));
if (have_posts()) :
while (have_posts()) : the_post();
$locations = "'\'get_the_slug()'\' => '\'get_the_title()'\',";
endwhile;
endif;
wp_reset_query();
$locations_list = "array (".$locations."),";
return $locations_list; // final variable
}
现在,这就是我被困的地方:-)
现在如何将$locations\u list
分配给'choices'
我尝试了'choices'=>$locations\u list
,但它使我的网站崩溃了
非常感谢您的指点。呃。。。为什么
$locations_list = array();
query_posts(...);
while(have_posts()) {
the_post();
$locations_list[get_the_slug()] = get_the_title();
}
wp_reset_query();
return $locations_list;
我不知道你从哪里读到可以从字符串构建变量,但是。。。你不能(除了eval
),所以只需阅读文档并从那里开始。。。为什么
$locations_list = array();
query_posts(...);
while(have_posts()) {
the_post();
$locations_list[get_the_slug()] = get_the_title();
}
wp_reset_query();
return $locations_list;
我不知道你从哪里读到可以从字符串构建变量,但是。。。你不能(除了eval
),所以只需阅读文档并从那里开始。尝试以下操作:-
function locations() {
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'post_type' => 'location'));
$locations = array();
if (have_posts()) :
while (have_posts()) : the_post();
$locations[get_the_slug()] = get_the_title();
endwhile;
endif;
wp_reset_query();
return $locations; // final variable
}
请尝试以下操作:-
function locations() {
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'post_type' => 'location'));
$locations = array();
if (have_posts()) :
while (have_posts()) : the_post();
$locations[get_the_slug()] = get_the_title();
endwhile;
endif;
wp_reset_query();
return $locations; // final variable
}
你可以用这个
<?php
function locations() {
$locations = array();
query_posts("orderby=date&order=DESC&post_type=location");
if (have_posts()) {
while (have_posts()) {
the_post();
$locations[] = get_the_slug() ."#". get_the_title();
}
}
wp_reset_query();
return $locations;
}
// using
$locations = locations();
foreach ($locations as $location) {
list($slug, $title) =@ explode("#", $location, 2);
echo $slug, $title;
}
?>
您可以使用这个
<?php
function locations() {
$locations = array();
query_posts("orderby=date&order=DESC&post_type=location");
if (have_posts()) {
while (have_posts()) {
the_post();
$locations[] = get_the_slug() ."#". get_the_title();
}
}
wp_reset_query();
return $locations;
}
// using
$locations = locations();
foreach ($locations as $location) {
list($slug, $title) =@ explode("#", $location, 2);
echo $slug, $title;
}
?>
什么是var\u dump
输出的$locations\u list
?我现在就试试:-)是的,在问题中更新它,然后在这里回答:)$locations\u list
的var\u dump
输出是什么?我现在就试试:-)是的,在问题和回答中更新它。:)这是一个愚蠢的错误。很抱歉当我运行该代码时,我的var\u dump($locations\u list)
=array(0){}
。然而,这种自定义帖子类型中肯定有帖子。有什么想法吗?试着在循环中输出一些东西,看看它是否在迭代?我认为,在这种情况下,'choices'
是数组的关联索引;所以他试图做的是$array['choices]=>$locations\u list代码>。。。但我可能错了。我想他网站上读到的'choices'=>array(…),
的代码实际上更像array('choices'=>array(…),…)代码>可以-2分钟后回来-谢谢你在这里对我的耐心:)这似乎不对。你的服务器上有错误日志文件吗?这是一个愚蠢的错误。很抱歉当我运行该代码时,我的var\u dump($locations\u list)
=array(0){}
。然而,这种自定义帖子类型中肯定有帖子。有什么想法吗?试着在循环中输出一些东西,看看它是否在迭代?我认为,在这种情况下,'choices'
是数组的关联索引;所以他试图做的是$array['choices]=>$locations\u list代码>。。。但我可能错了。我想他网站上读到的'choices'=>array(…),
的代码实际上更像array('choices'=>array(…),…)代码>可以-2分钟后回来-谢谢你在这里对我的耐心:)这似乎不对。您的服务器上是否有错误日志文件?