0

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.

10
  • 2
    What do you mean by "extract" , echo ? create a new array ? write to a file ? Commented Apr 28, 2015 at 12:38
  • 1
    Edit your question with what your desired output should look like. Commented Apr 28, 2015 at 12:41
  • 1
    @Rizier123 I think he wants it the other way. I think he wants to have the entire record where socketdecimal = 50. Commented Apr 28, 2015 at 12:45
  • 1
    @Rizier123 Even though I agree 100% with you that this is guesswork. I think it makes the most sense since I dont think he wants all the socketdecimal values where the value =50. Wouldn't make sense unless he wants to count the amount of values where value = 50 which still can be done in a better way. Commented Apr 28, 2015 at 12:51
  • 1
    @MeTa The better way was about if you wanted just the value==50 returned but according to your accepted answer, you dont want that and treegardens answer is the right answer. Commented Apr 28, 2015 at 12:58

6 Answers 6

3
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.

Sign up to request clarification or add additional context in comments.

1 Comment

(If I'm right)I think this answer is the only one that understands the question. +1
2

You can use array_column + array_search combo

$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'))]);

Comments

1

It's not the best way for big data! It's easy for deep multiarrays.

$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);

Comments

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

Comments

1

You can do it by this method

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

print_r($newarray);

Comments

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. 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.