1

I'm trying to take a result from fetchAll and to take some of the data and put it into an array.

Relevant PHP:

  $catcodessql = "select distinct cat_code from mytable";
  $result2 = $dbh->query($catcodessql)->fetchAll(PDO::FETCH_ASSOC); 

This returns something like below:

array(4) {
 [0]=>
array(1) {
["cat_code"]=>
string(3) "edu"
 }
 [1]=>
 array(1) {
["cat_code"]=>
string(3) "inc"
}
[2]=>
array(1) {
["cat_code"]=>
string(3) "net"
 }
[3]=>
 array(1) {
["cat_code"]=>
string(3) "occ"
}

What I want is all those strings so I can dynamically put together this line later in my PHP:

$allcategories = array ('edu', 'inc', 'net', 'occ' );

So, I need to create a variable that will replace this part: 'edu', 'inc', 'net', 'occ'

I've tried a couple of while functions, but clearly they aren't right.

4
  • If you use PHP +5.5 then you may want to look into: php.net/manual/en/function.array-column.php Commented Jan 27, 2015 at 18:42
  • do you want only this array $allcategories = array ('edu', 'inc', 'net', 'occ' )? Commented Jan 27, 2015 at 18:55
  • @acontell Yes, that's what I want Commented Jan 28, 2015 at 17:56
  • check my answer below then, I think it should work Commented Jan 28, 2015 at 17:57

2 Answers 2

2

PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column()

$allcategories = array_column($result2, 'cat_code');

Or foreach over the query:

foreach($dbh->query($catcodessql) as $row) {
    $allcategories = $row['cat_code'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

If he only have this column in the array he also could use this one: print_r(array_keys(call_user_func_array('array_merge', array_map("array_flip", $arr)))); :D But i know that you love array_column(), so +1
I think I'm not implementing what you suggested correctly. I'm trying this: $catcodessql = "select distinct cat_code from mytable"; $dbh->query($catcodessql); while($row = $dbh->fetch(PDO::FETCH_ASSOC)) { $allcategories = $row['cat_code']; } Is that right or am I missing something? This is returning a 500 error.
I don't know about the 500 and rarely use query but check the edit with the foreach.
1

If you only want to retrieve unique categories, You can do it directly with a query:

$stmt = $dbh->prepare("select GROUP_CONCAT( cat_code SEPARATOR ',') as cat_code from ( select distinct cat_code from mytable) as m"); 
$stmt->execute(); 
$row = $stmt->fetch();
$categories = explode(",",$row["cat_code"]);
var_dump($categories);

5 Comments

This results in roughly the same thing I had before: array(36) { [0]=> string(3) "edu" [1]=> string(3) "inc" [2]=> string(3) "net" [3]=> string(3) "occ" [4]=>
isn't it what you wanted?
Ideally, I wanted it precisely in this format: ('edu', 'inc', 'net', 'occ' ). I can't seem to get from the array returned to the array I need to feed another function, which just wants this comma separated list.
I mean, you need a string?
Wait a minute...my mistake. I actually don't need any further processing. I was able to take exactly what you had and put it into my function. So, this worked perfectly after all. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.