Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    Problema array multidimensionale in funzione ricorsiva

    Ciao a tutti,
    ho un problema da cui non riesco proprio ad uscire. da venerdì.
    Ho una struttura dati così:

    Codice PHP:
    ---------------------------------
    ID    ID_PARENT  NAME           
    ---------------------------------
    1     0          Cat 1           
    ---------------------------------
    2     0          Cat 2           
    ---------------------------------
    3     1          Cat 1.1        
    ---------------------------------
    4     2          Cat 2.1        
    ---------------------------------
    5     1          Cat 1.2     
    ---------------------------------
    6     3          Cat 1.1.1  
    ---------------------------------
    7     1          Cat 1.3     
    ---------------------------------
    8     6          Cat 1.1.1.1  
    ---------------------------------
    9     8          Cat 1.1.1.1.1  
    --------------------------------- 
    Ho costruito una funzione ricorsiva che dovrebbe prendere questa struttura multialbero e mostrarmela in un array multidimensionale.

    La prima volta chiamo questa funzione da un'altra passandogli come $dataLevel il seguente array:

    Array (
    [1] => array(),
    [2] => array()
    )


    Codice PHP:
    public function getLevel($dataLevel$level) {
           if (
    count($dataLevel) > 0) {
                foreach(
    $dataLevel as $key => $value) {
                    
                    
    $childrensDB $this->getChildrens($key$level);
                    if (
    count($childrensDB) > 0) {
                        for (
    $i=0$i<count($childrensDB); $i++) {                    
                                                    
                            
    $dataLevel[$key][$childrensDB[$i]['ID']] = array();                        
                        }
                    }
                    
                    echo 
    "<p style=\"text-indent : ".($level 10)."px\">".$key."</p>";
                    
                    
    $this->getLevel($dataLevel[$key], $level 1);
                    
                }
            }
            
            return 
    $dataLevel;
        } 
    L'output dell'echo è corretto ed è il seguente:

    Codice PHP:
    1
      3
        6
          8
            9
      5
      7
    2
      4 

    L'output del return della funzione invece non è corretto ed è così:
    Codice PHP:
    Array (
      [
    1] => Array (
        [
    3] => Array ( )
        [
    5] => Array ( )
        [
    7] => Array ( )
      ) 
      [
    2] => Array (
        [
    4] => Array ( )
      )

    Perchè mi arriva solo fino al primo livello e non va più in profondità??
    Dove sbaglio??

    Grazie a tutti

  2. #2
    Ciao ragazzi,
    ho trovato finalmente la soluzione. A chiunque dovesse servire la posto di seguito:

    Codice PHP:
       public function getLevel(&$dataLevel$level) {
           if (
    count($dataLevel) > 0) {
                foreach(
    $dataLevel as $key => $value) {
                    
                    
    $childrensDB $this->getChildrens($key$level);
                    if (
    count($childrensDB) > 0) {
                        for (
    $i=0$i<count($childrensDB); $i++) {                    
                                                    
                            
    $dataLevel[$key][$childrensDB[$i]['ID']] = array();                        
                        }
                    }
                    
                    echo 
    "<p style=\"text-indent : ".($level 10)."px\">".$key."</p>";
                    
                    
    $this->getLevel($dataLevel[$key], $level 1);
                    
                }
            }
            
            return 
    $dataLevel;
        } 
    Come potete notare l'unica cosa che è cambiata è il passaggio dell'array $dataLevel, non è più passato per valore, ma per riferimento. In questo modo spostiamo il puntato all'interno dell'array di nostro interesse.

  3. #3
    Utente di HTML.it L'avatar di pyotrex
    Registrato dal
    Feb 2003
    Messaggi
    554
    A me serve, volevo anche capire, come si usa e cos'è getChildrens($key, $level);
    Vivrò una vita intera e fortunatamente morirò una volta sola

  4. #4
    Utente di HTML.it L'avatar di pyotrex
    Registrato dal
    Feb 2003
    Messaggi
    554
    Qualche giorno fa ho risolto, se aqualcuno può servire lascio qualche funzioncina che trasforma una base dati come quella indicata Baluba in un array multidimensionale (con l'aggiunta del nome) e poi in un rudimentale elenco puntato

    Codice PHP:
    function get_root()
    {
      
    $query  "SELECT *
                 FROM catalogo
                 WHERE id_parent=0"
    ;
      
    $result mysql_query($query) or die(mysql_error());
      while(
    $row mysql_fetch_assoc($result))
      {
        
    $array[$row['id']]['name'] = $row['name'];
      }
      return 
    $array;

    Codice PHP:
    function get_childrens($key,$level)
    {
      
    $query  "SELECT *
                 FROM catalogo
                 WHERE id_parent="
    .$key."";
      
    $result mysql_query($query) or die(mysql_error());
      
    $childrens_db = array();
      while(
    $row mysql_fetch_assoc($result))
      {
        
    $childrens_db[] = $row;
      }
      return 
    $childrens_db;

    Codice PHP:
    function get_catalog(&$data_level,$level)
    {
      if(
    count($data_level) > 0)
      {
        foreach(
    $data_level as $key => $value)
        {
          if(
    is_numeric($key))
          {
            
    $childrens_db get_childrens($key,$level);
            if(
    count($childrens_db) > 0)
            {
              for(
    $i=0$i<count($childrens_db); $i++)
              {
                
    $data_level[$key][$childrens_db[$i]['id']]['name'] = $childrens_db[$i]['name'];
              }
            }
            
    //echo "<p style=\"text-indent : ".($level * 10)."px\">".$key."</p>";
            
    get_catalog($data_level[$key],$level+1);
          }
        }
      }
      return 
    $data_level;

    Codice PHP:
    function catalog_db_to_array()
    {
      
    $data_level_root get_root();
      
    $data_level get_catalog($data_level_root,1);
      return 
    $data_level;

    Codice PHP:
    function array_to_menu($array,&$menu)
    {
      
    $menu .= '<ul>'."\n";
      foreach(
    $array as $key => $value)
      {
        if(
    is_numeric($key))
        {
          
    $menu .= '[*][url="?id='.$key.'"]'.$value['name'].'[/url]'."\n";
          if(
    is_array($value) and count($value) > 1)
          {
            
    array_to_menu($value,$menu);
          }
        }
      }
      
    $menu .= '[/list]'."\n";
      return 
    $menu;

    un esempio di utilizzo

    Codice PHP:
    $data_level catalog_db_to_array();
    echo 
    '<pre>'print_r($data_level); echo '</pre>';
    $menu '';
    echo(
    array_to_menu($data_level,$menu)); 
    Vivrò una vita intera e fortunatamente morirò una volta sola

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.