如何在此数组中回显正确的变量

时间:2016-01-08 19:10:59

标签: php arrays

print_r给出:

Array ( 
  [8a19c684f653ac779ae1af5340d482bf] => Array ( 
    [booking] => Array ( 
      [_year] => 2016  
      [_month] => 2  
      [_day] => 27  
      [_persons] => Array ( [0] => 1 )  
      [_date] => 2016-2-27  
      [date] => February 27, 2016  
      [_time] =>  
      [_qty] => 1  
      [Persons] => 1  
      [_start_date] => 1456531200  
      [_end_date] => 1456617599  
      [_all_day] => 1  
      [_cost] => 36.2  
      [_booking_id] => 175  
    )   
    [product_id] => 145   
    [variation_id] => 0   
    [variation] => Array ( )   
    [quantity] => 1   
    [line_total] => 21   
    [line_tax] => 0   
    [line_subtotal] => 36.2   
    [line_subtotal_tax] => 0   
    [line_tax_data] => Array (   
      [total] => Array ( ) 
      [subtotal] => Array ( )   
    )   
    [data] => WC_Product_Booking Object (   
      [availability_rules:WC_Product_Booking:private] => Array ( )   
      [id] => 145 
      [post] => WP_Post Object (  
        [ID] => 145   
        [post_author] => 1   
        [post_date] => 2015-12-29 18:08:32   
        [post_date_gmt] => 2015-12-29 18:08:32   
        [post_content] => Sensational comedy   
        [post_title] => Bournemouth   
        [post_excerpt] =>   
        [post_status] => publish   
        [comment_status] => closed   
        [ping_status] => closed   
        [post_password] =>   
        [post_name] => bournemouth   
        [to_ping] =>   
        [pinged] =>   
        [post_modified] => 2015-12-30 19:26:20   
        [post_modified_gmt] => 2015-12-30 19:26:20   
        [post_content_filtered] =>   
        [post_parent] => 0   
        [guid] => /product/bournemouth/   
        [menu_order] => 0   
        [post_type] => product   
        [post_mime_type] =>   
        [comment_count] => 0   
        [filter] => raw   
      )   
      [product_type] => booking   
      [shipping_class:protected] =>    
      [shipping_class_id:protected] => 0    
      [price] => 36.2    
      [manage_stock] => no    
      [stock_status] => instock    
      [tax_status] => taxable    
    )   
  ) 
) 

如何回应_start_date?

我已经知道以下代码将回显post_title

foreach($items as $item => $values) { 
    $_product = $values['data']->post; 
    echo $_product->post_title;
} 

3 个答案:

答案 0 :(得分:0)

print $_product->post_title['8a19c684f653ac779ae1af5340d482bf']['booking']['_start_date'];

但请尝试

function showStruc(&$array,$prefix='$array'){
   foreach($array as $k=>$v){
      echo ($k = $prefix."['$k']"), "<br>";
      if(is_array($v)){
          showStruc($v,$k);
      }
   }
}
showStruc($array);

看到完整的结构 (对不起发布英菲尼迪循环,现在可以了)

答案 1 :(得分:0)

此关联数组只有一个项目。如果您希望系列中有多个项目,则需要使用foreach,您可以尝试此代码

from collections import defaultdict
import six


def calc_token_frequencies(doc_list):
    frequencies = defaultdict(int)  # Each dict item will start off as int(0)
    for token_set in doc_list:
        for token in token_set:
            frequencies[token] += 1
    return frequencies


if __name__ == '__main__':
    # Use a list of sets here in order to leverage set features
    tokenized_document_list = [{"egg", "apple", "bread", "milk", "pear"},
                               {"egg", "apple", "bread", "milk"},
                               {"egg", "apple", "bread", "milk"}]

    # Count the number of documents each token was in.
    token_frequencies = calc_token_frequencies(tokenized_document_list)

    # I used 50% here instead of the example 5% so that it would do something useful.
    token_min_docs = 0.5*len(tokenized_document_list)

    # Calculate the black list via set comprehension.
    token_blacklist = {token for token, doc_count in six.iteritems(token_frequencies)
                       if doc_count < token_min_docs}

    # Remove items on the black list
    for doc_tokens in tokenized_document_list:
        doc_tokens.difference_update(token_blacklist)

    print tokenized_document_list

该previuos代码并不完全正常。因为它试图在与数组上的项关联的每个值上查找_start_date变量。 只打印一次,因为数字上没有其他值的字段_start_date

否则,如果只是阵列中的一个项目尝试

 // iterate over the array items (booking, product_id, variation etc)
 foreach($items as $item => $values) { 

      // get the values associated with the items 
      $elem = $values['_start_date']; 

      // if the variable $values has an element  called _start_date
      // will print it
      echo $elem;
} 

答案 2 :(得分:-1)

ty smoqadam

比我想象的要简单

foreach($items as $item => $values) { 
    echo $values['booking']['_start_date'];
}