3

as am trying to stdClass Object array to normal array...product id and category separated by commas... how do i convert std class object to normal array...

Array (
  [0] => stdClass Object (
    [product_id] => SHIRT61
    [product_category] => 4
  )
  [1] => stdClass Object (
    [product_id] => SHIRT51
    [product_category] => 5
  )
) 

to product_id = {'SHIRT61','SHIRT51'} and product_category = {'5','7'}

3 Answers 3

3

What does your model look like where this data is being generated? If you query your database to retrieve arrays you won't have to worry about converting the data.

In codeigniter you can use row_array() instead of row() or result_array() instead of result()

For example change this:

$query = $this->db->get('mytable');
$query->get->row();
return $query;

Into this:

$query = $this->db->get('mytable');
$query->get->row_array();
return $query;

In this example I'm using row()/row_array(), but the same applies for result()/result_array()

Sign up to request clarification or add additional context in comments.

1 Comment

thanks daniel....i have used result_array().... both $array = json_decode(json_encode($objArray), true); & result_array() are rite....
3

try this :

$array = json_decode(json_encode($objArray), true);

This will work for recursive array also.

Comments

2

Try the following :

$array = (array) $object;

Or

$array = get_object_vars($object);

1 Comment

it returns the same std class object

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.