【问题标题】:php create json multiple arraysphp创建json多个数组
【发布时间】:2016-09-24 10:25:30
【问题描述】:

php 创建 json 多个数组。使用 php 跟踪代码我需要像 json 这样的输出 .. 这么多数组和对象让我感到困惑。我们使用wordpres

<?php 
 $categories = get_categories( array(
  'orderby' => 'name',
  'parent'  => 0
  ));

 foreach ( $categories as $category ) {
    $category_id =  $category->term_id;
    $category_name = $category->name;
    echo  $category_name;
    echo "<br>";

        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'category'  => $category_id
        );

        $myposts = get_posts( $args );
        foreach( $myposts as $post ){
            $category_postname = $post->post_title;
            echo $category_postname;
            echo "<br>";
        }
}
?>

php 输出为"

    Testcat1
      pos1
      post2
    Testcat2
      post3
      post4
    TestCat3
      post5
      post 6

我需要像这样创建 Json:

    {
      "data": [
        {
          "cat": "Testcat1",
          "post": [
            {
              "name": "post1"
            },
            {
              "name": "post2"
            }
          ]
        },
        {
          "cat": "Testcat2",
          "post": [
            {
              "name": "post3"
            },
            {
              "name": "post4"
            }
          ]
        },
        {
          "cat": "Testcat3",
          "post": [
            {
              "name": "post5"
            },
            {
              "name": "post6"
            }
          ]
        }
      ]
    }

我需要这样的 json 输出。

【问题讨论】:

  • json_encode 呢?

标签: php json wordpress


【解决方案1】:

您应该将所有内容放在一个数组中并对其进行 json_encode。我注释掉了您的回声,以便您以后需要时可以使用它们。

<?php 
$categories = get_categories( array(
    'orderby' => 'name',
    'parent'  => 0
));

// init empty array with root "data"
$array = array( 'data' => array() );
// set counter to 0 for array later on
$n = 0;

foreach ( $categories as $category ) {
    $category_id =  $category->term_id;
    $category_name = $category->name;

    // store cat.name
    $array['data'][$n]['cat'] = $category_name;
    // echo  $category_name;
    // echo "<br>";

    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category'  => $category_id
    );

    $myposts = get_posts( $args );
    // init posts counter
    $i = 0;
    foreach( $myposts as $post ){
        $category_postname = $post->post_title;
        $array['data'][$n]['post'][$i]['name'] = $category_postname;
        $array['data'][$n]['post'][$i]['id'] = $post->ID;
        // echo $category_postname;
        // echo "<br>";
        // increment post loop counter
        $i++;
    }

    // increment counter for array
    $n++;
}

echo json_encode( $array );
?>

【讨论】:

  • @suresh 没问题:顺便说一下,您可以选择这个答案作为您将要使用的答案
  • 如何在 post 数组中添加 $category_postid? "post": [ { "name": "post5","id": "1", }
  • 您需要在 $myposts 循环中添加一个额外的计数器。我将编辑代码
猜你喜欢
  • 2017-04-04
  • 2018-04-19
  • 2019-10-14
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 2011-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多