Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 删除条目等于空字符串的每一行_Php_Arrays - Fatal编程技术网

Php 删除条目等于空字符串的每一行

Php 删除条目等于空字符串的每一行,php,arrays,Php,Arrays,我有一个数组,其中有几行包含这些值(id、date、latlng、transport)。我想在传输“”时删除整行 我尝试了while、for和foreach,但问题是:当我找到多个要删除的条目时,“$I”与相应的行不再匹配 for($i=0;$i<count($json_a[data][entrees]);$i++) { if($json_a[data][entrees][$i][transport]!="") { array_splice($json_a[

我有一个数组,其中有几行包含这些值(id、date、latlng、transport)。我想在传输“”时删除整行

我尝试了while、for和foreach,但问题是:当我找到多个要删除的条目时,“$I”与相应的行不再匹配

for($i=0;$i<count($json_a[data][entrees]);$i++)
{
    if($json_a[data][entrees][$i][transport]!="")
    {
        array_splice($json_a[data][entrees],$i,1);
        //first removal is OK. Second won't scope the good row
    }
}
对于($i=0;$i
对于($i=0;$i请尝试以下操作:

foreach( $json_a['data']['entrees'] as $key => $entry ){
    if( $entry['transport'] != "" ){
        array_splice($json_a['data']['entrees'], $key, 1);
    }else{
        unset($json_a['data']['entrees'][$key]);
    }
}

foreach循环中没有$i

foreach($json_a['data']['entrees'] as $entryKey => $entry){
    if($entry['transport']!=""){
        unset($json_a['data']['entrees'][$entryKey]);
    }
}
您正在使用的内容如下:

移除阵列的一部分并用其他内容替换它

要从数组中删除一个条目,应在其读取位置使用:

取消设置给定变量

因此,您的代码应该是:

<?php
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
    if($json_a[data][entrees][$i][transport]!="")
    {
        //remove this entry from the array
        unset($json_a[data][entrees][$i]);
    }
}
?>
var_dump($json_a);
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
    if($json_a[data][entrees][$i][transport]!="")
    {
        //remove this entry from the array
        unset($json_a[data][entrees][$i]);
    }
}
var_dump($json_a);
控制输出:

<?php
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
    if($json_a[data][entrees][$i][transport]!="")
    {
        //remove this entry from the array
        unset($json_a[data][entrees][$i]);
    }
}
?>
var_dump($json_a);
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
    if($json_a[data][entrees][$i][transport]!="")
    {
        //remove this entry from the array
        unset($json_a[data][entrees][$i]);
    }
}
var_dump($json_a);
输出:

<?php
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
    if($json_a[data][entrees][$i][transport]!="")
    {
        //remove this entry from the array
        unset($json_a[data][entrees][$i]);
    }
}
?>
var_dump($json_a);
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
    if($json_a[data][entrees][$i][transport]!="")
    {
        //remove this entry from the array
        unset($json_a[data][entrees][$i]);
    }
}
var_dump($json_a);
数组(1){[“数据”]=>数组(1){[“主菜”]=>数组(4){[0]=>数组(4){[“id”]=>int(14)[“日期”=>string(10)“2012-06-14”[“latlng”]=>string(19)“14.000000,-9.00000”[“传输”]=>string(0)”}=>数组(4){“id”=>int(13)[“日期”=>string(10)“2012-06-14”[“latlng”=>string(19.00000)”传输”[.00000}=>数组(4){[“id”]=>int(12)[“date”]=>string(10)“2012-06-14”[“latlng”]=>string(19)“12.000000,-7.00000”[“transport”]=>string(2)“45”}[3]=>数组(4){[“id”]=>int(11)[“date”]=>string(10)“2012-06-14”[“latlng”=>string(19)“11.000000,-60.00000”[“transport”=>string(0)”}}}}}

运行循环:

<?php
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
    if($json_a[data][entrees][$i][transport]!="")
    {
        //remove this entry from the array
        unset($json_a[data][entrees][$i]);
    }
}
?>
var_dump($json_a);
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
    if($json_a[data][entrees][$i][transport]!="")
    {
        //remove this entry from the array
        unset($json_a[data][entrees][$i]);
    }
}
var_dump($json_a);
输出:

<?php
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
    if($json_a[data][entrees][$i][transport]!="")
    {
        //remove this entry from the array
        unset($json_a[data][entrees][$i]);
    }
}
?>
var_dump($json_a);
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
    if($json_a[data][entrees][$i][transport]!="")
    {
        //remove this entry from the array
        unset($json_a[data][entrees][$i]);
    }
}
var_dump($json_a);
数组(1){[“数据”]=>数组(1){[“主菜”]=>数组(2){[0]=>数组(4){[“id”]=>int(14)[“日期”]=>string(10)“2012-06-14”[“latlng”]=>string(19)“14.000000,-9.00000”[“传输”]=>string(0)”[3]=>数组(4){[“id”=>int(11)[“日期”=>string(10)“2012-06-14”[“latlng”=>string(19)”传输”[.00000}}}}}


谢谢,这确实是一个更好的解决方案。我以为unset的行为与splice相同。我错了。我知道。我在“each”循环中使用了一个外部$I,然后是$I++。我知道,这很难看。好吧。试着用unset代替array_splice.unset($json_a['data']['entres][$I]);