0

I got a multidimensional array with X number of .... dimensions? Example of my array:

Array
(
[system] => Array
    (
        [step_x_y] => Array
            (
                [0] => Schnitt %1 von %2
                [1] => Trin %1 af %2
                [2] => Step %1 of %2
            )

        [type] => Array
            (
                [0] => Typ
                [1] => Type
                [2] => Type
            )

        [next_button] => Array
            (
                [0] => Aktualisieren
                [1] => Opdater
                [2] => Update
            )
[account.php] => Array
    (
        [h1] => Array
            (
                [0] => Kontoeinstellungen
                [1] => Konto Indstillinger
                [2] => Account Settings
            )

        [fields_required] => Array
            (
                [0] => Alle Felder, außer die Geschäftsinformation, müssen ausgefüllt werden
                [1] => Alle felter er obligatoriske, dog ikke virksomhedsinfo
                [2] => All fields required
            )
[mounting] => Array
    (
        [no] => Array
            (
                [text] => Array
                    (
                        [0] => Keine
                        [1] => Ingen
                        [2] => Nothing
                    )

                [more_text] => Array
                    (
                        [0] => Keine Befestigung
                        [1] => Ingen befæstelser
                        [2] => No mounting
                    )

            )

        [CFL] => Array
            (
                [text] => Array
                    (
                        [0] => Frontflansch (CLF)
                        [1] => Front Flange
                        [2] => Front Flange
                    )

                [more_text] => Array
                    (
                        [0] => Frontflansch zum Schweißen an dem Zylinderrohr
                        [1] => Front flange til påsvejsning på cylinder rør
                        [2] => Front Flange to be welded on cylinder tube
                    )

            ) ....

I'm trying to convert that array, into this "flat" format: (# and the array keys that follows has to be included too)

# system > step_x_y
(DE) Schnitt %1 von %2
(DK) Trin %1 af %2
(EN) Step %1 of %2

# system > type
(DE) Typ
(DK) Type
(EN) Type

# system > next_button
(DE) Aktualisieren
(DK) Opdater
(EN) Update

I use this code, to output all values, one value per line:

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($merged_array));
foreach($it as $v) {
  echo $v, "<br>";    
}

A couple of problems:

  • Not sure how to display the "row of keys" above the values.

  • [0][1][2] has to replaced with country codes, 0=DE, 1=DK, 2=EN.

Something like this:

# system > step_x_y
(DE) Schnitt %1 von %2
(DK) Trin %1 af %2
(EN) Step %1 of %2

# system > type
(DE) Typ
(DK) Type
(EN) Type

# system > next_button
(DE) Aktualisieren
(DK) Opdater
(EN) Update

Hope one of you might be able to help me out :)

UPDATE:

I changed some of the code as suggested:

foreach ($it as $k => $v) {

  if($k == 0)
    $cc = '<br>(DE)';
  elseif($k == 1)
    $cc = '(DK)';
  elseif($k == 2)
    $cc = '(EN)';
  else
    $cc = '-';

  echo $cc, $v, "<br>";   
}

This is the output I recieve now:

(DE) Schnitt %1 von %2
(DK) Trin %1 af %2
(EN) Step %1 of %2

(DE) Typ
(DK) Type
(EN) Type

(DE) Aktualisieren
(DK) Opdater
(EN) Update

Just need to add "keys" above each "section" of values. This to see where the text is located in the array (it's huge).

# system > step_x_y
(DE) Schnitt %1 von %2
(DK) Trin %1 af %2
(EN) Step %1 of %2

# system > type
(DE) Typ
(DK) Type
(EN) Type
Kenneth Poulsen
  • 929
  • 10
  • 25
  • 1
    `foreach ($it as $k => $v)`? – Jeto Nov 24 '19 at 10:40
  • @Jeto I dont see see any difference in the output. I still get one value per row. – Kenneth Poulsen Nov 24 '19 at 10:47
  • Well, `$k` should be the key that you need, unless I misunderstood. – Jeto Nov 24 '19 at 10:51
  • My bad, it almost work now :) I get the key names in front of the values. Do you know how to post the names of all the keys "belonging" to the value? (Example: system > step_x_y ). Going to post a small update to my post with your code. – Kenneth Poulsen Nov 24 '19 at 11:03
  • Oh I missed that part. Let me propose a solution then (give me a few mins). – Jeto Nov 24 '19 at 11:17
  • Actually, found a duplicate: https://stackoverflow.com/questions/16855211/php-recursive-iterator-parent-key-of-current-array-iteration. That should help. – Jeto Nov 24 '19 at 11:27
  • that worked, thanks a lot. Please post an aswer, so I can accept :) – Kenneth Poulsen Nov 24 '19 at 14:05
  • I actually posted an alternative solution below. If you like the other question's answer better then it should not be posted again here :) – Jeto Nov 24 '19 at 14:14
  • Possible duplicate of [PHP Recursive Iterator: Parent key of current array iteration?](https://stackoverflow.com/questions/16855211/php-recursive-iterator-parent-key-of-current-array-iteration) – Jeto Nov 24 '19 at 14:16

1 Answers1

0

As an alternate solution to the one in the related topic (see comments), here's a version without RecursiveIterator*, using a "simple" recursive function:

function getLeafsWithKeys(array $array, array $keys = []): array
{
  $result = [];
  foreach ($array as $key => $value) {
    $new_keys = is_numeric($key) ? $keys : array_merge($keys, [$key]);
    if (is_array($value)) {
      $result = array_merge([], ...[$result, getLeafsWithKeys($value, $new_keys)]);
    }
    else {
      $result[implode(' > ', $new_keys)][] = $value;
    }
  }
  return $result;
}

Demo: https://3v4l.org/kWZUW

Jeto
  • 14,596
  • 2
  • 32
  • 46