-1

それらの値(id、date、latlng、transport)を含むいくつかの行を持つ配列があります。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
    }
}

2番目の配列を作成し、その中の適切な行をコピーしてから、最初の配列を置き換えることで、なんとか機能させることができました。しかし、おそらくもっと良い解決策があるでしょう?

4

4 に答える 4

1

foreach ループに $i はありません。

foreach($json_a['data']['entrees'] as $entryKey => $entry){
    if($entry['transport']!=""){
        unset($json_a['data']['entrees'][$entryKey]);
    }
}
于 2012-06-14T11:32:53.083 に答える
0

PHP array_spliceを使用しています。

配列の一部を削除し、別のものに置き換えます

配列から 1 つのエントリを削除するには、次の場所でPHP unsetを使用する必要があります。

指定された変数の設定を解除します。

したがって、コードは次のようになります。

<?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]);
    }
}
?>

テストケース:

// Sample Array with 4 entries
$json_a = array(
    "data" => array (
        "entrees" => array(
            array (
                "id"        => 14,
                "date"      => '2012-06-14',
                "latlng"    => '14.000000, -9.00000',
                "transport" => ''
            ),
            array(
                "id"        => 13,
                "date"      => '2012-06-14',
                "latlng"    => '13.000000, -8.00000',
                "transport" => '12'
            ),
            array(
                "id"        => 12,
                "date"      => '2012-06-14',
                "latlng"    => '12.000000, -7.00000',
                "transport" => '45'
            ),
            array(
                "id"        => 11,
                "date"      => '2012-06-14',
                "latlng"    => '11.000000, -6.00000',
                "transport" => ''
            )
        )
    )
);

制御出力:

var_dump($json_a);

出力:

array(1) { ["data"]=> array(1) { ["entrees"]=> array(4) { [0]=> array(4) { ["id"]=> int(14) ["date"]=> string(10) "2012-06-14" ["latlng"]=> string(19) "14.000000, -9.00000" ["transport"]=> string(0) "" } 1 => array(4) { ["id"]=> int(13) ["date"]=> string(10) "2012-06-14" ["latlng"]=> string(19) "13.000000, -8.00000" ["トランスポート"]=> 文字列(2) "12" } 2 => 配列(4) { ["id"]=> int(12) ["日付"]=> 文字列(10) "2012 -06-14" ["latlng"]=> string(19) "12.000000, -7.00000" ["transport"]=> string(2) "45" } [3]=> array(4) { ["id"]=> int(11) ["date"]=> string(10) "2012-06-14" ["latlng"]=> string(19) "11.000000, -6.00000" ["transport"]= > 文字列(0) "" } } }

サイクルを実行します。

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);

出力:

array(1) { ["data"]=> array(1) { ["entrees"]=> array(2) { [0]=> array(4) { ["id"]=> int(14) ["date"]=> string(10) "2012-06-14" ["latlng"]=> string(19) "14.000000, -9.00000" ["transport"]=> string(0) "" } [ 3]=> array(4) { ["id"]=> int(11) ["date"]=> string(10) "2012-06-14" ["latlng"]=> string(19) " 11.000000, -6.00000" ["トランスポート"]=> 文字列(0) "" } } }

于 2012-06-14T11:45:46.670 に答える
0

これを試して:

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]);
    }
}
于 2012-06-14T11:28:31.867 に答える
0
    for($i=0;$i<count($json_a[data][entrees]);$i++)
    {
        if($json_a[data][entrees][$i][transport]=="")
        {
            unset($json_a[data][entrees][$i]);
        }
    }

print_r($json_a[data][entrees])

お役に立てれば!

于 2012-06-14T11:23:52.147 に答える