简体   繁体   English

从 json 和 php 中嵌套的 arrays 获取数据

[英]Getting data from nested arrays in json with php

I have my json from a url feed.我的 json 来自 url 提要。 Here's a sample below.下面是一个示例。 I'm not doing the foreach loop correctly is the problem我没有正确执行 foreach 循环是问题所在

{
    "useLive": true,
    "models": [
        {
            "snapshotUrl": "https://img-eu.whatevercdn.com/eu7/previews/1537971705/5293074",
            "widgetPreviewUrl": "https://img-eu.whatevercdn.com/eu7/previews/1537971705/5293074",
            "id": 5293074,
            "country": "",
            "gender": "female",
            "isNew": false,
            "previewUrl": "https://st.whatevercdn.com/cdn/previews/b/a/a/baa515a42e75d80b0dc1e7a75bf4ea0f-full",
            "previewUrlThumbBig": "https://st.whatevercdn.com/cdn/previews/b/a/a/baa515a42e75d80b0dc1e7a75bf4ea0f-thumb-big",
            "previewUrlThumbSmall": "https://st.whatevercdn.com/cdn/previews/b/a/a/baa515a42e75d80b0dc1e7a75bf4ea0f-thumb-small",
            "broadcastGender": "female",
            "snapshotServer": "eu7",
            "tags": ["autoTagPopular","keyword","keyword2"],
            "topBestPlace": 0,
            "username": "model1",
            "languages": ["en"],
            "stripScore": 998.5,
            "token": "93021860dbebd5ba27e604f6b4b93754"
        },
        {
            "snapshotUrl": "https://img-eu.whatevercdn.com/eu8/previews/1537971700/6492104",
            "widgetPreviewUrl": "https://img-eu.whatevercdn.com/eu8/previews/1537971700/6492104",
            "id": 6492104,
            "country": "",
            "gender": "female",
            "isNew": false,
            "previewUrl": "https://st.whatevercdn.com/cdn/previews/2/b/3/2b366955f5a66d73ee038d43bf77c99b-full",
            "previewUrlThumbBig": "https://st.whatevercdn.com/cdn/previews/2/b/3/2b366955f5a66d73ee038d43bf77c99b-thumb-big",
            "previewUrlThumbSmall": "https://st.whatevercdn.com/cdn/previews/2/b/3/2b366955f5a66d73ee038d43bf77c99b-thumb-small",
            "broadcastGender": "female",
            "snapshotServer": "eu8",
            "tags": ["autoTagPopular","keyword","keyword2"],
            "topBestPlace": 0,
            "username": "model2",
            "languages": [],
            "stripScore": 997.25,
            "token": "2c6ee95270f6faf76cd33321732136e3"
        }
    ],
    "ttl": 15,
    "tagType": "F+T",
    "tagName": "Featured",
    "defaultTags": [
        {
            "name": "whatever1",
            "url": "/tags/whatever1"
        },
        {
            "name": "whatever2",
            "url": "/tags/whatever2"
        },
        {
            "name": "whatever3",
            "url": "/tags/whatever3"
        }
    ],
    "serverTime": "2018-09-26T14:23:00Z"
}

Here's my php code so far.到目前为止,这是我的 php 代码。 I've tried quite a few different things.我尝试了很多不同的东西。 I normally use xml feeds which seem to be easy for me to setup for what I need.我通常使用 xml 提要,这对我来说似乎很容易设置我需要的东西。 I'm not sure what I'm missing here.我不确定我在这里错过了什么。

$url = 'https://whatever.com/api/external/v4/widget?userId=whatever&tag=featured'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$performers = json_decode($data, true); // decode the JSON feed

foreach ($performers as $performer) {
  $info = $performer[0]["username"];
  echo $info;
}

I'm only getting the first username and then error messages.我只收到第一个用户名,然后收到错误消息。

Warning: Illegal string offset 'username' in /whatever警告:/whatever 中的非法字符串偏移量“用户名”

Can anyone help with this?有人能帮忙吗?

You should use $performers['models'] array in foreach and then get username it will work fine try the following code您应该在 foreach 中使用 $performers['models'] 数组,然后获取用户名它将正常工作尝试以下代码

$performers = json_decode($data, true); 
if(isset($performers['models'])){   
    foreach ($performers['models'] as $performer) {
        $info = (isset($performer["username"])) ? $performer["username"] : '';
        echo $info;
        echo "<br>";
    }   
}

Output Output

model1
model2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM