0

i have a array like below:

Array ( [0] => stdClass Object ( [city_name] => Hyderabad ) 
        [1] => stdClass Object ( [city_name] => Visakhapatnam )
        [and so on..] => and so on...
      )

this array is return by this block of code:::

 $query = "select city_name from city";
 $result = $CI->Dbmodel->customQueryResult($query);
 print_r(array_values($result));

and i would like to convert the above array to:

array('Hyderabad','Visakhapatnam','and so on..')

do php provide any built-in-function?

how can i do this?.. any help or suggestion would be a great help..thanks in advance

6
  • Show us the code that gets you the array you already have Commented Mar 23, 2014 at 13:32
  • Are you using a framework? The sign will be to map the array to convert objects into arrays... Commented Mar 23, 2014 at 13:34
  • @AllanStepps Sir.. yes.. codeigniter... Commented Mar 23, 2014 at 13:35
  • 2
    A simple foreach ? $city = array(); foreach($yourobj as $obj) { $city[]=$obj->city_name; } Commented Mar 23, 2014 at 13:35
  • 1
    @ShankarDamodaran thanks sir..your comment does this...works like charms.. thank you very much sir.. Commented Mar 23, 2014 at 13:50

2 Answers 2

2

This should work:

$query = "select city_name from city";
$result = $CI->Dbmodel->customQueryResult($query)->result_array();
print_r($result);
Sign up to request clarification or add additional context in comments.

Comments

2

If you can't modify your model use this code

$reslt = array();
    foreach($result_array as $value){
        $reslt[] = $value->city_name;
    }

or use result_array() function.

Comments

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.