PDA

View Full Version : [PHP] Leggere un array un po complicato


race2
12-05-2018, 12:27
Salve,
mi date una mano a leggere questo array,
mi sono disperato 3 ore e non ci riesco.

Questo è la risposta di una chiamata ad un metodo di una mia classe, fatta attraverso SOAP.


stdClass Object ( [item] => Array ( [0] => stdClass Object ( [key] => nome [value] => Mario ) [1] => stdClass Object ( [key] => cognome [value] => Rossi ) ) )


Vorrei leggerlo cosi:


echo $MyArray['nome']; //vorrei ricevere "Mario"


Perchè mi genera un array cosi complicato ???

La mia classe:


class Language
{
public function __construct()
{
}

public function GetLanguage($_sLn)
{
switch($_sLn)
{
case 'IT' : return $this->IT(); break;
case 'EN' : return $this->EN(); break;
case 'RO' : return $this->RO(); break;
default : return $this->IT();
}
}

private function IT()
{
$aArr['nome'] = 'Mario';
$aArr['cognome'] = 'Rossi';

return $aArr;
}
}

les2
21-05-2018, 13:35
ciao,
devi semplicemente ciclare prima l'oggetto e poi l'array.

Nuke987
21-05-2018, 20:45
Io farei semplicemente così
class Language
{
public function __construct()
{

}

public function GetLanguage($_sLn = 'IT')
{
return $this->{$_sLn}();
}

private function IT()
{
return array(
'nome' => 'Mario',
'cognome' => 'Rossi',
);
}
}
Se ho $_sLn alla classe mi referenzio così:
$lang = new Language;
$temp = $lang->GetLanguage('IT');
Altrimenti semplicemente così:
$lang = new Language;
$temp = $lang->GetLanguage();
Non serve fare lo switch né tantomeno dichiarare alla fine di esso il default. Puoi dichiarare il valore predefinito direttamente nel metodo così:
public function GetLanguage($_sLn = 'IT')

race2
22-05-2018, 07:25
E' vero Nuke987 !!!
azzz.., sono veramente fuso, non so perchè ho messo uno switch, eppure in C# faccio sempre come mi hai detto tu ora..., bo!!

Solo una cosa che dall'esempio non potevi vedere:

le lingue sono molte e quindi non posso referenziarmi cosi:

$lang = new Language;
$temp = $lang->GetLanguage();


ma cosi...


$lang = new Language;
$temp = $lang->GetLanguage('IT'); //EN, FR, Etc...


Grazie molte, ciao!!

Hardware Upgrade Forum Database Error
Database Error Database error
The Hardware Upgrade Forum database has encountered a problem.

Please try the following:
  • Load the page again by clicking the Refresh button in your web browser.
  • Open the www.hwupgrade.it home page, then try to open another page.
  • Click the Back button to try another link.
The www.hwupgrade.it forum technical staff have been notified of the error, though you may contact them if the problem persists.
 
We apologise for any inconvenience.