0

my sql query gets me results like this :

Maths
Maths|Algebre
Maths|Algebre|Algebre 1
Maths|Algebre|Algebre 2
Maths|Algebre|Algebre 3
Maths|Analyse
Maths|Analyse|Analyse 1
Maths|Analyse|Analyse 2
Maths|Probabilité
physics 

I have tryied many unsuccessful methods to use these strings to ultimately create an nested list in html.

I want to save them as a nested array like this :

$stack= array(
    "maths" =>  array(
            "Algebre" => array( "Algebre 1" ,"Algebre 2"),
            "Analyse" => array("Analyse 1","Analyse 2"),
            "Proba"
    ),
    "Physics"
) ;

and then I'd do something like this :

function print_list($array){
   echo "<ul>" ;
   foreach($array as $key => $item) {
       if (is_string($key)) echo "<li> $key" ;
       if (is_array($item)) print_list($item) ;
       else echo "<li> $item" ;
       echo "</li>" ;
    }
    echo "</ul>" ;
 }

 print_list($stack);
5
  • Please, could you post a short var_export() of $array ? Commented Mar 8, 2020 at 11:13
  • is(array()) : look at is_array() Commented Mar 8, 2020 at 11:16
  • the problem is I can't produce the nested array. and the function is just a code I wrote on the fly here as an eventual solution once I get my nested array Commented Mar 8, 2020 at 11:19
  • I understand the problem, but you can at least use proper syntax in your expected array. As it is now it's not php and therefore this question should be locked in my opinion because the question is unclear Commented Mar 8, 2020 at 11:37
  • @NeoMosaid can you share formatted data you get from mysql? Commented Mar 8, 2020 at 13:15

1 Answer 1

1

Here's a possible implementation:

  • cleanupLines removes all "useless" lines from the original string (it will only keep the ones with leafs),
  • lineToArray takes line parts (after splitting by |) and calls itself recursively to generate an array for the given line,
  • array_merge_recursive merges all arrays into one.

Code:

function cleanupLines(array $lines): array
{
  foreach ($lines as $index => $line) {
    if ($index > 0 && strpos($line, $lines[$index - 1]) === 0) {
      unset($lines[$index - 1]);
    }
  }
  return $lines;
}

function lineToArray(array $lineParts): array
{
  if (count($lineParts) > 1) {
    return [$lineParts[0] => lineToArray(array_slice($lineParts, 1))];
  }
  return $lineParts;
}

$lines = preg_split('/\r?\n/', $input);
$lines = cleanupLines($lines);

$result = array_reduce($lines, static function (array $result, string $line): array {
  $lineParts = explode('|', $line);
  $arrayPart = lineToArray($lineParts);
  return array_merge_recursive($result, $arrayPart);
}, []);

print_r($result);

Demo: https://3v4l.org/0F1IM

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.