Home > database >  Convert data array at php laravel
Convert data array at php laravel

Time:01-12

I have data array :

Array
(
  [0] => Illuminate\Support\Collection Object
    (
      [items:protected] => Array
        (
          [0] => stdClass Object
            (
              [product_id] => 780
            )

          [1] => stdClass Object
            (
              [product_id] => 789
            )

          [2] => stdClass Object
            (
              [product_id] => 800
            )

          [3] => stdClass Object
            (
              [product_id] => 769
            )
        )
     )  
)

How can I get data only value on array?
output needed $data = '780','789','800','769';

CodePudding user response:

I Am Supposing You Are fetching this data from your database using Equolent ORM.

So First Fetch the data:

$resultsInitial = Model::get(); // you can specify your clauses here

then create a new empty array

$results = [];

then run a foreach to push the data into the new array

foreach ($resultsInitial as $record) {
   array_push($results, $record->product_id);
}

then finally return the new array

return $results; // output: [1,2,3]

CodePudding user response:

Simple you can get array by following code

$resultsInitial->toArray();
  •  Tags:  
  • Related