0

I have this $record array

array(4) { 
[0]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "904" } 
[1]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "903" } 
[2]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "902" } 
[3]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "901" } 
} 

How can i manipulate it so it will become like this?

array("901","902","903","904");

Thanks in advance

3 Answers 3

5
$subfunctionIds = array();

foreach($record as $values) {
   $subFunctionIds[] = $values['SUBFUNCTION_ID'];
}

// If you want them reversed like in your example output...
$subFunctionIds = array_reverse($subFunctionIds);

var_dump($subFunctionIds);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alex! I cant believe it is that simple :D
2
    function fetch($row) {
       return $row["SUBFUNCTION_ID"];
    }
    $result = array_map("fetch", $record);
    sort($result);
    var_dump($result);

in 5.3+ you could do better:

    $result = array_map(function ($row) { return $row["SUBFUNCTION_ID"]; }, $record);
    sort($result);
    var_dump($result);

3 Comments

The anonymous function is indeed much clearer - however, pre 5.3 it is kind of ugly (having to define a named function) :P
@alex you could use create_function() in pre-5.3, but I don't want to stuff too much into one answer :)
Yeah, but it is not very pretty either. Looks just like a wrapper for eval(). +1 for array_map and anonymous function.
0

Try doing this:

foreach ($array as $row ) {   
  $response[] = $row["SUBFUNCTION_ID"]; 
}
print_r($response);

3 Comments

That will have $response equal to the last variable.
you're missing the [] in $response[] = $row['SUBFUNCTION_ID'];. This will only print the last ID
@Samuel Herzog He'll also get a warning if $response is not an array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.