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

嗯,我看到你有二维数组。为什么你不使用这个?

foreach($array as $row) { 
    if ($row['dimmer'] == $myValue) { $newArray[] = $row; } 
}
于 2013-02-26T04:11:47.893 回答
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 回答