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: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 44:41:48 |
| Calls: | 12,111 |
| Calls today: | 2 |
| Files: | 15,010 |
| Messages: | 6,518,460 |