【问题标题】:php json foreach string valuephp json foreach 字符串值
【发布时间】:2018-12-03 12:24:36
【问题描述】:

我有一个 JSON 文件,其中包含一些电影数据:

[{
    "title": "Bad Company",
    "desc": "------------------------",
    "rating": "6.0",
    "image": "Psycho-Ex-Girlfriend-Twisted-2018.png",
    "url": "master.m3u8",
    "category": "اكشن"
}, {
    "title": "The Pinch",
    "desc": "------------------------",
    "rating": "6.1",
    "image": "Psycho-Ex-Girlfriend-Twisted-2018.png",
    "url": "master.m3u8",
    "category": "اكشن , جريمه"
}, {
    "title": "Catskill Park",
    "desc": "------------------------",
    "rating": "6.2",
    "image": "Psycho-Ex-Girlfriend-Twisted-2018.png",
    "url": "master.m3u8",
    "category": "خيال علمي , رعب"
}, {
    "title": "Klippers",
    "desc": "------------------------",
    "rating": "5.3",
    "image": "Psycho-Ex-Girlfriend-Twisted-2018.png",
    "url": "master.m3u8",
    "category": "اثاره , اكشن"
}, {
    "title": "Psycho",
    "desc": "------------------------",
    "rating": "5.6",
    "image": "Psycho-Ex-Girlfriend-Twisted-2018.png",
    "url": "master.m3u8",
    "category": "اثاره , دراما"
}]

我需要将所有标题添加到下拉菜单中,当用户从菜单中选择一个项目时,会显示说明、图像和类别。 通过 php

谁能帮帮我?

我的托盘代码

$url = 'http://localhost/ar.json';
$xx = json_decode(file_get_contents($url));

$data = json_decode($xx, true);
$search='title';

foreach($data['meta_data'] as $d){
    if($d['key']==$search){
        $found=$d['value'];
        break;
    }
}
echo $found?$found:"$search not found";

【问题讨论】:

  • 您自己有没有尝试过任何事情来达到这个要求?如果是这样,请告诉我们
  • @RiggsFolly 是的,我尝试 foreach 但没有工作,我只是初学者谢谢你的回答:)
  • 好的,让我们看看这段代码。它将让我们更好地了解您实际在做什么以及使用什么语言
  • $url = 'localhost/ar.json'; $xx = json_decode(file_get_contents($url)); $data = json_decode($xx, true); $搜索='标题'; foreach($data['meta_data'] as $d){ if($d['key']==$search){ $found=$d['value'];休息; } } echo $found?$found:"$search not found";
  • 使用问题下方的edit 链接将其添加到您的问题中。没有人可以阅读评论中的代码

标签: php json html


【解决方案1】:

假设结构是正确的,而您并不完全了解我们。

像这样使用foreach的key和value方法

$url = 'http://localhost/ar.json';
$xx = json_decode(file_get_contents($url));

$data = json_decode($xx, true);
$search='title';

$found = false;

foreach($data['meta_data'] as $key => $val){
    if($key == $search){
        $found = $val;
        break;
    }
}
echo $found ? $found : "$search not found";

【讨论】:

  • 感谢 meta_data 并不重要,请问我如何在同一个 opject titek 中回显 desc 和其他
  • 该数组中的每个 Occurance 都有一个 title 键?也许您应该寻找像Bad Company 这样的特定值,而不是title 的键??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
相关资源
最近更新 更多