4

次のような配列形式があります。

Array
(
    [Australia] => Array
        (
            [0] => [1990,0.01],
            [1] => [1991,0.02],
            [2] => [1992,0.02],
            [3] => [1993,0.02],
            [4] => [1994,0.02],
            [5] => [1995,0.02],
            [6] => [1996,0.02],
            [7] => [1997,0.02],
            [8] => [1998,0.02],
            [9] => [1999,0.02],
            [10] => [2000,0.02],
            [11] => [2001,0.02],
            [12] => [2002,0.02],
            [13] => [2003,0.02],
            [14] => [2004,0.02],
            [15] => [2005,0.02],
            [16] => [2006,0.02],
            [17] => [2007,0.02],
            [18] => [2008,0.02],
            [19] => [2009,empty],
            [20] => [2010,empty],
            [21] => [2011,empty],
            [22] => [2012,empty],
            [23] => [2013,empty],
            [24] => [2014,empty],
            [25] => [2015,empty]
        )

    [Pakistan] => Array
        (
            [0] => [1990,0.00],
            [1] => [1991,0.00],
            [2] => [1992,0.00],
            [3] => [1993,0.00],
            [4] => [1994,0.00],
            [5] => [1995,0.00],
            [6] => [1996,0.00],
            [7] => [1997,0.00],
            [8] => [1998,0.00],
            [9] => [1999,0.00],
            [10] => [2000,0.00],
            [11] => [2001,0.00],
            [12] => [2002,0.00],
            [13] => [2003,0.00],
            [14] => [2004,0.01],
            [15] => [2005,0.01],
            [16] => [2006,0.00],
            [17] => [2007,0.00],
            [18] => [2008,0.00],
            [19] => [2009,empty],
            [20] => [2010,empty],
            [21] => [2011,empty],
            [22] => [2012,empty],
            [23] => [2013,empty],
            [24] => [2014,empty],
            [25] => [2015,empty]
        )
)

そして、配列構造と要素の位置を変更せずに「空」0に置き換えたい。やり方をくっつけた..

4

3 に答える 3

5

array_walk_recursive関数を使用できます。

function replace_empty(&$item, $key) {
    $item = str_replace('empty', '0', $item);
}

array_walk_recursive($your_array, 'replace_empty');
于 2013-04-04T10:13:44.923 に答える
2

array_walk_recursiveに置き換えるコールバック関数で関数をempty使用できます0


たとえば、配列が次のように宣言されているとします。

$myArray[0] = array(23, empty, 43, 12);
$myArray[1] = array(empty, empty, 53, 19);

注:タイプミスをしたと思います。配列には文字列だけではなく、いくつかのサブ要素が含まれています。


この種のコードを使用できます:

array_walk_recursive($myArray, 'replacer');
var_dump($myArray);


次のコールバック関数を使用:

function replacer(& $item, $key) {
    if ($item === empty) {
        $item = 0;
    }
}

ご了承ください :

  • 最初のパラメータは参照によって渡されます!
    • つまり、それを変更すると、配列内の対応する値が変更されます
  • ===比較に演算子を使用しています


そして、次の出力が得られます。

array(
  0 => 
    array
      0 => int 23
      1 => int 0
      2 => int 43
      3 => int 12
  1 => 
    array
      0 => int 0
      1 => int 0
      2 => int 53
      3 => int 19)
于 2013-04-04T10:20:04.050 に答える
0

私はforeach両方のインデックスで(テストされていません):

foreach($array as $country){
  foreach($country as &$field){
    if($field[1] == 'empty'){
      $field[1] = 0;
    }
  }
}

empty文字列だと思います)

編集:

これ[1990,0.00]が配列ではなく文字列の場合は、代わりに str_replace を使用できます

foreach($array as $country){
  foreach($country as &$field){
    $field = str_replace('empty', '0.00', $field);
    }
  }
}
于 2013-04-04T10:07:44.823 に答える