我有以下字符串:
Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone_break_
我是使用DOM从HTML页面的cURL获得的。
我的最终结果必须是JSON文件,如:
!important
但我知道首先我必须拆分字符串,我试过这个
Shop 1: { name: "example", location: "example", telephone: "0123"}
Shop 2: { name: "example", location: "example", telephone: "0123"}
但它不起作用。有人能帮助我吗?
答案 0 :(得分:1)
你正在爆炸错误的价值:
Name_Location_Telephone_break_Name_Location_Telephone etc...
将成为一个数组:
0 => 'Name_Location_Telephone_'
1 => '_Name_Location_Telephone_'
2 => '_Name_Location_Telephone_'
etc...
当您分解这些内容时,第一个会在索引Name
处有0
,但在索引1
处会有所有后续爆炸。
你应该在_break_
上爆炸。
如果您已完成任何基本调试,例如var_dump($shops)
和var_dump($values)
,您就已经看到了事情的变化。
答案 1 :(得分:1)
1)你的第一次爆炸会保留一些修剪下划线。
2)您必须使用json_encode方法将数组转换为json数据
您可以做的一个例子
$result = "Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone_break_";
$shops = explode("_break_",$result);
foreach($shops as $key => $shop) {
$res["Shop ".$key] = explode('_', $shop);
}
$jsonData = json_encode($res);
答案 2 :(得分:0)
这并不是你如何编写foreach的。它与Java不同。 结帐:http://php.net/manual/en/control-structures.foreach.php示例,并且从现在开始不要犹豫(thei PHP API非常好)
答案 3 :(得分:0)
你几乎接近你所要求的结果。尝试以下
$shops = explode("_break_",$result);
$jsonArray = array();
foreach ($shops as $shop){
// three elements are same in response no need to worry just explode in array of three elements.
$value = explode("_", $shop);
// assign prepared array or print it according to your business logic.
$jsonArray[] = array ('name'=>$value[0], 'location'=>$value[1], 'telephone'=>$value[2]);
}
//here you can encode your final multi dimensional array to json string.
echo json_encode($jsonArray);
答案 4 :(得分:0)
首次爆炸后,您需要修剪' _'在$ shops数组中的最后一个字符串。
<?php
$result = "Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone";
$shops = explode("break",$result);
echo json_encode($shops)."\n";
$values = array();
foreach ($shops as $shop){
$values = explode("_", trim($shop, '_'));
echo json_encode($values)."\n";
$name = $values[0];
$location = $values[1];
$tel = $values[2];
echo $name."\n";
}
?>
结果是,
["Name_Location_Telephone_","_Name_Location_Telephone_","_Name_Location_Telephone_","_Name_Location_Telephone"]
["Name","Location","Telephone"]
Name
["Name","Location","Telephone"]
Name
["Name","Location","Telephone"]
Name
["Name","Location","Telephone"]
Name
答案 5 :(得分:0)
它没有用,因为你的第一次爆炸会保留一些修剪下划线。
要么
$shops = explode("_break_",$result);
或
$shops = trim(explode("break",$result), "_");
答案 6 :(得分:0)
试试这个:
$result = "Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone";
$shops = explode("_break_",$result);
$results = array();
$index = 1;
foreach ($shops as $shop) {
$values = explode("_", $shop);
$results["Shop $index"] = [
'name' => $values[0],
'location' => $values[1],
'telephone' => $values[2],
];
$index++;
}
print_r($results);
结果是:
Array
(
[Shop 1] => Array
(
[name] => Name
[location] => Location
[telephone] => Telephone
)
[Shop 2] => Array
(
[name] => Name
[location] => Location
[telephone] => Telephone
)
[Shop 3] => Array
(
[name] => Name
[location] => Location
[telephone] => Telephone
)
[Shop 4] => Array
(
[name] => Name
[location] => Location
[telephone] => Telephone
)
)
你有空闲&#34; _&#34;在explode
仅break
之后,这就是结果可能出错的原因。