php使用循环取消设置
为什么我不能用这个代码得到我想要的结果php使用循环取消设置,php,for-loop,Php,For Loop,为什么我不能用这个代码得到我想要的结果 for($i=0; $i<count($results); $i++){ if($results[$i]->high == $results[$i]->open and $results[$i]->low == $results[$i]->close and $results[$i]->vol == 0){
for($i=0; $i<count($results); $i++){
if($results[$i]->high == $results[$i]->open and $results[$i]->low == $results[$i]->close and $results[$i]->vol == 0){
unset($results[$i]);
}
echo json_encode($results);
for($i=0;$ihigh==$results[$i]->open和$results[$i]->low==$results[$i]->close和$results[$i]->vol==0){
未设置($results[$i]);
}
echo json_编码($results);
为什么我仍然得到相同的结果,好像循环中的unset不工作?但是,如果我使用您提供的示例json数据调用循环内的echo,那么我添加了一个与if中的条件匹配的
伪记录,因此它将取消设置,如下所示:
<?php
$json = '[
{
"code":"TLKM",
"_date":"2020-11-09",
"_time":"09:17:00",
"open":2890,
"high":2890,
"low":2880,
"close":2890,
"vol":260100
},
{
"code":"TLKM",
"_date":"2020-11-09",
"_time":"09:18:00",
"open":2880,
"high":2890,
"low":2880,
"close":2880,
"vol":288300
},
{
"code":"DUMMIE",
"_date":"2020-11-09",
"_time":"09:19:00",
"open":2890,
"high":2890,
"low":2880,
"close":2880,
"vol":0
},
{
"code":"TLKM",
"_date":"2020-11-09",
"_time":"09:19:00",
"open":2890,
"high":2890,
"low":2880,
"close":2890,
"vol":3070200
}
]';
// turn json into associative array
$arr = json_decode($json, true);
// process the array
foreach ($arr as $key => $value) {
if ($arr[$key]['high'] == $arr[$key]['open'] and $arr[$key]['low'] == $arr[$key]['close'] and $arr[$key]['vol'] == 0) {
unset($arr[$key]);
}
}
// array back to json format
$json = json_encode($arr);
你想要什么结果?您的问题不是很清楚您实际想做什么。您能否给我们一个$results
的示例,以便我们可以复制并可能帮助找到解决方案?顺便说一句,你没有为
loop.hi@berend关闭}
的}
结果有点像这样{“代码”:“TLKM”,“日期”:“2020-11-09”,“时间”:“09:17:00”,“打开”:2890,“高”:2890,“低”:2880,“关闭”:2890,“vol”:260100}{“代码”:“TLKM”,“日期”:“2020-11-09”,“时间”:“09:18:00”,“打开”:2880,“高”:2890,“低”:2880,“关闭”:260100,“vol 300}{“代码”:“TLKM”,“_date”:“2020-11-09”,“_time”:“09:19:00”,“open”:2890,“high”:2890,“low”:2880,“close”:2890,“vol”:3070200}我想使用for循环删除其中一些。但是如果我在循环外调用结果,它看起来像是代码异步工作的,因此结果显示原始结果,而不显示unset
{
"0": {
"code": "TLKM",
"_date": "2020-11-09",
"_time": "09:17:00",
"open": 2890,
"high": 2890,
"low": 2880,
"close": 2890,
"vol": 260100
},
"1": {
"code": "TLKM",
"_date": "2020-11-09",
"_time": "09:18:00",
"open": 2880,
"high": 2890,
"low": 2880,
"close": 2880,
"vol": 288300
},
"3": {
"code": "TLKM",
"_date": "2020-11-09",
"_time": "09:19:00",
"open": 2890,
"high": 2890,
"low": 2880,
"close": 2890,
"vol": 3070200
}
}