2

How to convert a multidimensional array into single dimensional array without using foreach method

Array
    (
       [0] => Array
           (
               [opportunityid] => 5
               [id] => 89
               [discountedpackagecost] => 89990.00
               [discountedaddoncost] => 61000.00
               [title] => This is a very big title okayyyyyyy???????
           )

       [1] => Array
           (
               [opportunityid] => 42
               [id] => 90
               [discountedpackagecost] => 45592.00
               [discountedaddoncost] => 0.00
               [title] => test book
           )

    )

I just need only the Key->"id" form each rows.

Any default methods available in php??

I just expect the result to be like this.

array(2) { [0]=> string(2) "89" [1]=> string(2) "90" }
Sai Deepak
  • 678
  • 4
  • 16
  • You can take a look at phps array functions, namely the `array_walk_...()` family. But those use loops themselves too, so what is the point here? – arkascha Jul 18 '15 at 06:10
  • I know that some default functions are having inbuilt loop. I just want those functions to have inbuilt loops without making us to create a custom loop. – Sai Deepak Jul 18 '15 at 06:19
  • OK, that sounds like the array functions php offers are just what you are looking for. The php documentation contains a full list with good examples of the functions usages. – arkascha Jul 18 '15 at 06:33

1 Answers1

2

You can use array_column in PHP 5.5 or newer

$array = array(
               array(
                  'id' => 1,
                  'name' => 'foo'
               ),
               array(
                  'id' => 2,
                  'name' => 'bar'
               )
         );

$ids= array_column($array, 'id');
print_r($ids);

Read More

pinkal vansia
  • 10,240
  • 5
  • 49
  • 62