Look at this array
array (
'a' =>
array (
'b1' =>
array (
'c1' => 1,
'c2' => 2,
'c3' => 3,
),
'b2' =>
array (
'c' => 4,
),
),
)
You can convert it to
array (
'a/b1/c1' => 1,
'a/b1/c2' => 2,
'a/b1/c3' => 3,
'a/b2/c' => 4,
)
Possibly without using a complex set of instructions:
foeach, array_map (), etc.
On 6/28/2021 6:57 AM, alex wrote:
Look at this array
array (
'a' =>
array (
'b1' =>
array (
'c1' => 1,
'c2' => 2,
'c3' => 3,
),
'b2' =>
array (
'c' => 4,
),
),
)
You can convert it to
array (
'a/b1/c1' => 1,
'a/b1/c2' => 2,
'a/b1/c3' => 3,
'a/b2/c' => 4,
)
Possibly without using a complex set of instructions:
foeach, array_map (), etc.
A little bit of complexity may be needed.
:-)
I came up with:
function pathjoin_help($o, $elms, &$res) {
if(is_int($o)) {
$res[implode('/', $elms)] = $o;
} else if(is_array($o)) {
$elms[] = null;
foreach($o as $pathelm => $value) {
$elms[count($elms)-1] = $pathelm;
pathjoin_help($value, $elms, $res);
}
} else {
die("Houston we have a problem");
}
}
function pathjoin($a) {
$res = array();
pathjoin_help($a, array(), $res);
return $res;
}
Arne
Il 24/07/21 03:14, Arne Vajhøj ha scritto:
On 6/28/2021 6:57 AM, alex wrote:
Look at this array
array (
'a' =>
array (
'b1' =>
array (
'c1' => 1,
'c2' => 2,
'c3' => 3,
),
'b2' =>
array (
'c' => 4,
),
),
)
You can convert it to
array (
'a/b1/c1' => 1,
'a/b1/c2' => 2,
'a/b1/c3' => 3,
'a/b2/c' => 4,
)
Possibly without using a complex set of instructions:
foeach, array_map (), etc.
A little bit of complexity may be needed.
:-)
I came up with:
function pathjoin_help($o, $elms, &$res) {
if(is_int($o)) {
$res[implode('/', $elms)] = $o;
} else if(is_array($o)) {
$elms[] = null;
foreach($o as $pathelm => $value) {
$elms[count($elms)-1] = $pathelm;
pathjoin_help($value, $elms, $res);
}
} else {
die("Houston we have a problem");
}
}
function pathjoin($a) {
$res = array();
pathjoin_help($a, array(), $res);
return $res;
}
Arne
function pathjoin_help($o, $elms, &$res) {
if(is_int($o)) {
$res[implode('/', $elms)] = $o;
} else if(is_array($o)) {
$elms[] = null;
foreach($o as $pathelm => $value) {
$elms[count($elms)-1] = $pathelm;
pathjoin_help($value, $elms, $res);
}
} else {
die("Houston we have a problem");
}
}
function pathjoin($a) {
$res = array();
pathjoin_help($a, array(), $res);
return $res;
}
print_r(
pathjoin(
array (
'a' =>
array (
'b1' =>
array (
'c1' => '1',
'c2' => 2,
'c3' => 3,
),
'b2' =>
array (
'c' => 4,
),
),
)
)
);
Houston we have a problem
If you want the final item to be anything then you can use:
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 349 |
Nodes: | 16 (0 / 16) |
Uptime: | 151:26:11 |
Calls: | 7,615 |
Calls today: | 3 |
Files: | 12,792 |
Messages: | 5,685,428 |
Posted today: | 2 |