首页 > 解决方案 > 如何输出数组的值

问题描述

我是 PHP 的完整初学者。但是我知道如何输出自定义字段的值。我对数组有一些问题。后元键是fw_options. 该值有多个数组,如下所示:

array (
  0 => 
  array (
    'featured_post' => false,
    'featured_expiry' => '',
    '_featured_book_string' => '0',
    'reading_level' => 'medium',
    'reading_type' => 
    array (
      'time' => 'fixed',
      'hourly' => 
      array (
        'hourly_read' => '',
        'estimated_hours' => '',
      ),
      'fixed' => 
      array (
        'reading_times' => '500',
      ),
    ),
    'reading_duration' => 'one_month',
    'english_level' => 'fluent',
    'readers_level' => 'starter',
    'expiry_date' => '2019/12/31',
    'show_attachments' => 'off',
    'read_documents' => 
    array (
    ),
    'address' => '',
    'longitude' => '',
    'latitude' => '',
    'country' => 
    array (
      0 => '717',
    ),
  ),
)

我有这段代码,我试过但没有成功:

$array = get_post_meta( get_the_ID(), 'fw_options', true );
echo $array[0]['reading_type']['fixed']['reading_times'];

如何500从 post meta key输出值reading_times

标签: phparrays

解决方案


简单根据键打印数组先将值保存到变量中,然后根据数组键打印

$data = array(
            'featured_post' => false,
            'featured_expiry' => '',
            '_featured_book_string' => '0',
            'reading_level' => 'medium',
            'reading_type' =>
                array(
                    'time' => 'fixed',
                    'hourly' =>
                        array(
                            'hourly_read' => '',
                            'estimated_hours' => '',
                        ),
                    'fixed' =>
                        array(
                            'reading_times' => '500',
                        ),
                ),
            'reading_duration' => 'one_month',
            'english_level' => 'fluent',
            'readers_level' => 'starter',
            'expiry_date' => '2019/12/31',
            'show_attachments' => 'off',
            'read_documents' =>array(),
            'address' => '',
            'longitude' => '',
            'latitude' => '',
            'country' =>
                array(
                    0 => '717',
                ),
        );

echo $data['reading_type']['fixed']['reading_times'];

输出# 500


推荐阅读