1

配列を使用して配列内の値を検索し、値が含まれる完全な配列を取得して配列に追加しようとしています。以下は、値を取得する配列です。

Array (
    [0] => Array ( [ID] => 138 [dimmer] => 5 [order] => 1 [double] => 0 [location1] => DSR [location2] => Stage Pockets ) 
    [1] => Array ( [ID] => 139 [dimmer] => 6 [order] => 1 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
    [2] => Array ( [ID] => 140 [dimmer] => 7 [order] => 2 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
    [3] => Array ( [ID] => 141 [dimmer] => 8 [order] => 2 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
)

dimmer以下の検索機能を使用して値を取得しようとしています。

function search($array, $key, $value)
{
  $results = array();

  if (is_array($array))
  {
      if (isset($array[$key]) && $array[$key] == $value)
          $results[] = $array;

      foreach ($array as $subarray)
          $results = array_merge($results, search($subarray, $key, $value));
  }

  return $results;
}

以下で$chanは、整数である値を使用して、上記の関数を使用して配列を検索します。次にforeach、 は配列を通過し、$patch上記の配列から配列を選択することになっています (空の配列のみを返します$patch_single['dimmer']) "7"

  $patch = search($patch, 'Channel', $chan);
  foreach ($patch as $patch_single) {
    print_r($patch_single);
    $dim_single = intval($patch_single['dimmer']);
    echo $dim_single;
    $dimmers = search($dimmers, 'dimmer', $dim_single);
  }

取得するために使用されている配列$patch_single['dimmer']は、内部では次のforeachとおりです。

Array ( [ID] => 241 [Channel] => 100 [dimmer] => 7 )
Array ( [ID] => 242 [Channel] => 100 [dimmer] => 25 )

アドバイスありがとうございます。

4

2 に答える 2

0

これを試して :

$arr       = Array (Array ( "ID" => 138, "dimmer" => 5, "order" => 1, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ), 
                    Array ( "ID" => 139, "dimmer" => 6, "order" => 1, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
                    Array ( "ID" => 140, "dimmer" => 7, "order" => 2, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
                    Array ( "ID" => 141, "dimmer" => 8, "order" => 2, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ));

$arr       = array_filter($arr, function($ar) {
   return ($ar['dimmer'] == '7' );
});
echo "<pre>";
print_r($arr);

出力:

Array
(
    [2] => Array
        (
            [ID] => 140
            [dimmer] => 7
            [order] => 2
            [double] => 0
            [location1] => DSR
            [location2] => Stage Pockets
        )

)

参照:http://php.net/manual/en/function.array-filter.php

于 2013-02-26T04:44:30.730 に答える
0

うーん、2次元配列があるようです。なぜこれを使わないのですか?

foreach($array as $row) { 
    if ($row['dimmer'] == $myValue) { $newArray[] = $row; } 
}
于 2013-02-26T04:11:47.893 に答える