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
9
  • 1
    foreach ($it as $k => $v)? Commented Nov 24, 2019 at 10:40
  • @Jeto I dont see see any difference in the output. I still get one value per row. Commented Nov 24, 2019 at 10:47
  • Well, $k should be the key that you need, unless I misunderstood. Commented Nov 24, 2019 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. Commented Nov 24, 2019 at 11:03
  • Oh I missed that part. Let me propose a solution then (give me a few mins). Commented Nov 24, 2019 at 11:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.