1

How can I build an array from string and values ?

For exemple :

            string              |        value
                                |   
objectId                        |   19
location.street                 |   Rue des clochets
translations.fr.idTranslation   |   4

And the result must be :

[
    'objectId' => 19,
    'location' => [
        'street' => 'Rue des clochets'
    ],
    'translations' => [
        'fr' => [
            idTranslation => 4
        ],
    ]
]

Obiviously, if the key already exists, it is complete, not duplicate.

Like : translation.fr.country | France

The array will become :

[
    'object' => 19,
    'location' => [
        'street' => 'Rue des clochets'
    ],
    'translations' => [
        'fr' => [
            'idTranslation' => 4,
            'country'       => 'France'
        ],
    ]
]

I think, using explode is the good way but I don't find the good syntax.

The head of my method is :

 public function buildArray($key, $value) {

 }

I call this method in a foreach.

And the array is a property.

Thank's for help.

Royce
  • 45
  • 1
  • 1
  • 9

2 Answers2

0
public function buildArray($key, $value) {   
  $keys = explode('.', $key);

  $x = count($keys) - 1;
  $temp = array($keys[$x] => $value);
  for($i = $x-1; $i >= 0; $i--)
  {
    $temp = array($keys[$i] => $temp);
  }
  return $temp
}

Something like this should work. Please appologize if it does not run without some work of you, it is untested, but it seems to work https://3v4l.org/3HBAY. Of course this will give you one array per key/value pair. You will have to merge the returning array (array_merge_recursive) with your result in your foreach.

Doktor OSwaldo
  • 5,732
  • 20
  • 41
  • Why the downvote? Can this person please explain what should be wrong with this answer? If there is a reason for a downvote, I will gladly improve my answer or even delete it. But without a comment I see no sense in it. – Doktor OSwaldo Jul 11 '17 at 07:56
  • Thank's that's work. (Indeed, I need to use array_merge_recursive) – Royce Jul 11 '17 at 08:02
  • @NicolasLamblin Glad to help you. Could you please tell me what you fixed, so i can correct my answer (for future visitors). – Doktor OSwaldo Jul 11 '17 at 08:04
  • I saw the answer without the comment about array_merge_recursive. I used array_merge and keys weren't saved in the array. With array_merge_recursive the problem is solved. – Royce Jul 11 '17 at 08:07
  • @NicolasLamblin yeah I saw that a little bit late. Thank you for your feedback I appreciate it. I've updated my answer. – Doktor OSwaldo Jul 11 '17 at 08:09
0

Code below, you can also tinker with it at https://3v4l.org/sLvXp

<?php

$input = "objectId                        |   19
location.street                 |   Rue des clochets
translations.fr.idTranslation   |   4";

$output = [];

$lines = explode("\n", $input);
foreach ($lines as $line) {
    $cols = explode("|", $line);
    $key = trim($cols[0]);
    $value = trim($cols[1]);
    $indices = explode('.', $key);
    $first = array_shift($indices);
    if (!isset($output[$first]))
        $output[$first] = [];
    $target = &$output[$first];
    foreach ($indices as $index) {
        if (!isset($target[$index])) {
            $target[$index] = [];
            $target = &$target[$index];
        }
    }
    $target = $value;
}

var_dump($output);
Jirka Hrazdil
  • 3,983
  • 1
  • 14
  • 17