我正在为mySQL导入提取JSON数据,并在我的JSON文件的JSON解码语句之后设置变量我正在努力解决某些问题。我的变量设置如此
$name = $data['available_channels']['3']['name'];
和像这样的相关JSON
"available_channels": {
"3": {
"num": 152,
"name": "Sky Sports 3",
"stream_type": "live",
"type_name": "Live Streams",
"stream_id": "3",
"stream_icon": "http://www.tv-logo.com/pt-data/uploads/images/logo/sky_uk_sports3.jpg",
"epg_channel_id": "Sky Sports 3",
"added": "0",
"category_name": "Sports UK",
"category_id": "5",
"series_no": null,
"live": "1",
"container_extension": null,
"custom_sid": ":0:86:EEE:7F2:2:11A0000:0:0:0:",
"tv_archive": 0,
"direct_source": "",
"tv_archive_duration": 0
},
我的问题是服务的每个频道都以新号码开头。所以我需要把我的变量拉进去,
$name = $data['available_channels']['ANY VALUE HERE']['name'];
任何想法?我知道这一定很简单,我在这里有一个金发的时刻
由于
更新1
//convert json object to php associative array
$data = json_decode($jsondata, true);
//get the values and asign variables
$name = $data['available_channels']['3']['name'];
更新2
现在完整代码
$data = json_decode($jsonFile, true);
for($i = 0; $i <= count($data['available_channels']); $i++){
$name = $data['available_channels'][$i]['name'];
$num = $data['available_channels'][$i]['num'];
$epg_channel_id = $data['available_channels'][$i]['epg_channel_id'];
$category_name = $data['available_channels'][$i]['category_name'];
$stream_icon = $data['available_channels'][$i]['stream_icon'];
//insert into mysql table
$sql = "INSERT INTO channels(name, num, epg_channel_id, category_name, stream_icon)
VALUES('$name', '$num', '$epg_channel_id', '$category_name', '$stream_icon')";
if(!mysql_query($sql,$con))
{
die('Error : ' . mysql_error());
}
}
&#13;
获取234行中的117行,但其他行是空白的...任何想法
答案 0 :(得分:1)
$name = array_values($data['available_channels'])[0]['name'];
array_values
返回一个新数组,该数组由源数组的值组成,从0
重新编号。
答案 1 :(得分:0)
您可以计算数组内的通道数,然后遍历每个通道。
for($i = 0; $i <= count($data['available_channels']); $i++) {
$name = $data['available_channels'][$i]['name'];
// TODO: Do something with the name
}
或者你可以做@cske说的话:
$channels = [];
foreach($data['available_channels'] as $k => $v) {
// this will keep the key's the same
$channels[$k] = $v['name'];
}
或者您可以选择更加OO风格的方法,看它在3v4l工作:
class Channel {
private $_data;
public function __construct($json) {
$this->_data = json_decode($json, true);
}
public function parse() {
$parsed = [];
foreach($this->_data['available_channels'] as $k => $v) {
$parsed[$k] = $v['name'];
}
$this->_data['available_channels'] = $parsed;
return $this;
}
public function getByKey($key) {
return $this->_data['available_channels'][$key];
}
}
$c = new Channel($json_response);
echo 'Name for Channel 3 is: ' . $c->parse()->getByKey('3');
答案 2 :(得分:0)
因此,在更新2之后,这将起作用
foreach($data['available_channels'] as $d) {
$name = $d['name'];
$num = $d['num'];
$epg_channel_id = $d['epg_channel_id'];
$category_name = $d['category_name'];
$stream_icon = $d['stream_icon'];
//insert into mysql table
$sql = "INSERT INTO channels(name, num, epg_channel_id, category_name, stream_icon)
VALUES('$name', '$num', '$epg_channel_id', '$category_name', '$stream_icon')";
if (!mysql_query($sql, $con)) {
die('Error : ' . mysql_error());
}
}
或者如果使用for循环@KDOT答案,但由@Niet更正Dark Absol代码
$data['available_channels'] = array_values($data['available_channels']);
for($i = 0; $i <= count($data['available_channels']); $i++) {
$name = $data['available_channels'][$i]['name'];
...
}
如果一个php数组有count()= 5然后它的键是0,1,2,3,4那么KDOT的答案会丢失行,那就不是真的