PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP将数组传递给JSON大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个PHP文件,该文件使用array_push创建类的数组,并且需要将此数组转换为JSON格式.
该数组创建完美,但是当我尝试编码为JSON时,返回的值是一个空元素数组.@H_944_2@

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);

JSON返回的值是:

[{},{}]

谢谢

已编辑

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)

类的定义:

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;
}
}

解决方法:

您的对象属性都是私有的-它们必须是公共的,以便json_encode可以访问.或者您需要从一个对象方法调用json_encode.

大佬总结

以上是大佬教程为你收集整理的PHP将数组传递给JSON全部内容,希望文章能够帮你解决PHP将数组传递给JSON所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: