1

I have used this function to get multi column from array. Alternate of array_column. I have created this function as array_column not work according my requirement.

My Data

$students = 
Array
(
    [0] => stdClass Object
        (
            [id] => 498
            [uuid] => 6cb91efd-9111-4be8-a2d7-80d3edeed732
            [name] => Andrew A. Blaine
            [email] => [email protected]
            [usertype_id] => 6
            [first_name] => Andrew A.
            [last_name] => Blaine
        )

    [1] => stdClass Object
        (
            [id] => 499
            [uuid] => 208764a0-c53d-404b-ad05-ee7cba28a51c
            [name] => Billie C. Heath
            [email] => [email protected]
            [usertype_id] => 6
            [first_name] => Billie C.
            [last_name] => Heath
        )
)

My function

  public function filterArrayByKeys($data, $keys = array()) {
    $filterData = array_map(function($e) use ($keys) {
        if (is_object($e)) {
            foreach ($keys as $key) {
                $filterArray[$key] = $e->$key;
            }
            return $filterArray;
        } else {
            foreach ($keys as $key) {
                $filterArray[$key] = $e[$key];
            }
            return $filterArray;
        }
    }, $data);
    return array_values(array_unique($filterData, SORT_REGULAR));
 }
 $students = $this->filterArrayByKeys($students, ['id', 'name', 'email']);

Now my result as I expected is:

$students = Array
(
    [0] => Array
        (
            [id] => 498
            [name] => Andrew A. Blaine
            [email] => [email protected]
        )

    [1] => Array
        (
            [id] => 499
            [name] => Billie C. Heath
            [email] => [email protected]
        )
)

You can use this function to get multi columns from array. Hope this will help you.

2
  • Function which will work as array_column but for multiple keys, is the question Commented Apr 23, 2019 at 13:11
  • 1
    So... this is a non-question? You posted a working technique (an answer to a problem) as a question. There is no question. Commented May 20, 2022 at 3:35

1 Answer 1

4

There is custom function to achieve this,

public function filterArrayByKeys(array $input, array $column_keys)
{
    $result      = array();
    $column_keys = array_flip($column_keys); // getting keys as values
    foreach ($input as $key => $val) {
         // getting only those key value pairs, which matches $column_keys
        $result[$key] = array_intersect_key($val, $column_keys);
    }
    return $result;
}

$a = $this->filterArrayByKeys($students, ['id','name','email']);
print_r($a);

array_flip — Exchanges all keys with their associated values in an array
array_intersect_key — Computes the intersection of arrays using keys for comparison

Output

Array
(
    [0] => Array
        (
            [id] => 498
            [name] => Andrew A. Blaine
            [email] => [email protected]
        )

    [1] => Array
        (
            [id] => 499
            [name] => Billie C. Heath
            [email] => [email protected]
        )

)

Working demo.

Source.

Sign up to request clarification or add additional context in comments.

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.