Skip to content
Advertisement

Array, array, and objects [closed]

I got this result in my variable

array(2){

  [0]=>
  array(1) {
    [0]=>
    object(stdClass)#1280 (23) 
    {
      ["num_id"]=>
      string(5) "73982"
    }
           }

  [1]=>
  array(2){
    [0]=>
    object(stdClass)#1281 (23) 
    {
      ["num_id"]=>
    string(5) "74216"
    }

  [1]=>
    object(stdClass)#1281 (23)
    {
      ["num_id"]=>
    string(5) "74216"
    }
        
        }
          } 

What I need is all in a single array, is that possible? Something like ARRAY (objects of array 1, and objects of array 2)

Advertisement

Answer

If this really just is about merging those entries into a single array, then the array_merge(...) function comes in handy…

If however you need some more control, maybe want to do some sanitizing on the fly, then just iterate over the entries:

<?php
$input = [
  [ (object) ["num_id" => "73982" ] ],
  [ (object) ["num_id" => "74216" ] ],
  [ (object) ["num_id" => "74212" ] ]
];      

$output = [];
array_walk($input, function($entry) use(&$output) {
  $output[] = array_values($entry)[0];
});

print_r($output);

The output obviously is:

Array
(
    [0] => stdClass Object
        (
            [num_id] => 73982
        )
    [1] => stdClass Object
        (
            [num_id] => 74216
        )
    [2] => stdClass Object
        (
            [num_id] => 74212
        )
)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement