5

i queried a DB like this which got me an array:

foreach($oid as $orderid) {
    $orderdetailData[] = DB::table('order_details')
        ->join('orders', 'order_details.oid', '=', 'orders.oid')
        ->select('order_details.oid', 'orders.ostatus')
        ->where('order_details.oid', $orderid)->get();
    }

    $data = array_flatten($orderdetailData);
    return $data;

This is the array i get

array (size=2)
  0 => 
    object(stdClass)[174]
      public 'oid' => int 1
      public 'ostatus' => string 'Placed' (length=6)
  1 => 
    object(stdClass)[158]
      public 'oid' => int 2
      public 'ostatus' => string 'Placed' (length=6)

I am trying to get this array in the form

array (size=2)
  0 => 
    array (size=2)
      public 'oid' => int 1
      public 'ostatus' => string 'Placed' (length=6)
  1 => 
    array (size=2)
      public 'oid' => int 2
      public 'ostatus' => string 'Placed' (length=6)

I tried doing this:

foreach($orderdetailData as $key => $value){
    $data[] = array_flatten($orderdetailData[$key]);
}

But doing this gets me an array in this form:

array (size=2)
  0 => 
    array (size=1)
      0 => 
        object(stdClass)[174]
          public 'oid' => int 1
          public 'ostatus' => string 'Placed' (length=6)
  1 => 
    array (size=1)
      0 => 
        object(stdClass)[158]
          public 'oid' => int 2
          public 'ostatus' => string 'Placed' (length=6)

Which is not i what i am looking for. Can someone tell me what would be an easy way to do this ? Thanks

1
  • You can check the solution to this SO question. It explains how to convert an object to an array. Commented Feb 22, 2015 at 16:08

2 Answers 2

13

Using array_map and casting to an array should be sufficient:

$data = array_map(function($object){
    return (array) $object;
}, $data);

I also wouldn't run queries inside a loop. You should try getting that data in one query from the db. Something like this could work:

$data = DB::table('order_details')
    ->join('orders', 'order_details.oid', '=', 'orders.oid')
    ->select('order_details.oid', 'orders.ostatus')
    ->whereIn('order_details.oid', $oid)->get();

Edit

As it has been mentioned in another answer shortly let me explain how you can accomplish the same by setting PDO to FETCH_ASSOC:

DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();

However this changes the fetch mode globally for the rest of the request (at least if you don't open a new connection). To be save you should change it back afterwards:

DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
DB::setFetchMode(PDO::FETCH_CLASS);

Or even back it up first if you can't be sure what default is used:

$fetchModeBefore = DB::getFetchMode();
DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
DB::setFetchMode($fetchModeBefore);
Sign up to request clarification or add additional context in comments.

3 Comments

array_map() worked. Thanks for replying, learnt something new today :)
Beware of the performance draw backs from this.
I tried to get values directly from object but i am getting 'Cannot use object of type MyClass as array' error how can i get value directly from class
1

On the PDO/DB object you can set the return style to assoc.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.