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

    Leggere array associativo e cambiare valore

    Ciao a tutti!

    sono alle prime armi in php e dovrei leggere questo array e poter cambiare per ogni riga la proprieta "id" assegnandogli il valore che contiene +1;

    questo è l'array:

    array(2) {
    [0]=>
    array(4) {
    ["id"]=>
    string(6) "CE0000"
    ["text"]=>
    string(30) "CESPITI CLIENTE "
    ["leaf"]=>
    bool(false)
    ["children"]=>
    array(2) {
    [0]=>
    array(3) {
    ["id"]=>
    string(6) "CE0001"
    ["text"]=>
    string(30) "GESTIONE CESPITI SCHEDA "
    ["leaf"]=>
    bool(true)
    }
    [1]=>
    array(4) {
    ["id"]=>
    string(6) "CE0004"
    ["text"]=>
    string(30) "MENU SOTTO ROOT "
    ["leaf"]=>
    bool(false)
    ["children"]=>
    array(1) {
    [0]=>
    array(4) {
    ["id"]=>
    string(6) "MN0301"
    ["text"]=>
    string(30) "CHIAMO MN02 "
    ["leaf"]=>
    bool(false)
    ["children"]=>
    array(2) {
    [0]=>
    array(3) {
    ["id"]=>
    string(6) "MN0201"
    ["text"]=>
    string(30) "SCHEDA SOTTO SOTTOMENU "
    ["leaf"]=>
    bool(true)
    }
    [1]=>
    array(3) {
    ["id"]=>
    string(6) "MN0202"
    ["text"]=>
    string(30) "SCHEDA 2 "
    ["leaf"]=>
    bool(true)
    }
    }
    }
    }
    }
    }
    }
    [1]=>
    array(4) {
    ["id"]=>
    string(6) "PE0000"
    ["text"]=>
    string(30) "GESTIONE PERSONALE "
    ["leaf"]=>
    bool(false)
    ["children"]=>
    array(3) {
    [0]=>
    array(3) {
    ["id"]=>
    string(6) "PE0001"
    ["text"]=>
    string(30) "GESTIONE DATI DIPENDENTE "
    ["leaf"]=>
    bool(true)
    }
    [1]=>
    array(3) {
    ["id"]=>
    string(6) "PE0002"
    ["text"]=>
    string(30) "GESTIONE DATI GIORNALIERI "
    ["leaf"]=>
    bool(true)
    }
    [2]=>
    array(4) {
    ["id"]=>
    string(6) "PE0003"
    ["text"]=>
    string(30) "SOTTOMENU "
    ["leaf"]=>
    bool(false)
    ["children"]=>
    array(2) {
    [0]=>
    array(3) {
    ["id"]=>
    string(6) "MN0101"
    ["text"]=>
    string(30) "PGM Menu figlio "
    ["leaf"]=>
    bool(true)
    }
    [1]=>
    array(4) {
    ["id"]=>
    string(6) "MN0102"
    ["text"]=>
    string(30) "SOTTO SOTTOMENU "
    ["leaf"]=>
    bool(false)
    ["children"]=>
    array(2) {
    [0]=>
    array(3) {
    ["id"]=>
    string(6) "MN0201"
    ["text"]=>
    string(30) "SCHEDA SOTTO SOTTOMENU "
    ["leaf"]=>
    bool(true)
    }
    [1]=>
    array(3) {
    ["id"]=>
    string(6) "MN0202"
    ["text"]=>
    string(30) "SCHEDA 2 "
    ["leaf"]=>
    bool(true)
    }
    }
    }
    }
    }
    }
    }
    }


    non so se è chiara la cosa, ho fatto un var_dump dell'array! per ogni "casellina" id dovrei aggiungere un +1

    Grazie mille!
    Ristoranti Roma
    http://www.ristorantiromaristo.it
    info@ristorantiromaristo.it

  2. #2
    Codice PHP:

    //ESEMPIO ERRATO!!!
    foreach($tuoarray as &$elemento)
       
    $elemento['id'] += 1
    scusa come si fa ad aggiungere un +1 ad una cosa che non è un numero? mi fai un esempio di come dovrebbe cambiare un ID
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

  3. #3
    scusami volevo intendere che l'id CE0000 diventa CE00001

    ovvero ci "concateno" un valore numerico preso da un contatre che incremento sempre per rendere questo id univoco

    grazie 1000
    Ristoranti Roma
    http://www.ristorantiromaristo.it
    info@ristorantiromaristo.it

  4. #4
    Originariamente inviato da blow1986
    scusami volevo intendere che l'id CE0000 diventa CE00001

    ovvero ci "concateno" un valore numerico preso da un contatre che incremento sempre per rendere questo id univoco

    grazie 1000
    non fai prima a mettere un altro campo per distinguerli, tipo:

    Codice PHP:

    $auto_increment 
    0;
    foreach(
    $tuoarray as &$elemento
       
    $elemento['fake_id'] = ++$auto_increment
    e poi li distingui per fake_id? oppure per la chiave dell'array (0,1,2,...)? cambiare l'id...boh...poi scusa, se si chiama ID vorrà dire che di suo è già univoco no? come fai ad avere dei doppioni?
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

  5. #5
    non so, se proprio vuoi aumentare quel numero:

    Codice PHP:

    $increment 
    0;
    //dando per scontato che tutti gli id siano due lettere a sinistra e 4 numeri a destra
    foreach($tuoarray as &$element){
        
        
    $c substr($element['id'],0,2);
        
    $n = (int)substr($element['id'],-4);
        
        
    $element['id'] = $c.str_pad($n+(++$increment),4,"0",STR_PAD_LEFT);

    solo che un codice tipo GS9998, se lo aumenti di più di due valori diventa a 5 numeri e questo codice non andrebbe più bene ad un passaggio successivo... boh non so che cosa devi ottenere e quali siano le problematiche sinceramente
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

  6. #6
    mi basterebbe anche una cosa di questo tipo:

    Codice PHP:
    $indice 0;
    foreach (
    $root as &$ele){
          
    $indice=$indice+1;
          
    $ele['id'] = $ele['id'].$indice;

    Quello che però non riesco a fare, e la stasse cosa appena fatta per i "sottoArray" ovvero (sicuramente non è molto chiaro dal mio primo post ma ora ho inserito i tag php), in molti casi è presente l'indice "children" che contiene ulteriori array (posso avere anche qui dei children) e devo modifcare anche questo id.

    Codice PHP:
    array(2) {
      [
    0]=>
      array(
    4) {
        [
    "id"]=>
        
    string(6"CE0000"
        
    ["text"]=>
        
    string(30"CESPITI CLIENTE               "
        
    ["leaf"]=>
        
    bool(false)
        [
    "children"]=>
        array(
    2) {
          [
    0]=>
          array(
    3) {
            [
    "id"]=>
            
    string(6"CE0001"
            
    ["text"]=>
            
    string(30"GESTIONE CESPITI SCHEDA       "
            
    ["leaf"]=>
            
    bool(true)
          }
          [
    1]=>
          array(
    4) {
            [
    "id"]=>
            
    string(6"CE0004"
            
    ["text"]=>
            
    string(30"MENU SOTTO ROOT               "
            
    ["leaf"]=>
            
    bool(false)
            [
    "children"]=>
            array(
    1) {
              [
    0]=>
              array(
    4) {
                [
    "id"]=>
                
    string(6"MN0301"
                
    ["text"]=>
                
    string(30"CHIAMO MN02                   "
                
    ["leaf"]=>
                
    bool(false)
                [
    "children"]=>
                array(
    2) {
                  [
    0]=>
                  array(
    3) {
                    [
    "id"]=>
                    
    string(6"MN0201"
                    
    ["text"]=>
                    
    string(30"SCHEDA SOTTO SOTTOMENU        "
                    
    ["leaf"]=>
                    
    bool(true)
                  }
                  [
    1]=>
                  array(
    3) {
                    [
    "id"]=>
                    
    string(6"MN0202"
                    
    ["text"]=>
                    
    string(30"SCHEDA 2                      "
                    
    ["leaf"]=>
                    
    bool(true)
                  }
                }
              }
            }
          }
        }
      }
      [
    1]=>
      array(
    4) {
        [
    "id"]=>
        
    string(6"PE0000"
        
    ["text"]=>
        
    string(30"GESTIONE PERSONALE            "
        
    ["leaf"]=>
        
    bool(false)
        [
    "children"]=>
        array(
    3) {
          [
    0]=>
          array(
    3) {
            [
    "id"]=>
            
    string(6"PE0001"
            
    ["text"]=>
            
    string(30"GESTIONE DATI DIPENDENTE      "
            
    ["leaf"]=>
            
    bool(true)
          }
          [
    1]=>
          array(
    3) {
            [
    "id"]=>
            
    string(6"PE0002"
            
    ["text"]=>
            
    string(30"GESTIONE DATI GIORNALIERI     "
            
    ["leaf"]=>
            
    bool(true)
          }
          [
    2]=>
          array(
    4) {
            [
    "id"]=>
            
    string(6"PE0003"
            
    ["text"]=>
            
    string(30"SOTTOMENU                     "
            
    ["leaf"]=>
            
    bool(false)
            [
    "children"]=>
            array(
    2) {
              [
    0]=>
              array(
    3) {
                [
    "id"]=>
                
    string(6"MN0101"
                
    ["text"]=>
                
    string(30"PGM Menu figlio               "
                
    ["leaf"]=>
                
    bool(true)
              }
              [
    1]=>
              array(
    4) {
                [
    "id"]=>
                
    string(6"MN0102"
                
    ["text"]=>
                
    string(30"SOTTO SOTTOMENU               "
                
    ["leaf"]=>
                
    bool(false)
                [
    "children"]=>
                array(
    2) {
                  [
    0]=>
                  array(
    3) {
                    [
    "id"]=>
                    
    string(6"MN0201"
                    
    ["text"]=>
                    
    string(30"SCHEDA SOTTO SOTTOMENU        "
                    
    ["leaf"]=>
                    
    bool(true)
                  }
                  [
    1]=>
                  array(
    3) {
                    [
    "id"]=>
                    
    string(6"MN0202"
                    
    ["text"]=>
                    
    string(30"SCHEDA 2                      "
                    
    ["leaf"]=>
                    
    bool(true)
                  }
                }
              }
            }
          }
        }
      }
    }


    array(
    2) {
      [
    0]=>
      array(
    4) {
        [
    "id"]=>
        
    string(7"CE00001"
        
    ["text"]=>
        
    string(30"CESPITI CLIENTE               "
        
    ["leaf"]=>
        
    bool(false)
        [
    "children"]=>
        array(
    2) {
          [
    0]=>
          array(
    3) {
            [
    "id"]=>
            
    string(6"CE0001"
            
    ["text"]=>
            
    string(30"GESTIONE CESPITI SCHEDA       "
            
    ["leaf"]=>
            
    bool(true)
          }
          [
    1]=>
          array(
    4) {
            [
    "id"]=>
            
    string(6"CE0004"
            
    ["text"]=>
            
    string(30"MENU SOTTO ROOT               "
            
    ["leaf"]=>
            
    bool(false)
            [
    "children"]=>
            array(
    1) {
              [
    0]=>
              array(
    4) {
                [
    "id"]=>
                
    string(6"MN0301"
                
    ["text"]=>
                
    string(30"CHIAMO MN02                   "
                
    ["leaf"]=>
                
    bool(false)
                [
    "children"]=>
                array(
    2) {
                  [
    0]=>
                  array(
    3) {
                    [
    "id"]=>
                    
    string(6"MN0201"
                    
    ["text"]=>
                    
    string(30"SCHEDA SOTTO SOTTOMENU        "
                    
    ["leaf"]=>
                    
    bool(true)
                  }
                  [
    1]=>
                  array(
    3) {
                    [
    "id"]=>
                    
    string(6"MN0202"
                    
    ["text"]=>
                    
    string(30"SCHEDA 2                      "
                    
    ["leaf"]=>
                    
    bool(true)
                  }
                }
              }
            }
          }
        }
      }
      [
    1]=>
      &array(
    4) {
        [
    "id"]=>
        
    string(7"PE00002"
        
    ["text"]=>
        
    string(30"GESTIONE PERSONALE            "
        
    ["leaf"]=>
        
    bool(false)
        [
    "children"]=>
        array(
    3) {
          [
    0]=>
          array(
    3) {
            [
    "id"]=>
            
    string(6"PE0001"
            
    ["text"]=>
            
    string(30"GESTIONE DATI DIPENDENTE      "
            
    ["leaf"]=>
            
    bool(true)
          }
          [
    1]=>
          array(
    3) {
            [
    "id"]=>
            
    string(6"PE0002"
            
    ["text"]=>
            
    string(30"GESTIONE DATI GIORNALIERI     "
            
    ["leaf"]=>
            
    bool(true)
          }
          [
    2]=>
          array(
    4) {
            [
    "id"]=>
            
    string(6"PE0003"
            
    ["text"]=>
            
    string(30"SOTTOMENU                     "
            
    ["leaf"]=>
            
    bool(false)
            [
    "children"]=>
            array(
    2) {
              [
    0]=>
              array(
    3) {
                [
    "id"]=>
                
    string(6"MN0101"
                
    ["text"]=>
                
    string(30"PGM Menu figlio               "
                
    ["leaf"]=>
                
    bool(true)
              }
              [
    1]=>
              array(
    4) {
                [
    "id"]=>
                
    string(6"MN0102"
                
    ["text"]=>
                
    string(30"SOTTO SOTTOMENU               "
                
    ["leaf"]=>
                
    bool(false)
                [
    "children"]=>
                array(
    2) {
                  [
    0]=>
                  array(
    3) {
                    [
    "id"]=>
                    
    string(6"MN0201"
                    
    ["text"]=>
                    
    string(30"SCHEDA SOTTO SOTTOMENU        "
                    
    ["leaf"]=>
                    
    bool(true)
                  }
                  [
    1]=>
                  array(
    3) {
                    [
    "id"]=>
                    
    string(6"MN0202"
                    
    ["text"]=>
                    
    string(30"SCHEDA 2                      "
                    
    ["leaf"]=>
                    
    bool(true)
                  }
                }
              }
            }
          }
        }
      }

    Ristoranti Roma
    http://www.ristorantiromaristo.it
    info@ristorantiromaristo.it

  7. #7
    Codice PHP:


    $ids
    = array(
        array(
    "id" => "CD0001",
            
    'children' => array(
                array(
    'id' => 'pi0012''children' => array(array('id' => 'pi0010')))
            )
        ),
        array(
    "id" => "FC0128"),
        array(
    "id" => "GS9998",'children' =>
            array(
                array(
    'id' => 'ee0001')
            )
        ));
    $increment 0;

    function 
    modificaID(&$array,&$increment){

        foreach(
    $array as &$element){
            
            
    $element['id'] = $element['id'].++$increment;
            if(
    array_key_exists('children',$element) && $element['children'])
                
    modificaID($element['children'],$increment);
        }
        
    }

    modificaID($ids,$increment);

    print_r($ids); 
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

  8. #8
    Perfetto!

    ti ringrazio!
    Ristoranti Roma
    http://www.ristorantiromaristo.it
    info@ristorantiromaristo.it

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.