从php数组中按值提取行

时间:2015-04-28 12:37:18

标签: php arrays

这是我的阵列:

Array (
    [0] => Array ( [SocketID] => 1 [SocketName] => Name [SocketDecimal] => 0 [SocketHex] => 00 [SocketAtt] => 1 [Category] => 1 [Value] => 100 [Procentage] => 0 ) 
    [1] => Array ( [SocketID] => 2 [SocketName] => Name2 [SocketDecimal] => 50 [SocketHex] => 32 [SocketAtt] => 1 [Category] => 1 [Value] => 800 [Procentage] => 0 ) 
    [2] => Array ( [SocketID] => 3 [SocketName] => Name3 [SocketDecimal] => 100 [SocketHex] => 64 [SocketAtt] => 1 [Category] => 1 [Value] => 60 [Procentage] => 0 )
)

如何按SocketDecimal提取行?

例如:我想在SocketDecimal = 50中提取行,并仅使用该行创建一个新数组。

6 个答案:

答案 0 :(得分:2)

foreach($array as $entry) {
    if($entry['SocketDecimal'] == 50)
        $newArr[] = $entry;
}

$newArr将包含所需的"行"。当然,您可以根据哪个"行"来操作if语句。 (我只是称之为数组条目)你要提取。

答案 1 :(得分:1)

这不是大数据的最佳方式!深度多阵列很容易。

$arr = array(
array('socket_id'=>1,'name'=>'test1'),
array('socket_id'=>2,'name'=>'test2'),
array('socket_id'=>3,'name'=>'test3'),
array('socket_id'=>2,'name'=>'test4')
);

$newArr = array();
foreach($arr as $row){
    foreach($row as $key=>$r){
        if($key == 'socket_id' && $r==2)
        $newArr[] = $row;
    }
}
print_r($newArr);

答案 2 :(得分:1)

$result = array();
foreach($input as $i){
   if($i['SocketDecimal']==50)
      $result[]=$i;
}

答案 3 :(得分:1)

你可以通过这种方法来实现

foreach ($yourarray as $key => $value){
    $newarray = array("SocketDecimal"=>$value["SocketDecimal"];
}

print_r($newarray);

答案 4 :(得分:1)

If your result array is like given below
$arr = array(
            array( 'SocketID' => 1, 'SocketName' => 'Name', 'SocketDecimal' => 0, 'SocketHex' => 0, 'SocketAtt' => 1, 'Category' => 1, 'Value' => 100, 'Procentage' => 0 ),
            array ( 'SocketID' => 2, 'SocketName' => 'Name2', 'SocketDecimal' => 50, 'SocketHex' => 32, 'SocketAtt' => 1, 'Category' => 1, 'Value' => 800, 'Procentage' => 0 ), 
            array ( 'SocketID' => 3, 'SocketName' => 'Name3', 'SocketDecimal' => 100, 'SocketHex' => 64, 'SocketAtt' => 1, 'Category' => 1, 'Value' => 60, 'Procentage' => 0 )
            );

print_r($arr);

Get row for SocketDecimal=50 by following loop:
<pre>
$resultArr = '';
foreach($arr as $recordSet)
{
    if($recordSet['SocketDecimal'] == 50)
    {
        $resultArr[] = $recordSet;
        break;
    }   
}
</pre>
print_r($resultArr);

break foreach loop so that it will not traverse for all the array when SocketDecimal(50) founded. 

答案 5 :(得分:0)

您可以使用array_column + array_search组合

$array = Array (
            "0" => Array ( "SocketID" => 1, "SocketName" => "Name", "SocketDecimal" => 0, "SocketHex" => 00, "SocketAtt" => 1, "Category" => 1, "Value" => 100, "Procentage" => 0 ) ,
            "1" => Array ( "SocketID" => 2, "SocketName" => "Name2", "SocketDecimal" => 50, "SocketHex" => 32, "SocketAtt" => 1, "Category" => 1, "Value" => 800, "Procentage" => 0 ),
            "2" => Array ( "SocketID" => 3, "SocketName" => "Name3", "SocketDecimal" => 100, "SocketHex" => 64, "SocketAtt" => 1, "Category" => 1, "Value" => 60 ,"Procentage" => 0 )
        );

var_dump($array[array_search(50,array_column($array,'SocketDecimal'))]);