【问题标题】:Not getting exact key data from json php? [closed]没有从 json php 获得准确的关键数据? [关闭]
【发布时间】:2021-12-24 13:28:12
【问题描述】:

我正在用 PHP 解码 JSON。我想显示第一个类别,然后是相关文章链接,然后是标题。这是我正在尝试的 PHP 代码 + Json..

<html>
<head>
</head>
<body>
<?php
$json = '[{
    "article": "https://example.com",
    "category": "Cat2",
    "title" : "1the title Cat2"
}, {
    "article": "https://example.com",
    "category": "Cat1",
    "title" : "1the title Cat1"
}, {
    "article": "https://example.com",
    "category": "Cat1",
    "title" : "2the title Cat1"
}, {
    "article": "https://example.com",
    "category": "Cat2",
    "title" : "2the title Cat2"
}, {
    "article": "https://example.com",
    "category": "Cat1",
    "title" : "3the title Cat1"
}]';
$values = json_decode($json, true);

$res = [];
foreach ($values as $entry) {
  $category = $entry['category'];
  if (! array_key_exists($category, $res)) {
    $res[$category] = [];
  }
   $res[$category][] = $entry;
}

foreach($res as $category => $entry): ?>
  <h1><?= $category; ?></h1>
    <?php foreach($entry as $article): ?>
    <p><?= $entry['title']; ?></p>
  <?php endforeach; ?>
<?php endforeach; 
?>
</body>
</html>

它有时会返回数组,现在什么都没有......

我想要这样的东西......

<h1>Cat2</h1>
    <p>https://example.com</p>
<span>the title</span>
    <p>https://example.com</p>
<span>the title</span>
  <h1>Cat1</h1>
    <p>https://example.com</p>
<span>the title</span>
    <p>https://example.com</p>
<span>the title</span>
    <p>https://example.com</p>
<span>the title</span>

提前致谢。

【问题讨论】:

  • $entry['title'] 应该是$article['article']
  • “未定义的索引:标题”应该给你一个提示。
  • @NigelRen 你是对的..
  • @NigelRen 我可以在没有第二次 foreach 的情况下获得这些键值吗?

标签: php arrays json


【解决方案1】:
<html>
<head>
</head>
<body>
<?php
$json = '[{
    "article": "https://example.com",
    "category": "Cat2",
    "title" : "1the title Cat2"
}, {
    "article": "https://example.com",
    "category": "Cat1",
    "title" : "1the title Cat1"
}, {
    "article": "https://example.com",
    "category": "Cat1",
    "title" : "2the title Cat1"
}, {
    "article": "https://example.com",
    "category": "Cat2",
    "title" : "2the title Cat2"
}, {
    "article": "https://example.com",
    "category": "Cat1",
    "title" : "3the title Cat1"
}]';
$values = json_decode($json, true);

$res = [];
foreach ($values as $entry) {
  $category = $entry['category'];
  if (! array_key_exists($category, $res)) {
    $res[$category] = [];
  }
   $res[$category][] = $entry;
}

foreach($res as $category => $entry): ?>
  <h1><?= $category; ?></h1>
    <?php foreach($entry as $article): ?>
    <p><?= $article['article']; ?></p>
    <p><?= $article['title']; ?></p>
  <?php endforeach; ?>
<?php endforeach; 
?>
</body>
</html>

【讨论】:

  • 不要只是转储代码......一个好的答案包含对您所做更改的解释以及 - 最重要的是 - 为什么会解决问题
【解决方案2】:

一种解决方案是首先解析 json 并将数据值保存在单独的变量中。例如你已经做过的:

 foreach ($values as $entry) {
       $article = $entry->article;
       $category = $entry->category;
       $title = $entry->title; 

       //Keep the variables in an array called information                
       $information = array(
            array($article, $category, $title)
       );

       //Then you can show the results as you want. For example:
       echo '<br>';
       echo "article: ". $information[0][0]."<br>"." category : ".$information[0][1]."<br>"."title : ".$information[0][2]."<br>";
    
         
}

【讨论】:

  • 我认为在这里使用另一个变量没有意义。 OP 想要按类别分组的条目。这并不能解决 OP 的问题,这是一个错字。
  • 感谢您的评论@gre_gor。您完全正确,我们需要按类别对条目进行分类。这是一个遗漏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
相关资源
最近更新 更多