【问题标题】:Multidimensional PHP array in for loop as JSONfor循环中的多维PHP数组作为JSON
【发布时间】:2016-07-09 10:14:18
【问题描述】:

我无法将我的 php 数组显示为 json 对象。我想在 php for 循环中用数据库中的数据填充数组,这样我就可以用它制作一个 json 对象,这样我就可以在 javascript 中使用它了。

当我使用 GetJSON 在控制台中记录数据时,它不会显示像“id”这样的数据,但是当我在 php.ini 中回显 JSON 时它会显示出来。 'name' 中的值在 for 循环之外填充,以测试我的数组是否作为 json 对象工作。

<script type='text/javascript'>
    $(document).ready(function(){

      $.getJSON('database/getTourInfo.php', function(data) {
        console.log(data);
          $.each(data, function(key, val) {
            console.log(val.show.name);
          });
      });

    });
  </script>

这是 getTourInfo.php。当我在 for 循环之外填充数组的值时,它将在控制台中输出(参见 'name' )。但是当我将它填充到如下所示的 for 循环中时,它会在我回显它时出现在 JSON 中,但不会显示在控制台中。

<?php

header("Content-type: text/javascript");

$x = 0;
$tourInfo  = array();
$tourInfo_array = array (
  "show"      => array(
          "id" => "", 
          "name" => "Naam1", 
          "date" => "", 
          "support" => "", 
          "festival" => ""
  ),
  "location"  => array(
          "latitude" => "",
          "longitude" => ""
  ),
  "venue"     => array(
          "name" => "", 
          "space" => "", 
          "capacity" => ""
  ),
  "people"    => array(
          "attending" => "",
           "interested" => ""
  )        
);

$tourdates = json_decode( $tourdatesJSON, true );
foreach($tourdates as $tourdate) { 
  $x++;

  $event_page_id  = $tourdate['ShowEventpage'];
  $festival       = $tourdate['ShowFestival'];
  $support        = $tourdate['ShowVoorprogramma'];

  $tourdates_array[$x]["show"]["id"] = $event_page_id;
  $tourdates_array[$x]["show"]["support"] = $support;
  $tourdates_array[$x]["show"]["festival"] = $festival;

  array_push($tourInfo, $tourInfo_array[$x]);

}

$tourInfoJSON = json_encode($tourInfo, JSON_PRETTY_PRINT);
echo $tourInfoJSON;
?>

如何在 for 循环中填充我的多维数组并使用 getJSON 将其输出为 json?

【问题讨论】:

  • $.getJSON() 期待Content-Type: application/json,不是吗?变量$tourdatesJSON 来自哪里?
  • 这是另一个来自数据库的 JSON 对象,我已将其包含在 HTML 的 中。我已将内容类型更改为 application/json 但没有结果。
  • $tourInfo_array[$x] 没有定义 - 你的代码有点混乱,你能简化一下吗?

标签: php arrays json multidimensional-array getjson


【解决方案1】:

你试过了吗?

header('Content-Type: application/json');

echo json_encode($tourInfo, JSON_PRETTY_PRINT);

【讨论】:

    【解决方案2】:

    从查询的预期输出来看,我认为您的构造有点错误。

    试试这个;

    <?php
    
    $tourdate = array(
      '2016-03-01' => array(
        'ShowEventpage' => '1',
        'ShowFestival' => 'Isle of Wight',
        'ShowVoorprogramma' =>'N'
        ),
      '2016-04-01' => array(
        'ShowEventpage' => '2',
        'ShowFestival' => 'V Festival',
        'ShowVoorprogramma' =>'N'
        ),
      '2016-05-01' => array(
        'ShowEventpage' => '3',
        'ShowFestival' => 'Glastonbury',
        'ShowVoorprogramma' =>'Y'
        ),
      '2016-06-01' => array(
        'ShowEventpage' => '4',
        'ShowFestival' => '1Xtra',
        'ShowVoorprogramma' =>'Y'
        ),
      '2016-07-01' => array(
        'ShowEventpage' => '5',
        'ShowFestival' => 'Party in the Park',
        'ShowVoorprogramma' =>'N'
        )
      );
    
    $tourdatesJSON = json_encode($tourdate);
    
    
    $x = 0;
    $tourInfo  = array();
    $tourInfo_array = array (
      "location"  => array(
              "latitude" => "",
              "longitude" => ""
      ),
      "venue"     => array(
              "name" => "", 
              "space" => "", 
              "capacity" => ""
      ),
      "people"    => array(
              "attending" => "",
               "interested" => ""
      )        
    );
    
    $tourdates = json_decode( $tourdatesJSON, true );
    $tourdates_array = array();
    
    $i = intval(0);
    foreach($tourdates as $date)
    { //Foreach through list of dates
    
        $tourdates_array[] = array(
          'show' => array(
            'id' => $date['ShowEventpage'],
            'support' => $date['ShowVoorprogramma'],
            'festival' => $date['ShowFestival']
            ),
            "location"  => array(
                    "latitude" => "",
                    "longitude" => ""
            ),
            "venue"     => array(
                    "name" => "", 
                    "space" => "", 
                    "capacity" => ""
            ),
            "people"    => array(
                    "attending" => "",
                     "interested" => ""
            )  
        );    
        $i++;
    }
    
    $tourInfoJSON = json_encode($tourdates_array, JSON_PRETTY_PRINT);
    
    
    header("Content-type: application/json");
    echo $tourInfoJSON;
    ?>
    

    这是我在命令行上运行时得到的;

    [
        {
            "show": {
                "id": "1",
                "support": "N",
                "festival": "Isle of Wight"
            },
            "location": {
                "latitude": "",
                "longitude": ""
            },
            "venue": {
                "name": "",
                "space": "",
                "capacity": ""
            },
            "people": {
                "attending": "",
                "interested": ""
            }
        },
        {
            "show": {
                "id": "2",
                "support": "N",
                "festival": "V Festival"
            },
            "location": {
                "latitude": "",
                "longitude": ""
            },
            "venue": {
                "name": "",
                "space": "",
                "capacity": ""
            },
            "people": {
                "attending": "",
                "interested": ""
            }
        },
        {
            "show": {
                "id": "3",
                "support": "Y",
                "festival": "Glastonbury"
            },
            "location": {
                "latitude": "",
                "longitude": ""
            },
            "venue": {
                "name": "",
                "space": "",
                "capacity": ""
            },
            "people": {
                "attending": "",
                "interested": ""
            }
        },
        {
            "show": {
                "id": "4",
                "support": "Y",
                "festival": "1Xtra"
            },
            "location": {
                "latitude": "",
                "longitude": ""
            },
            "venue": {
                "name": "",
                "space": "",
                "capacity": ""
            },
            "people": {
                "attending": "",
                "interested": ""
            }
        },
        {
            "show": {
                "id": "5",
                "support": "N",
                "festival": "Party in the Park"
            },
            "location": {
                "latitude": "",
                "longitude": ""
            },
            "venue": {
                "name": "",
                "space": "",
                "capacity": ""
            },
            "people": {
                "attending": "",
                "interested": ""
            }
        }
    ]
    

    您所做的主要问题是您正在定义$tourInfo_array 数组,然后尝试向其中添加信息。因此,当您尝试从那里输出数组时,您最初放入数组中的所有内容仍保持原样,并且循环只是将新条目添加到底部。

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 2019-07-17
      相关资源
      最近更新 更多