我试图在给定条件的情况下从多维数组中的数组中更改值。
<link rel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">
是否有必要进行foreach循环? 任何帮助将不胜感激!
答案 0 :(得分:0)
不确定省略号在做什么,但你试过了吗?
$arg_pub['tax_query'][2]['terms'] = array('susan', 'martha');
===
更新:
这对你有用吗?
...
'tax_query' => array(
'index' => array(
'taxonomy' => 'index',
'field' => 'slug',
'terms' => array('one', 'two', 'three'),
),
'class' => array(
'taxonomy' => 'class',
'field' => 'slug',
'terms' => array('aaa', 'bbb', 'ccc'),
),
'author' => array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => array('joe', 'peter', 'mark'), //array to be changed
),
),
...
$arg_pub['tax_query']['author']['terms'] = array('susan', 'martha');
====
更新2:
试试这个:
foreach($arg_pub['tax_query'] as &$taxq){
if($taxq['taxonomy'] == 'author'){
$taxq['terms'] = array('susan', 'martha');
}
}
答案 1 :(得分:0)
只需将...
替换为2
:
$arg_pub = array(
'post_type' => 'pubs',
'order' => 'ASC',
'orderby' => 'meta_value_num',
'meta_key' => 'pub_y',
'meta_query' => array(
array(
'key' => 'pub_y',
'value' => array( $_period_from, $_period_to),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
),
'tax_query' => array(
array(
'taxonomy' => 'index',
'field' => 'slug',
'terms' => array('one', 'two', 'three'),
),
array(
'taxonomy' => 'class',
'field' => 'slug',
'terms' => array('aaa', 'bbb', 'ccc'),
),
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => array('joe', 'peter', 'mark'), //array to be changed
),
),
);
if ( $some_condition ) {
// I want to only replace the values from this specific array from the $arg_pub:
// terms => array('joe', 'peter', 'mark')
$arg_pub['tax_query'][...]['terms'] = array('susan', 'martha');
}