get_post_meta()到JSON

时间:2017-06-07 06:31:02

标签: php arrays json wordpress multidimensional-array

我有来自get_post_meta的生成数据,我需要将其转换为JSON。生成的数组如下:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [rate] => 5
                            [review] => Array
                                (
                                    [content] => Test Review
                                    [custom_field_1] => Azman Ishak
                                    [custom_field_2] => Selagnor
                                    [date] =>  2017-06-06  , 15:06 
                                    [title] => 
                                    [status] => publish
                                    [weight] => 5
                                )

                            [user] => Array
                                (
                                    [name] => ebeneadmin
                                    [email] => azman@considerdigital.com
                                    [id] => 1
                                    [ip] => 115.135.10.5
                                )

                        )

                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [rate] => 4
                            [review] => Array
                                (
                                    [content] => Test review 2
                                    [custom_field_1] => Azman
                                    [custom_field_2] => Beranang
                                    [date] =>  2017-06-06  , 16:01 
                                    [title] => 
                                    [status] => publish
                                    [weight] => 5
                                )

                            [user] => Array
                                (
                                    [name] => Azman Ishak
                                    [email] => azmunk@gmail.com
                                    [id] => 
                                    [ip] => 115.135.10.5
                                )

                        )

                )

        )

    [2] => Array
        (
            [0] => Array
                (
                )

        )

)

我需要从上面的数组获得的值是:

  1. 含量
  2. CUSTOM_FIELD_1
  3. CUSTOM_FIELD_2
  4. 日期
  5. 标题
  6. 状态
  7. 此数据需要转换为JSON。

1 个答案:

答案 0 :(得分:1)

试试这个:

将您的get_post_meta结果转换为json_encode();

它会使你的结果

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);
?>

输出:

{"a":1,"b":2,"c":3,"d":4,"e":5}