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;
}
}
9
  • 1
    What does var_dump($return); output? Commented May 7, 2012 at 17:23
  • is the value of $class an object? If so make sure the properties of the object are public - json_encode does not access private properties. EDIT - to clarify - json_encode can access private properties but only if run from a method within the class. Commented May 7, 2012 at 17:23
  • What does $var->findimatge($list[$i]->getIdBibl()) output? It is overwriting the previous value of $class Commented May 7, 2012 at 17:24
  • yes, it's for create the array. I modified the code tring to found the solution. Commented May 7, 2012 at 17:28
  • @PaulP.R.O. var_dump($return); shows all the array created, and all the values that is shown are correct. Commented May 7, 2012 at 17:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@TheOnyx Thank you very much, I made a mistake with the private, thank you now it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.