Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

array_of_array.inc

Tadatoshi Tokutake edited this page May 30, 2015 · 13 revisions

Index

Details

aoa_set

array aoa_set(array $aoa, array $keys, mixed $value)

  • Return the array set $value at key1 => [key2 => ...].
  • Example:
var_dump(aoa_set([], ['VAIO Z', 'spec', 'cpu'], 'Core i7-5557U 3.10 GHz'));
// => [
//       'VAIO Z' => [
//          'spec' => [
//             'cpu' => 'Core i7-5557U 3.10 GHz',
//          ]
//       ]
//    ]

aoa_unset

array aoa_unset(array $aoa, array $keys)

  • Return the array unset the value at key1 => [key2 => ...].
  • Example:
$aoa = ['db' => ['primary' => 1, 'secondary' => 2]];
var_dump(aoa_unset($aoa, ['test', 'secondary'])); // => ['db' => ['primary' => 1]]

aoa_collect

array aoa_collect(array $aoa, mixed $key)

  • Return the array whose element has $key as key in $aoa.
  • Example:
$aoa = [
   'main' => [
      'title'   => 'who am i',
      'authors' => ['A']     ,
   ],
   'subs' => [
      ['title' => 'who are you', 'authors' => ['B', 'C']],
      ['title' => 'what is it' , 'authors' => ['D'     ]],
   ],
];
var_dump(aoa_collect($aoa, 'authors')); // => [['A'], ['B', 'C'], ['D']]

aoa_transpose

array aoa_transpose(array $matrix)

  • Return the transposed matrix.
  • Example:
$matrix = [
   [1, 2, 3],
   [4, 5, 6],
];
var_dump(aoa_transpose($matrix));
// => [
//       [1, 4],
//       [2, 5],
//       [3, 6],
//    ]

aoa_values

array aoa_values(array $aoa, mixed $key)

  • Return the array which is the elements having $key of the array of the array.
  • Example:
$aoa = [['id' => 0, 'name' => 'A'], ['id' => 1, 'name' => 'B']];
var_dump(aoa_values($aoa, 'id')); // => [0, 1]

aoa_sum

mixed aoa_sum(array $aoa, mixed $key)

  • Return the sum of elements having $key of the array of the array.
  • Example:
$aoa = [['id' => 0, 'point' => 2.5], ['id' => 1, 'point' => 3.5]];
var_dump(aoa_sum($aoa, 'point')); // => 6

aoa_sort

array aoa_sort(array $aoa, mixed $key, mixed $order = SORT_ASC, mixed $option = SORT_REGULAR)

$aoa = [['id' => 0, 'nice' => 15], ['id' => 1, 'nice' => 10]];
var_dump(aoa_sort($aoa, 'nice')); // => [['id' => 1, 'nice' => 10], ['id' => 0, 'nice' => 15]]

aoa_associate

array aoa_associate(array $aoa, mixed $key)

  • Return the array having the values of $key as keys.
  • Example:
$aoa = [['id' => 0, 'name' => 'A', 'nice' => 1], ['id' => 1, 'name' => 'B', 'nice' => 15]];
var_dump(aoa_associate($aoa, 'name')); // => ['A' => ['id' => 0, 'nice' => 1], 'B' => ['id' => 1, 'nice' => 15]]

Clone this wiki locally