Skip to content

Instantly share code, notes, and snippets.

@twysto
Last active February 22, 2024 15:57
Show Gist options
  • Save twysto/88dc0cb187d2db81c3b0f7634be0234b to your computer and use it in GitHub Desktop.
Save twysto/88dc0cb187d2db81c3b0f7634be0234b to your computer and use it in GitHub Desktop.
PHP | Array Weaving
<?php
if (! function_exists('array_weave')) {
function array_weave(...$args) {
/**
* @todo
* 1. Check that each argument is the same size. If they're not, return false.
* 2. Add the type declarations and corresponding DocBlock.
*/
return array_map(fn (...$args) => func_get_args(), ...$args);
}
}
$ids = [1, 2, 3, 4, 5];
$types = ['waiting', 'failed', 'success', 'waiting', 'success'];
$statuses = ['verified', 'verified', 'unverified', 'unverified', 'verified'];
$dates = ['2022-05', '2023-01', '2023-03', '2023-11', '2024-01'];
$res = array_weave($ids, $types, $statuses, $dates);
print_r($res);
// Array
// (
// [0] => Array
// (
// [0] => 1
// [1] => waiting
// [2] => verified
// [3] => 2022-05
// )
// [1] => Array
// (
// [0] => 2
// [1] => failed
// [2] => verified
// [3] => 2023-01
// )
// [2] => Array
// (
// [0] => 3
// [1] => success
// [2] => unverified
// [3] => 2023-03
// )
// [3] => Array
// (
// [0] => 4
// [1] => waiting
// [2] => unverified
// [3] => 2023-11
// )
// [4] => Array
// (
// [0] => 5
// [1] => success
// [2] => verified
// [3] => 2024-01
// )
// )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment