我正在尝试更改多维数组中的数组的值,给定条件。
$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');
// EDIT: with [...] I mean that I don't know what to write there.
// The position for this array won't be always [2], it could change.
}
是否有必要做一个 foreach 循环?任何帮助将不胜感激!
不确定省略号在做什么,但你试过吗:
$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');
}
}
只需将...
替换为 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');
}