3

I have a PHP file that create an array of class with array_push and I need to convert this array to JSON format. The array is created perfect but when I try to encode to JSON the value returned is an array of empty elements.

the code of php:

$return = array();

if($_GET['action'] == 'loadImg'){
    $id = $_GET['idAct'];
    $class = $var->selectActById($id);
    $list = $class->getImatgesAct();
    for($i = 0; $i < sizeof($list);$i++){
        $class = $var->findimatge($list[$i]->getIdBibl());
        array_push($return, $class);
    }
}
echo json_encode($return,true);

The value returned by JSON is:

[{},{}]

Thanks

EDITED

the var_dump:

array
  0 => 
object(imatgeclass)[4]
  private 'idimatge' => string '1' (length=1)
  private 'nomimatge' => string 'hydra' (length=5)
  private 'urlimatge' => string 'biblioimg/2012-05-06-23-19-17.jpg' (length=33)
  1 => 
object(imatgeclass)[3]
  private 'idimatge' => string '2' (length=1)
  private 'nomimatge' => string 'pen' (length=3)
  private 'urlimatge' => string 'biblioimg/2012-05-06-23-19-36.jpg' (length=33)

The class definition:

class imatgeclass{

private $idimatge, $nomimatge, $urlimatge;

public function setData($idimatge,$nomimatge,$urlimatge){
    $this->idimatge = $idimatge;
    $this->nomimatge = $nomimatge;
    $this->urlimatge = $urlimatge;      
}
public function getIdImatge(){
    return $this->idimatge;
}
public function setIdImatge($idimatge){
    $this->idimatge = $idimatge;
}
public function getNomImatge(){
    return $this->nomimatge;
}
public function setNomImatge($nomimatge){
    $this->nomimatge = $nomimatge;
}
public function geturlimatge(){
    return $this->urlimatge;
}
public function setUrlImatge($urlimatge){
    $this->urlimatge = $urlimatge;
}
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Frab Lopez
  • 103
  • 1
  • 3
  • 10

1 Answers1

4

Your object properties are all private - they need to be public in order to be accessible to json_encode. Or you need to call json_encode from within an object method.

TheOx
  • 2,208
  • 25
  • 28