来自JSON数组的数据

时间:2018-08-29 23:22:42

标签: php json

鉴于我已阅读的所有内容,我是如此亲密。确实,我已经在研究此问题,并尝试了几天。无休止的google搜索和php手动阅读示例。我只是不认为我击中了那个AHA!随我而来的瞬间。

我想从其他服务器中的js文件中获取数据。

<?PHP

$token = "123";
//setup the request, you can also use CURLOPT_URL
$ch = curl_init('http://www.123.com/api/channels?limit=100000');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Content-Type: application/json',
   'Authorization: Bearer ' . $token
   ));

$data = curl_exec($ch);

curl_close($ch);

//$xx = json_decode($data, true);
  $someArray = json_decode($data, true);

//print_r($someArray);
 foreach ($someArray as $key => $value) {
    echo $key["id"] . ", " . $key["text"] . "<br>";
  }
?>  

**尝试使用PHP解析JSON文件。但是我现在被困住了。

这是我的JSON文件outout的内容**

 {
    "code": 100,
    "message": "get successfully",
    "all": [{
            "id": 1,
            "image": "http:\/\/www.live-plus.io\/cache\/d7\/23d79957adf9dc7d8e675f0cfa76a55d.png",
            "text": "Bein Sport",
            "order": 1,
            "is_active": 1,
            "created_at": "2017-10-22 00:03:08",
            "updated_at": "2018-05-08 16:22:53",
            "channels": [{
                    "id": 1,
                    "name": "bein News",
                    "image": "http:\/\/www.live-plus.io\/cache\/35\/7835dc385c571b09cbb8caabc07dcdb4.png",
                    "is_active": 1,
                    "is_feature": 0,
                    "order": 1,
                    "created_at": "2017-10-22 00:03:51",
                    "updated_at": "2018-01-18 21:22:45",
                    "pivot": {
                        "category_id": 1,
                        "channel_id": 1
                    },
                    "servers": [{
                            "id": 87,
                            "title": "\u062c\u0648\u062f\u0647 \u0639\u0627\u0644\u064a\u0647",
                            "url": "redirect\/sport1\/BeinSportNews_source",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 1,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 11:48:56",
                            "updated_at": "2018-08-17 12:54:26",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/BeinSportNews_source\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=IKcJuqTDez4txx5PLD2-eLPJx07T0UCyCgMJTWqavlg="
                        }, {
                            "id": 89,
                            "title": "\u062c\u0648\u062f\u0647 \u0645\u062a\u0648\u0633\u0637\u0629 (360)",
                            "url": "redirect\/sport1\/BeinSportNews_360p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 1,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 11:50:23",
                            "updated_at": "2018-08-17 12:54:35",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/BeinSportNews_360p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=3mxu47vfYmQ5EwLoWNd2_Ni-j7hKWEKnc7BTgcVVgOM="
                        }, {
                            "id": 659,
                            "title": "\u062c\u0648\u062f\u0629 \u0636\u0639\u064a\u0641\u0629 (240)",
                            "url": "redirect\/sport1\/BeinSportNews_240p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 1,
                            "server_type_id": 2,
                            "created_at": "2018-03-30 14:49:50",
                            "updated_at": "2018-08-17 12:54:39",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/BeinSportNews_240p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=HEIr-OaAmNDLE_gqvVPj8-uW6FcaG190ZLDzlNRM6mk="
                        }
                    ]
                }, {
                    "id": 2,
                    "name": "bein HD",
                    "image": "http:\/\/www.live-plus.io\/cache\/7d\/6e7dd27da3b4a9ed33dca15307ba879f.png",
                    "is_active": 1,
                    "is_feature": 0,
                    "order": 2,
                    "created_at": "2017-10-22 00:04:12",
                    "updated_at": "2018-01-18 21:24:14",
                    "pivot": {
                        "category_id": 1,
                        "channel_id": 2
                    },
                    "servers": [{
                            "id": 90,
                            "title": "\u062c\u0648\u062f\u0647 \u0639\u0627\u0644\u064a\u0647",
                            "url": "redirect\/sport1\/beINSPORTSHD_source",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 2,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 11:50:57",
                            "updated_at": "2018-08-17 12:54:43",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/beINSPORTSHD_source\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=zziSNmcRfuPO8du2h-u4meRkuLV40G3SBVcGTqVmYUc="
                        }, {
                            "id": 92,
                            "title": "\u062c\u0648\u062f\u0647 \u0645\u062a\u0648\u0633\u0637\u0629 (360)",
                            "url": "redirect\/sport1\/beINSPORTSHD_360p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 2,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 11:52:03",
                            "updated_at": "2018-08-17 12:54:47",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/beINSPORTSHD_360p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=6JxLzcmNV1wdQN8ltaHaBEBfJz6Q7xwNz2UklshsLuw="
                        }, {
                            "id": 661,
                            "title": "\u062c\u0648\u062f\u0629 \u0636\u0639\u064a\u0641\u0629 (240)",
                            "url": "redirect\/sport1\/beINSPORTSHD_240p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 2,
                            "server_type_id": 2,
                            "created_at": "2018-03-30 14:54:13",
                            "updated_at": "2018-08-17 12:54:52",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/beINSPORTSHD_240p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=HMIy3NwWgJzMCxp8TCAO55AMhM_QT0DxChNU6nAUyIo="
                        }
                    ]
                }, {
                    "id": 3,
                    "name": "Bein Sports 1",
                    "image": "http:\/\/www.live-plus.io\/cache\/ff\/59ffb0c1f6fc5f230f7afcc750da6f12.png",
                    "is_active": 1,
                    "is_feature": 0,
                    "order": 3,
                    "created_at": "2017-10-22 00:04:34",
                    "updated_at": "2018-01-17 17:18:13",
                    "pivot": {
                        "category_id": 1,
                        "channel_id": 3
                    },
                    "servers": [{
                            "id": 1,
                            "title": "\u062c\u0648\u062f\u0647 \u0639\u0627\u0644\u064a\u0647 (720)",
                            "url": "redirect\/Beinx1\/bx1_source",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 3,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 00:30:21",
                            "updated_at": "2018-08-17 18:08:16",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_source\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=85LyxJDxf9i0bU6kioFowv2I-Qon_0xPNb_M9G_kBqM="
                        }, {
                            "id": 2,
                            "title": "\u062c\u0648\u062f\u0629 \u0645\u062a\u0648\u0633\u0637\u0629 (480)",
                            "url": "redirect\/Beinx1\/bx1_480p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 3,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 00:33:39",
                            "updated_at": "2018-08-17 18:08:20",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_480p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=0vjltj0WknuGZ2ZH5yVdGASflpXN9WtYtZGi-PQdJ-A="
                        }, {
                            "id": 3,
                            "title": "\u062c\u0648\u062f\u0647 \u0645\u062a\u0648\u0633\u0637\u0629 (360)",
                            "url": "redirect\/Beinx1\/bx1_360p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 3,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 00:34:14",
                            "updated_at": "2018-08-17 18:08:24",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_360p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=S8vjztw7O8vZJfoDqmMWQ8Kd5OXgh624Fz3H1mDOdn0="
                        }, {
                            "id": 4,
                            "title": "\u062c\u0648\u062f\u0647 \u0636\u0639\u064a\u0641\u0629 (240)",
                            "url": "redirect\/Beinx1\/bx1_240p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 3,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 00:34:33",
                            "updated_at": "2018-08-17 18:08:28",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_240p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=kcIoL7nR7fGRfJ_HoA747nbjiAjphEncaj1JpIJ8_dc="
                        }, {
                            "id": 6,
                            "title": "\u062c\u0648\u062f\u0647 \u0636\u0639\u064a\u0641\u0629 - \u0645\u062e\u0635\u0635\u0647 \u0644\u0644\u0628\u0627\u0642\u0629",
                            "url": "redirect\/Beinx1\/bx1_160p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 3,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 00:38:25",
                            "updated_at": "2018-08-17 18:08:32",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_160p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=nS6iGwp-kLIOMsIWygOnuamfFq13KKjJhJgJ0i2lAos="
                        }, {
                            "id": 7,
                            "title": "\u062c\u0648\u062f\u0647 \u0639\u0627\u0644\u064a\u0647 (720) \u0645\u0635\u062f\u0631 2",
                            "url": "redirect\/Beinx3\/x1_source",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 3,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 00:38:47",
                            "updated_at": "2018-08-17 18:14:34",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx3\/x1_source\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=1MuhTMmCh8l8EBcfMrhpRmLt8XbeVCIlwF5zDk6sbfg="

我需要通过名称项获取secure_url

1 个答案:

答案 0 :(得分:0)

从理论上讲,您需要三个条件才能找到特定的secure_url

  • $decodedData['all']数组($searchedAllId)中元素的ID;
  • 上述数组元素(channels)的$searchedChannelId列表中的通道ID;
  • 上述通道元素(servers)的$searchedServerId列表中的服务器ID。

如果您还有其他要求或看法,请详细地告诉我们,以便我们为您提供适合新情况的答案。

我认为您可以轻松地遵循我在代码中使用的步骤。为了简单起见,我没有对数组元素进行任何验证。

//...

$data = curl_exec($ch);

curl_close($ch);

$decodedData = json_decode($data, true);

//=========================================================
// This line is just for testing: display the decoded data.
//echo '<pre>' . print_r($decodedData, TRUE) . '</pre>';
//=========================================================
//
// Criterias to search for in the decoded data.
$searchedAllId = 1;
$searchedChannelId = 1;
$searchedServerId = 659;

// Holds the found secure url.
$foundSecureUrl = '';

// Search for the secure url based on the above criterias.
foreach ($decodedData['all'] as $allKey => $allItem) {
    if ($allItem['id'] === $searchedAllId) {
        foreach ($allItem['channels'] as $channelKey => $channelItem) {
            if ($channelItem['id'] === $searchedChannelId) {
                foreach ($channelItem['servers'] as $serverKey => $serverItem) {
                    if ($serverItem['id'] === $searchedServerId) {
                        $foundSecureUrl = $serverItem['secure_url'];

                        /*
                         * Secure url is now found.
                         * So break the further execution of all THREE foreach loops.
                         */
                        break(3);
                    }
                }
            }
        }
    }
}

// Display the found secure url.
echo 'Found secure url:<br/><br/>' . $foundSecureUrl;

$decodedData数组如下所示,在我将其短路并更改了其中的敏感数据之后:

Array
(
    [code] => 100
    [message] => get successfully
    [all] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [image] => some-image.png
                    [text] => Bein Sport
                    [order] => 1
                    [is_active] => 1
                    [created_at] => 2017-10-22 00:03:08
                    [updated_at] => 2018-05-08 16:22:53
                    [channels] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 1
                                    [name] => bein News
                                    [image] => some-image.png
                                    [is_active] => 1
                                    [is_feature] => 0
                                    [order] => 1
                                    [created_at] => 2017-10-22 00:03:51
                                    [updated_at] => 2018-01-18 21:22:45
                                    [pivot] => Array
                                        (
                                            [category_id] => 1
                                            [channel_id] => 1
                                        )

                                    [servers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 87
                                                    [title] => جوده عاليه
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 1
                                                    [server_type_id] => 2
                                                    [created_at] => 2017-10-22 11:48:56
                                                    [updated_at] => 2018-08-17 12:54:26
                                                    [secure_url] => some-secure-url
                                                )

                                            [1] => Array
                                                (
                                                    [id] => 89
                                                    [title] => جوده متوسطة (360)
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 1
                                                    [server_type_id] => 2
                                                    [created_at] => 2017-10-22 11:50:23
                                                    [updated_at] => 2018-08-17 12:54:35
                                                    [secure_url] => some-secure-url
                                                )

                                            ...

                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 2
                                    [name] => bein HD
                                    [image] => some-image.png
                                    [is_active] => 1
                                    [is_feature] => 0
                                    [order] => 2
                                    [created_at] => 2017-10-22 00:04:12
                                    [updated_at] => 2018-01-18 21:24:14
                                    [pivot] => Array
                                        (
                                            [category_id] => 1
                                            [channel_id] => 2
                                        )

                                    [servers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 90
                                                    [title] => جوده عاليه
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 2
                                                    [server_type_id] => 2
                                                    [created_at] => 2017-10-22 11:50:57
                                                    [updated_at] => 2018-08-17 12:54:43
                                                    [secure_url] => some-secure-url
                                                )

                                            [1] => Array
                                                (
                                                    [id] => 92
                                                    [title] => جوده متوسطة (360)
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 2
                                                    [server_type_id] => 2
                                                    [created_at] => 2017-10-22 11:52:03
                                                    [updated_at] => 2018-08-17 12:54:47
                                                    [secure_url] => some-secure-url
                                                )

                                            ...

                                        )

                                )

                            ...

                        )

                )

            [1] => Array
                (
                    [id] => 2
                    [image] => some-image.png
                    [text] => bein
                    [order] => 2
                    [is_active] => 1
                    [created_at] => 2017-10-22 12:53:17
                    [updated_at] => 2018-05-08 16:22:58
                    [channels] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 536
                                    [name] => BBC EARTH
                                    [image] => some-image.png
                                    [is_active] => 1
                                    [is_feature] => 0
                                    [order] => 0
                                    [created_at] => 2018-08-17 15:53:06
                                    [updated_at] => 2018-08-17 15:53:06
                                    [pivot] => Array
                                        (
                                            [category_id] => 2
                                            [channel_id] => 536
                                        )

                                    [servers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 1136
                                                    [title] => جودة عالية
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 536
                                                    [server_type_id] => 2
                                                    [created_at] => 2018-08-17 16:01:26
                                                    [updated_at] => 2018-08-17 16:01:26
                                                    [secure_url] => some-secure-url
                                                )

                                            [1] => Array
                                                (
                                                    [id] => 1137
                                                    [title] => جوده متوسطة
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 536
                                                    [server_type_id] => 2
                                                    [created_at] => 2018-08-17 16:01:58
                                                    [updated_at] => 2018-08-17 16:01:58
                                                    [secure_url] => some-secure-url
                                                )

                                            ...

                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 535
                                    [name] => BeIN Dlife
                                    [image] => some-image.png
                                    [is_active] => 1
                                    [is_feature] => 0
                                    [order] => 0
                                    [created_at] => 2018-08-17 15:52:15
                                    [updated_at] => 2018-08-17 15:52:15
                                    [pivot] => Array
                                        (
                                            [category_id] => 2
                                            [channel_id] => 535
                                        )

                                    [servers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 1133
                                                    [title] => جودة عالية
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 535
                                                    [server_type_id] => 2
                                                    [created_at] => 2018-08-17 16:00:13
                                                    [updated_at] => 2018-08-17 16:00:13
                                                    [secure_url] => some-secure-url
                                                )

                                            [1] => Array
                                                (
                                                    [id] => 1134
                                                    [title] => جودة متوسطة
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 535
                                                    [server_type_id] => 2
                                                    [created_at] => 2018-08-17 16:00:35
                                                    [updated_at] => 2018-08-17 16:00:35
                                                    [secure_url] => some-secure-url
                                                )

                                            ...

                                        )

                                )

                            ...
                        )

                )

            ...

        )

)