2013-03-06 91 views
0

我想在另一個數組中打印一個聯合數組。PHP:將聯合數組打印到另一個數組中

下面是代碼:

「$ posted_fields」,是多個陣列組成的assosiative陣列,每個陣列包含一個張貼字段的數據。語法如下所示:my post field(checkboxes)= array('key'=>'FIELD'的名稱','value'=>'array(FIELD VALUES)',...)。

環路就像這樣:

$posted_fields = array(); 
foreach($post_values as $field_name => $field_value){ 


      $posted_fields[] = array('key' => $field_name, 
      'value' => $field_value, 
      'compare' => 'IN', 
      ); 



} 

我得到以下結果:

$posted_fields = array(
        array 
        (
         "key" => "property_country", 
         "value" => array 
          (
           "es", 
           "fr", 
           "ca", 
          ), 

         "compare" => "IN" 
        ),array 
        (
         "key" => "property_city", 
         "value" => array 
          (
           "montreal" 
          ), 

         "compare" => "IN" 
        ) 

      ); 

這是我想做的事:

「的$ args」 是assosiative陣列在那裏我想打印「$ posted_field」結果。

$args = array(
      'posts_per_page ' => -1, 
      'post_type' => 'property', 
      'post_status' => 'publish',    
      'meta_query' => array(
       'relationship' => 'AND', 

       $posted_fields 

      ), 
      'fields' => 'ids', 
     ); 

而不必此

$args = array(
      'posts_per_page ' => -1, 
      'post_type' => 'property', 
      'post_status' => 'publish',    
      'meta_query' => array(
       'relationship' => 'AND', 
       array(
        array 
        (
         "key" => "property_country", 
         "value" => array 
          (
           "es", 
           "fr", 
           "ca", 
          ), 

         "compare" => "IN" 
        ),array 
        (
         "key" => "property_city", 
         "value" => array 
          (
           "montreal" 
          ), 

         "compare" => "IN" 
        ) 

       ) 
      ), 
      'fields' => 'ids', 
     ); 

我想要得到的結果是這樣的:

$args = array(
      'posts_per_page ' => -1, 
      'post_type' => 'property', 
      'post_status' => 'publish',    
      'meta_query' => array(
       'relationship' => 'AND', 

        array 
        (
         "key" => "property_country", 
         "value" => array 
          (
           "es", 
           "fr", 
           "ca", 
          ), 

         "compare" => "IN" 
        ),array 
        (
         "key" => "property_city", 
         "value" => array 
          (
           "montreal" 
          ), 

         "compare" => "IN" 
        ) 

       ), 

      'fields' => 'ids', 
     ); 

這裏的區別是後[ '關係'=> '和'] 。我想要'['relationship'=>'AND',array(),array(),...]而不是['relationship'=>'AND',array(array(),array(), ...]

我希望信息是明確的

謝謝

回答

0

這可以幫助你更接近解決方案,我以前用過下面的功能擴展多維數組:

$input_size = (count($multidim_array)); 
for($row = 0; $row < $input_size; $row++) 
    { 
    echo "the row number $row<br>"; 

     foreach($multidim_array[$row] as $key => $value) 
     { 
      echo "key $key, value $value<br>"; 
     } 
    } 
相關問題