-2

I have this array

Array
(
    [0] => Array
        (
            [column_name] => product_id
        )

    [1] => Array
        (
            [column_name] => product_name
        )

    [2] => Array
        (
            [column_name] => product_slug
        )

)

and I need a array only with the names of the columns:

array('product_id', 'product_name', 'product_slug')

I made a empty array and iterate the main array like this:

$data = array();
foreach ($result as $key => $res) {
    $data[] = $res['column_name'];
}

Is there any other method instead of iterating?

2 Answers2

2

If you are running PHP > 5.5 then try to use array_column function. Like this:

$data = array_column($result, 'column_name');
print_r($data);

If your PHP < 5.5 then kindly use this:

//Signature: array array_column ( array $input , mixed $column_key [, mixed $index_key ] )
if( !function_exists( 'array_column' ) ) {

    function array_column( array $input, $column_key, $index_key = null ) {

        $result = array();
        foreach( $input as $k => $v )
            $result[ $index_key ? $v[ $index_key ] : $k ] = $v[ $column_key ];

        return $result;
    }
}
Parixit
  • 3,829
  • 3
  • 37
  • 61
  • 1
    Note to @Ionvasile This is a nice clean solution. But just for reference, this just moves the array iteration from something you wrote to something someone else wrote. It did not remove the array iteration! With arrays come array iteration, there is no way round it – RiggsFolly Dec 22 '15 at 12:41
  • @RiggsFolly But I guess using inbuilt function would be faster compare to user defined functions!! Isn't it? – Parixit Dec 22 '15 at 12:43
  • Yes quite probably, as this is a piece of compiled `C` code you are running. I was not complaining about your answer, just making a comment to the questioner – RiggsFolly Dec 22 '15 at 12:45
  • @RiggsFolly Yes I understand, But let us provide bit faster solution to lon vasile. :D – Parixit Dec 22 '15 at 12:48
  • 1
    Absolutely. Like I said earlier. **I was not complaining about your answer** just reminding the questioner that the array still has to be iterated over by something as that is the only way to get data out of an array. – RiggsFolly Dec 22 '15 at 12:59
0

You can use array_column() in you are using PHP > 5.5.

$data = array();
$data = array_column($result, 'column_name');
print_r($first_names);

If your version is less than it, add this code:

<?php
if (! function_exists('array_column')) {
    function array_column(array $input, $columnKey, $indexKey = null) {
        $array = array();
        foreach ($input as $value) {
            if ( ! isset($value[$columnKey])) {
                trigger_error("Key \"$columnKey\" does not exist in array");
                return false;
            }
            if (is_null($indexKey)) {
                $array[] = $value[$columnKey];
            }
            else {
                if ( ! isset($value[$indexKey])) {
                    trigger_error("Key \"$indexKey\" does not exist in array");
                    return false;
                }
                if ( ! is_scalar($value[$indexKey])) {
                    trigger_error("Key \"$indexKey\" does not contain scalar value");
                    return false;
                }
                $array[$value[$indexKey]] = $value[$columnKey];
            }
        }
        return $array;
    }
}
Pupil
  • 23,834
  • 6
  • 44
  • 66