Visualizzazione dei risultati da 1 a 4 su 4

Discussione: Array di oggetti

  1. #1

    Array di oggetti

    Ciao a tutti.

    Ho un problema nel creare un'array di oggetti nel sito che sto creando.

    In breve ho un file serieA.giornata.inc.php che contiene le classi Giornata e Partita e il file serieA.php che contiene lo script per scrivere la pagina.

    Devo caricare i dati da un file XML attraverso un ciclo for each che non riesco ad eseguire completamente perché mi visualizza un'errore alla riga di codice segnata.

    Questo è lo script:

    Codice PHP:
    foreach ($xmlLoader->GIORNATE as $root_key => $root_val)
        foreach (
    $root_val->children() as $giornata_key => $giornata_val)
        {
            
    //Imposto l'indice dell'array su zero:
            
    $current_id 0;
            
            
    //Controllo tutti gli attributi:
            
    foreach ($giornata_val->attributes() as $att => $val)
            {
                if (
    $att == "id")
                {
                    
    $current_id $val;
                    
    //Riga che mi da errore:
                    
    $giornate[$current_id] = new Giornata();
                }
                elseif (
    $att == "andata")
                    
    $giornate[$current_id]->andata $val;
                elseif (
    $att == "ritorno")
                    
    $giornate[$current_id]->ritorno $val;
                }
            
            
    $i 0;
            
            foreach (
    $root_val->children() as $child => $child_val)
                if (
    $child == "PARTITA")
                {
                    
    $giornate[$current_id]->partite[$i] = new Partita;
                        
                    foreach (
    $giornata_val->attributes() as $att => $val)
                        if (
    $att == "casa")
                            
    $giornate[$current_id]->partite[$i]->casa $val;
                        elseif (
    $att == "ospite")
                            
    $giornate[$current_id]->partite[$i]->ospite $val;
                            
                    
    $i++;
                }    
        } 
    Quando eseguo lo script, mi viene visualizzato l'errore: Warning: Illegal offset type in serieA.php on line 65.

    Grazie mille.

  2. #2
    Controlla cosa contiene $val: se e' un array o un object, bingo.

  3. #3
    Ciao ho controllato, ma $val è un numero.

    Ho fatto echo $val; e mi visualizza correttamente 1, 2, 3, 4, ... fino a 19.

    Questo è il file serieA.giornata.inc.php se può essere utile:

    Codice PHP:
    class Giornata
    {
        private 
    $_properties;
        public 
    $partite;
        
        function 
    _get($propertyName)
        {
            if (!
    array_key_exists($propertyName$this->_properties))
                throw new 
    Exception("Invalid property value.");
            
            if (
    method_exist($this'get' $propertyName))
                return 
    call_user_func(array($this'get' $propertyName));
            else
                return 
    $this->_properties[$propertyName];
        }
        
        function 
    _set($propertyName$value)
        {
            if (!
    array_key_exists($propertyName$this->_properties))
                throw new 
    Exception("Invalid property value.");
        
            if (
    method_exist($this'set' $propertyName))
                return 
    call_user_func(array($this'set' $propertyName), $value);
            else
                
    $this->_properties[$propertyName] = $value;
        }
            
        public function 
    _construct()
        {
            
    $_properties = array('andata' => NULL'ritorno' => NULL);
        }
        
    }

    class 
    Partita
    {
        
    //Dichiaro le proprietà che ci sono all'interno della classe:
        
    private $_properties;
        
        function 
    _get($propertyName)
        {
            if (!
    array_key_exists($propertyName$this->_properties))
                throw new 
    Exception("Invalid property value.");
            
            if (
    method_exist($this'get' $propertyName))
                return 
    call_user_func(array($this'get' $propertyName));
            else
                return 
    $this->_properties[$propertyName];
        }
        
        function 
    _set($propertyName$value)
        {
            if (!
    array_key_exists($propertyName$this->_properties))
                throw new 
    Exception("Invalid property value.");
            
            if (
    method_exist($this'set' $propertyName))
                return 
    call_user_func(array($this'set' $propertyName), $value);
            else
                
    $this->_properties[$propertyName] = $value;
        }
        
        public function 
    _construct()
        {
            
    $_properties = array('casa' => NULL'ospite' => NULL);
        }
        


  4. #4
    Ciao a tutti, ho trovato il problema.

    Bisognava convertire il tipo dell'indice in intero con la funzione settype.

    Così la riga: $current_id = $val;
    diventa: $current_id = settype ($val,"integer");

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 © 2026 vBulletin Solutions, Inc. All rights reserved.