【问题标题】:Combine two or more arrays with differnet formant in php在php中组合两个或多个不同格式的数组
【发布时间】:2018-10-05 11:42:45
【问题描述】:

我有三个数组 ($genneral , $step_1 , $step_2),我已经将它们组合成特定的格式

if (ISSET($_GET['step_1'])){
    $step_1 = array(
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',
                    'terms' => $_GET['step_1']
                );
}

if(ISSET($_GET['step_2'])){
    $all_data=explode(",",$_GET['step_2']);
    for ($i=0; $i<count($all_data); $i++){
        $step_2[] = array(
                        'taxonomy' => 'product_cat',
                        'field' => 'slug',
                        'terms' =>$all_data[$i]
                    );
   }

}

$genneral  = array(
                    'post_status' => 'publish',
                    'post_type' => 'product',
                    'orderby' => 'title',
                    'posts_per_page' => -1
                );

所以我必须从这三个数组中以特定格式组合这三个数组

if $_GET['step_1']=red

if $_GET['step_2']=hard,soft

那么结果参数应该是这样的,

 $args = array(
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'post_type' => 'product',
        'orderby' => 'title',
        'tax_query' => array(
        'relation' => 'AND',
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'slug',
                    'terms'    => array( 'red' ),
                ),
                array(
                    'relation' => 'OR',
                        array(
                            'taxonomy' => 'product_cat',
                            'field'    => 'slug',
                            'terms'    => array( 'hard' ),
                        ),
                        array(
                            'taxonomy' => 'product_cat',
                            'field'    => 'slug',
                            'terms'    => array( 'soft' ),
                        ),
                ),
     )
);     

请帮助解决这个问题。我知道这很难,但有人可以给出答案,这将非常有帮助。

请注意有时$_GET['step_2'] 包含许多字符串,例如 $_GET['step_2']=hard,soft,boiled, melt..

【问题讨论】:

    标签: php arrays wordpress multidimensional-array


    【解决方案1】:

    我不会直接分配三个单独的变量:-

    if(isset($_GET['step_1'])  && isset($_GET['step_2'])){
    
        $all_data=explode(",",$_GET['step_2']);
    
        $step_2['relation'] = 'OR';
    
        foreach ($all_data as $value){
            $step_2[] = array(
                            'taxonomy' => 'product_cat',
                            'field' => 'slug',
                            'terms' =>$value
                        );
       }
    
        $args = array(
                        'post_status' => 'publish',
                        'post_type' => 'product',
                        'orderby' => 'title',
                        'posts_per_page' => -1
                        'tax_query' => array(
                                'relation' => 'AND',
                                array(
                                    'taxonomy' => 'product_cat',
                                    'field' => 'slug',
                                    'terms' => $_GET['step_1']
                                ),
                                $step_2
                            );
                );
    
    }
    
    print_r($args);
    

    【讨论】:

      【解决方案2】:

      为了实现所描述的结果数组,我会这样处理它:

      if( isset($_GET['step_1']) )
      {
          $step1 = [
              'taxonomy' => 'product_cat',
              'field' => 'slug',
              'terms' => [ $_GET['step_1'] ],
          ];
      }
      
      if( isset($_GET['step_2']) )
      {
          $step2 = ['relation' => 'OR'];
          foreach(explode(',', $_GET['step_2']) as $search_term)
          {
      
              $step2[] = [
                              'taxonomy' => 'product_cat',
                              'field' => 'slug',
                              'terms' => [ $search_term ]
              ];
          }
      }
      
      $tax_query = ['relation' => 'AND'];
      if(isset($step1)){ $tax_query[] = $step1; }
      if(isset($step2)){ $tax_query[] = $step2; }
      
      $args = [
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'post_type' => 'product',
          'orderby' => 'title',
          'tax_query' => $tax_query,
      ];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-08
        • 2019-12-02
        • 2015-10-21
        • 2022-11-04
        • 2022-11-25
        • 2017-10-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多