PHP如何根据指定ID从JSON频道数据中匹配获取对应链接?
PHP根据频道ID匹配对应link字段实现方案
你可以根据自己的查询频次选择下面两种实现方式:
方法1:遍历匹配(适合单次/少量查询场景)
直接循环解码后的频道列表,匹配目标ID后返回对应链接即可,逻辑简单无额外开销:
<?php // 原有请求结果定义保持不变 $content = <<<END_OF_STRING [{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}] END_OF_STRING; $items = json_decode($content); /** * 按频道ID查找对应播放链接 * @param array $channelList json解码后的频道对象数组 * @param string $targetId 待查找的频道ID * @return string|null 匹配成功返回链接,失败返回null */ function getLinkByChannelId(array $channelList, string $targetId): ?string { foreach ($channelList as $channel) { // 原数据id为字符串类型,用全等匹配避免类型转换导致的匹配错误 if ($channel->id === $targetId) { return $channel->link; } } return null; } // 调用示例 $targetId = "1858"; $channelLink = getLinkByChannelId($items, $targetId); if ($channelLink) { echo "ID {$targetId} 对应频道链接:" . $channelLink . PHP_EOL; } else { echo "未找到ID为 {$targetId} 的频道" . PHP_EOL; }
方法2:ID索引映射(适合多次查询场景)
如果需要频繁根据ID查询频道信息,可以先把列表重组为以ID为键的映射表,后续查询直接按键取值,时间复杂度为O(1),性能更高:
<?php // 原有请求结果定义、解码逻辑保持不变 $content = <<<END_OF_STRING [{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}] END_OF_STRING; $items = json_decode($content); // 构建ID映射表 $channelMap = []; foreach ($items as $channel) { $channelMap[$channel->id] = $channel; } // 后续多次查询无需再循环 echo "MBC1链接:" . $channelMap["2972"]->link . PHP_EOL; echo "MBC3链接:" . $channelMap["1859"]->link . PHP_EOL; // 查找不存在的ID前先判断键是否存在,避免抛出错误 $nonExistentId = "9999"; if (isset($channelMap[$nonExistentId])) { echo $channelMap[$nonExistentId]->link; } else { echo "指定ID的频道不存在" . PHP_EOL; }
注意事项
如果你更习惯数组操作,可以在调用
json_decode时传入第二个参数true,将返回结果转为关联数组格式,对应字段访问方式从$channel->id改为$channel['id']即可,其余逻辑完全一致。
原JSON数据中id字段为字符串类型,做匹配时不要直接用整数ID做全等判断,否则会匹配失败。
内容的提问来源于stack exchange,提问作者achraf
相关产品推荐
相关产品推荐

