数组推送多维数组

时间:2013-05-29 01:48:38

标签: php arrays multidimensional-array array-push

尝试使用 array_push 将下面的数组添加到现有数组中:

'tax_query' => array(
        'relation' => 'and',
        array(
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => 'national'
            )
    )

现有数组:

$args = array(
        'post_type' => 'events',       
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'events_start-date',
                'value' => date('YYYY-mm-dd'),
                'type' => 'CHAR',
                'compare' => '>='
            )
        )
);

我试过这段代码没有运气:

array_push(
$args, array('tax_query'=> array('relation' => 'and', 
array('taxonomy' => 'prince-range','field' => 'slug','terms' => '99')
)));

3 个答案:

答案 0 :(得分:1)

你过度复杂了。

$args['tax_query'] = array(
     'relation' => 'and',
          array(
               'taxonomy' => 'type',
               'field' => 'slug',
               'terms' => 'national'
         )
     );

答案 1 :(得分:0)

$merged = array_merge($args, $array_to_merge);

只要合并2个数组 - 您需要使用array_merge,而不是array_push

在线演示:http://ideone.com/jHQipV

答案 2 :(得分:0)

$arr = array('tax_query' => array(
        'relation' => 'and',
        array(
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => 'national'
            )
    )
);

$args = array(
        'post_type' => 'events',       
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'events_start-date',
                'value' => date('YYYY-mm-dd'),
                'type' => 'CHAR',
                'compare' => '>='
            )
        )
);

$args[] = $arr;

$args上的var_dump:

array(3) { ["post_type"]=> string(6) "events" ["meta_query"]=> array(2) { ["relation"]=> string(3) "AND" [0]=> array(4) { ["key"]=> string(17) "events_start-date" ["value"]=> string(26) "2013201320132013-0505-2929" ["type"]=> string(4) "CHAR" ["compare"]=> string(2) ">=" } } [0]=> array(1) { ["tax_query"]=> array(1) { ["relation"]=> array(3) { ["taxonomy"]=> string(4) "type" ["field"]=> string(4) "slug" ["terms"]=> string(8) "national" } } } }