torunar/array-functions

Set of functions to operate the most glorious PHP data structure

v0.0.1 2021-09-30 13:01 UTC

This package is auto-updated.

Last update: 2024-04-29 04:28:00 UTC


README

Set of functions to operate the most glorious PHP data structure.

Installation

$ composer require torunar/array-functions

Functions

array_column

Works as the regular array_column function, but supports column value and index extraction via data providers.

Example:

<?php

$data_from_api = [
    [
        'currency' => [
            'code' => 'EUR',
        ],
        'amount' => [
            'value' => 42.00,
        ],
    ],
    [
        'currency' => [
            'code' => 'USD',
        ],
        'amount' => [
            'value' => 53.00,
        ],
    ],
    [
        'currency' => [
            'code' => 'JPY',
        ],
        'amount' => [
            'value' => 5300,
        ],
    ],
];

$necessary_data = \Torunar\array_column(
    $data_from_api,
    static function ($item, $key) {
        return $item['amount']['value'];
    },
    static function ($item, $key) {
        return $item['currency']['code'];
    }
);

var_dump(
    $necessary_data
);

Output:

array(3) {
  ["EUR"]=>
  float(42)
  ["USD"]=>
  float(53)
  ["JPY"]=>
  int(5300)
}