I want to be able to take an array of stdClass returned from PDO fetchAll and turn this into a single array.
So i get an array back that looks like this:
// print_r($getCategories); returns..
Array
(
[0] => stdClass Object
(
[field1] => 'foo',
[field2] => 'foo2'
)
[1] => stdClass Object
(
[field1] => 'bar'
[field2] => 'bar2'
)
)
I loop through this and put a list of one field into a combo box
// <select> and <option> around this..
foreach($getCategories as $category) {
echo $category->field1;
}
Now i know i could create an array by using the following code
foreach($getCategories as $category){
if($_POST['category'] == $category->field1){
$chosenCategory = array(
'field1' => $category->field1,
'field2' => $category->field2
);
}
}
I just want to know if there is a better way to do this where I don't have to build an array, instead just filter what is returned in $getCategories using user input?
I am fairly new to OOP and programming in general so an explanation as well as an answer would be much appreciated!
Thanks
PDO::fetchAll
return an array by default? – Jonathan Kuhn Mar 13 at 17:04$categories = array_walk( $categories, function (&$value) { $value = (array) $value; } );
– Mark Baker Mar 13 at 17:05fetch_style
in Manual – david strachan Mar 13 at 17:37