Skip to content
Advertisement

Extract a row by value from php array

This is my 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 )
)

How can I extract a row by SocketDecimal?

For example: I want to extract row where SocketDecimal = 50 and make new an array only with that row.

Advertisement

Answer

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

$newArr will contain the desired “row”. Of course you can manipulate the if-statement depending on which “row” (I’d just call it array entry) you want to extract.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement