【发布时间】:2014-12-08 19:28:11
【问题描述】:
我已经构建了一个用于自定义 Wordpress 查询的多维数组。虽然是Wordpress,但我觉得这更像是一个基本的PHP问题。
我现在拥有的数组可以正确输出所有内容,接受索引值。我需要它是一个字符串而不是一个整数。
这是我需要的输出
Array (
[post_type] => property
[posts_per_page] => -1
[tax_query] => Array (
[0] => Array (
[taxonomy] => state
[field] => slug
[terms] => illinois
)
[1] => Array (
[taxonomy] => illinois_county
[field] => slug
[terms] => fulton
)
)
)
这是实际输出的内容
Array (
[post_type] => property
[posts_per_page] => -1
[0] => Array (
[0] => Array (
[taxonomy] => state
[field] => slug
[terms] => illinois
)
[1] => Array (
[taxonomy] => illinois_county
[field] => slug
[terms] => fulton
)
)
)
唯一的区别是我的第二个数组的键是0,需要是tax_query。
为了获得这个数组,我声明了$tax_query_array = array();,然后根据 url 中存在的变量(例如$tax_query_array[] = $state_array; 和$tax_query_array[] = $county_array;)根据需要删除我的子数组。然后最后调用$tax_query_array 我需要最终的多维数组输出。
唯一阻止我的是最初的 [0] 需要改为 [tax_query]。
这里是完整的代码:
$tax_query_array = array();
$tax_query_array['tax_query'][] = $state_array;
$tax_query_array['tax_query'][] = $county_array;
$tax_query_array['tax_query'][] = $price_range_array;
$taxonomy_args = array(
'post_type' => 'property',
'posts_per_page' => -1,
$tax_query_array
}
通过 MikeBrant 将 $tax_query_array[] = $county_array; 更改为 $tax_query_array['tax_query'][] = $county_array; 的输出:
Array (
[post_type] => property
[posts_per_page] => -1
[0] => Array (
[tax_query] => Array (
[0] => Array (
[taxonomy] => state
[field] => slug
[terms] => illinois
)
[1] => Array (
[taxonomy] => illinois_county
[field] => slug
[terms] => fulton
)
)
)
)
【问题讨论】:
-
在代码中改一下?
-
你是怎么得到这个数组的?您有什么理由不能按照上面的建议更改密钥?
-
为清楚问题而编辑
标签: php arrays wordpress multidimensional-array