我想使用简单的str_replace()
目标是定位'value'
的值并将其从'male'
更改为'female'
。
<?php
$args = array(
'post_type' => 'mycustom',
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'nopaging' => true,
'cache_results' => false,
'update_post_meta_cache' => false,
'meta_query' => array(
array(
'key' => 'gender',
'value' => 'male',
'compare' => 'EXISTS'
)
)
);
$args = str_replace(('meta_query'[0][1]),'female',$args, $i);
print_r($args);
?>
$args = str_replace(('meta_query'[0][1]),'female',$args, $i);
中有什么用?
答案 0 :(得分:2)
为什么不使用它,尝试找到正确的路径并插入新值。像这里:
$args['meta_query'][0]['value'] = 'female';
插入新值而不是使用str_replace()
说明: str_replace
用于更改字符串的一部分,您正在使用嵌套的数组。
要更改数组中的值,您可以像上面一样选择它,并使用=
答案 1 :(得分:1)
您的解决方案无效,因为str_replace
不适用于嵌套数组(并且您的搜索字符串'meta_query[0][2]'
无效)。
因此,虽然您可以使用str_replace将顺序从'ASC'
更改为'DESC'
,但如果要定位嵌套数组值,则需要在数组上调用str_replace recursivly。
C Travel发布了一个有效的解决方案