Some lightweight utility classes to plug missing basic functions in php
use MOJ\Utils\Arrays;
$input = ['foo', 'bar'];
$renamedArray = Arrays::rename(['foo' => 'hello', 'bar' => 'world']);
// returns $renamedArray = ['hello', 'world']
$input = [[0],[1],[2]];
$flattenedArray = Arrays::flatten($input);
// returns $flattenedArray = [0, 1, 2];
$input = [
'book' => 'Dune',
'housePlanets' => [
'Atreides' => 'Caladan',
'Harkonnen' => 'Giedi Prime',
],
'houses' => [
[
'name' => 'Atreides',
'homePlanet' => 'Caladan',
'leader' => 'Duke Leto',
],
[
'name' => 'Harkonnen',
'homePlanet' => 'Geidi Prime',
'leader' => 'Baron',
]
],
'rating' => '*****',
];
$flattendArray = Arrays::flatten($input, '.');
// returns $flattenedArray = [
// 'book' => 'Dune',
// 'housePlanets.Atreides' => 'Caladan',
// 'housePlanets.Harkonnen' => 'Giedi Prime',
// 'houses.0.name' => 'Atreides',
// 'houses.0.homePlanet' => 'Caladan',
// 'houses.0.leader' => 'Duke Leto',
// 'houses.1.name' => 'Harkonnen',
// 'houses.1.homePlanet' => 'Geidi Prime',
// 'houses.1.leader' => 'Baron',
// 'rating' => '*****',
// ];
$array1 = [
'book' => 'Dune',
'housePlanets' => [
'Atreides' => 'Caladan',
'Harkonnen' => 'Giedi Prime',
],
'rating' => '*****',
];
$array2 = [
'book' => null,
'housePlanets' => [
'Harkonnen' => null,
]
];
$intersectedArray = Arrays::intersectKeyRecursive($array1, $array2);
// $intersectedArray = [
// 'book' => 'Dune',
// 'housePlanets' => [
// 'Harkonnen' => 'Giedi Prime',
// ],
// ];