update page now

Voting

: five plus two?
(Example: nine)

The Note You're Voting On

opencart dot ocfilter at gmail dot com
3 years ago
Array multiple columns:

<?php
function array_columns() {
  $args = func_get_args();

  $array = array_shift($args);

  if (!$args) {
    return $array;
  }

  $keys = array_flip($args);

  return array_map(function($element) use($keys) {
    return array_intersect_key($element, $keys);
  }, $array);
}
?>
EXAMPLE:
<?php
$products = [
  [    
    'id' => 2,
    'name' => 'Phone',
    'price' => 210.3
  ],
  [    
    'id' => 3,
    'name' => 'Laptop',
    'price' => 430.12
  ]
];

print_r(array_columns($products, 'name', 'price'));
?>

Output:

Array
(
    [0] => Array
        (
            [name] => Phone
            [price] => 210.3
        )

    [1] => Array
        (
            [name] => Laptop
            [price] => 430.12
        )

)

<< Back to user notes page

To Top