我使用json_decode
來解析JSON文件。在for循環中,我試圖捕獲存在一個或另一個元素的JSON中的特定情況。我已經實現了一個似乎符合我需求的函數,但是我發現我需要使用兩個for循環來獲取它以捕獲我的兩個案例。確保訂單循環涉及json_decode()
我寧願使用一個循環,如果可能的話,但我堅持如何在一次傳遞中捕獲這兩種情況。下面是我想結果看起來像什麼樣機:
<?php
function extract($thisfile){
$test = implode("", file($thisfile));
$obj = json_decode($test, true);
for ($i = 0; $i <= sizeof($obj['patcher']['boxes']); $i ++) {
//this is sometimes found 2nd
if ($obj['patcher']['boxes'][$i]['box']['name'] == "mystring1") {
}
//this is sometimes found 1st
if ($obj['patcher']['boxes'][$i]['box']['name'] == "mystring2") {
}
}
}
?>
誰能告訴我,我怎麼能抓到一個迭代中上述兩種情況? 我顯然不能這樣做
if ($obj['patcher']['boxes'][$i]['box']['name'] == "string1" && $obj['patcher']['boxes'][$i]['box']['name'] == "string2") {}
...因爲這條件永遠不會得到滿足。
但你*使用*僅單迴路/傳...另外,'file' /'implode'是不必要的:只需使用'file_get_contents'。 – Jon 2014-11-14 23:18:23
我只在示例中使用了單個傳遞來說明我希望結果如何。 – jml 2014-11-14 23:18:51
你寫的是什麼,不行? – Aesphere 2014-11-14 23:27:04