1

I have a MYSQL query the goes through a framework (WolfCMS).

$sql_query = 'SELECT DISTINCT country FROM ' . $tableName . ' ORDER BY country ASC';
$countries = Record::query($sql_query, array()); //execute query

But what is returned is an array of objects like this

Array ( [0] => stdClass Object ( 
        [country] => Canada ) [1] => stdClass Object ( 
        [country] => France ) )

I was wondering if there was a way with php to merge all the object to get the array as simple as possible like

Array ( [0] => Canada  [1]  => France )

I know I could always parse the array with a foreach loop once I get the data and create a custom array the way I needed but I was wondering if there was a way to directly get the data to it's final form from the database.

I just want a simple array to use it as an parameter for an autocomplete function on a text field.

* EDIT *

I found a better way. I simply had to avoid executing the query with the Record class.

Here's how

//create sql statement
$sql_query = 'SELECT DISTINCT country' . 
            ' FROM ' . Record::tableNameFromClassName(__CLASS__) . 
            ' ORDER BY country ASC';
$stmt = Record::getConnection()->prepare($sql_query);
$stmt->execute(array());
return $stmt->fetchAll(Record::FETCH_COLUMN);
VVV
  • 7,563
  • 3
  • 34
  • 55
  • 2
    what framework do you use, some frameworks come with built-in capability to get array instead of object. why does the way data returned is important in your situation? – DevZer0 Jul 31 '13 at 13:47
  • Can you post the code you use to execute and retrieve your query? – jterry Jul 31 '13 at 13:47
  • possible duplicate http://stackoverflow.com/questions/455700/what-is-the-best-method-to-merge-two-php-objects – Sergei Beregov Jul 31 '13 at 13:48
  • You could extend the framework/api and add some methods for convenience. – Vatev Jul 31 '13 at 13:49
  • Updated the answer. @SergeiBeregov I know about the array_merge but I'm looking to merge an unknown amount of arrays. I know I could use a for loop but I just thought there would be a simpler solution considering It's a simple query. But I don't think I can't with this framework. Thanks everyone. – VVV Jul 31 '13 at 13:54

2 Answers2

1

You'll not be able to achieve this with only a query in WolfCMS. You should try:

$finalArr = array();
for($i=0; $i<sizeof($result);$i++) // $result being your query result
    array_push($finalArr,$result[$i]->country);

print_r($finalArr);
Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
  • 1
    As far as I inderstand OP is aware of this solution, he is looking for a 'cleaner'/simpler way to do it. – Vatev Jul 31 '13 at 13:50
  • You can't get a 1D array like this with the builtin MySQL API's. What you can do is make a function/method which accepts a recordset and returns an array with the 1-st field of every row. – Vatev Jul 31 '13 at 13:54
  • @YotamOmer I think you're right. It's a limitation from the framework. Thanks! – VVV Jul 31 '13 at 13:54
1

Have you tried array_map?

$countries = array_map(function ($item) { return $item->country; }, $result );
SWilk
  • 3,261
  • 8
  • 30
  • 51