Skip to content

Instantly share code, notes, and snippets.

@r0hack
Created October 16, 2017 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r0hack/9b030d3f2fe64743df3721a5b5ddfd47 to your computer and use it in GitHub Desktop.
Save r0hack/9b030d3f2fe64743df3721a5b5ddfd47 to your computer and use it in GitHub Desktop.
$x = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
function reverse($getArray) {
if(!empty($getArray)) {
$value = array_pop($getArray);
$sendElement = [$value => to_array($getArray)];
return $sendElement;
}
}
$x = reverse($x);
echo '<pre>';
print_r($x);
echo '</pre>';
$data = [
'parent.child.field' => 1,
'parent.child.field2' => 2,
'parent2.child.name' => 'test',
'parent2.child2.name' => 'test',
'parent2.child2.position' => 10,
'parent3.child3.position' => 10,
];
function getResult1($getArray) {
foreach($getArray as $i => $j) {
$i = explode('.', $i);
$result[$i[0]][$i[1]][$i[2]] = $j;
}
return $result;
}
$result1 = getResult1($data);
echo '<pre>';
print_r($result1);
echo '</pre><br>';
function getResult2($getArray) {
foreach($getArray as $i1 => $j1) {
foreach($j1 as $i2 => $j2) {
foreach($j2 as $i3 => $j3) {
$result["{$i1}.{$i2}.{$i3}"] = $j3;
}
}
}
return $result;
}
$result2 = getResult2($result1);
echo '<pre>';
print_r($result2);
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment