试图在stdclass对象中获取非对象的属性

时间:2018-02-28 07:43:30

标签: php object stdclass

我尝试与API集成。假设以下代码有效。

$lists = $ac->api("list/list_", array("ids" => "all"));

            echo"<pre>"; print_r( $lists); echo"</pre>";

输出以下stdclass对象

stdClass Object
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => List 3
            [cdate] => 2018-02-27 08:19:39
            [private] => 0
            [userid] => 1
            [subscriber_count] => 0
        )

    [result_code] => 1
    [result_message] => Success: Something is returned
    [result_output] => json
    [http_code] => 200
    [success] => 1
)

以下代码

            foreach($lists as $list) {

                   echo $list->id;

               }

显示错误

Trying to get property of non-object

行$ list-&gt; id错误。我该如何解决这个问题?

4 个答案:

答案 0 :(得分:1)

这是一个奇怪的结构,发生的事情是它循环result_coderesult_messageresult_outputsuccess,这些值不是对象。

要么修复它,要么检查各种循环。

foreach ($lists as $key => $list) {
    if (!is_numeric($key) || !is_object($list)) {
        continue;
    }
    echo $list->id;
}

答案 1 :(得分:0)

试试这个

foreach($lists as $list) {
       $list = (array)$list;
       if(isset($list['id'])){
           echo $list['id'];
       }
}

答案 2 :(得分:0)

PHP对象干净且易于编写。

//回应PHP数组值

SqlCommand cmd = new SqlCommand("insert into students (S_username,S_password,S_f_name,S_m_name,S_l_name,S_father_name,S_mother_name,S_dob,S_gender,S_cast_id,S_religion,S_father_occu,S_mothers_occu,S_annual_income,S_local_add,S_p_address,S_L_pincode,S_p_pincode,S_L_city,S_p_city,S_L_state,S_p_state,S_department,S_semistor,s_mob_no,s_parents_mob_no,s_email,S_aadhar_no) values ('" + txtuName.Text + "','" + txtpass.Text + "','" + Txtfname.Text + "','" + Txtmname.Text + "', '" + Txtlname.Text + "','" + TxtFthname.Text + "','" + Txtmname.Text + "','" + TextBox2.Text + "','" + DropDownList2.SelectedItem.Value + "', '" + DropDownList.SelectedItem.Value + "','" + txtrel.Text + "','" + Txtfoccu.Text + "','" + Txtmoccu .Text + "','" + DropDownList1.SelectedItem.Value + "','" + Txtlcaladd.Text + "','" + TxtpAdd.Text + "','" + Txtzipcode.Text + "', '" + Txtzipc.Text + "','" + Txtcity.Text + "', '" + Textcity.Text + "', '" + Txtstate.Text + "', '" + Textstate.Text + "', '" + DropDownList3.SelectedItem.Value + "', '" + Txtsem.Text + "','" + Txtmb.Text + "','" + Txtparmb + "','" + TxtEmail.Text + "','" + TxtAdhno.Text + "')", con);

con.Open();

cmd.ExecuteNonQuery();

txtuName.Text = string.Empty;
txtpass.Text = string.Empty;
Txtfname.Text = string.Empty;
Txtmname.Text = string.Empty;
Txtlname.Text = String.Empty;
TxtFthname.Text = String.Empty;
Txtmname.Text = String.Empty;
TextBox2.Text = String.Empty;

DropDownList2.DataTextField = "TextFiled";
DropDownList.DataTextField = "TextFiled";

txtrel.Text = string.Empty;
Txtfoccu.Text = string.Empty;
Txtmoccu.Text = string.Empty;
DropDownList1.DataTextField = "TextValues";

Txtlcaladd.Text = string.Empty;
TxtpAdd.Text = string.Empty;
Txtzipcode.Text = string.Empty;
Txtzipc.Text = string.Empty;
Txtcity.Text = string.Empty;
Textcity.Text = string.Empty;
Txtstate.Text = string.Empty;
Textstate.Text = string.Empty;
DropDownList3.DataTextField = "TextFiled";
Txtsem.Text = string.Empty;

// objnew.lastAppointmentNo = Convert.ToInt32(Request["txtLastAppointmenNo"]);
int s_mob_no = Convert.ToInt32(Request.QueryString.Get("s_mob_no"));
int s_parents_mob_no = Convert.ToInt32(Request.QueryString.Get(" s_parents_mob_no"));
//Txtmb.Text = string.Empty;
// Txtparmb.Text = string.Empty;
TxtEmail.Text = string.Empty;
int S_aadhar_no = Convert.ToInt32(Request.QueryString.Get(" S_aadhar_no"));

con.Close();
Console.WriteLine("Success");

//回应PHP对象值

echo $array['value'];

现在将PHP数组转换(转换)为PHP对象。这很简单。我只是在返回时将类型转换为Object。

echo $object->value;

以上只是一个例子。您不需要PHP函数将Array转换为Object。 (对象)函数将对任何PHP数组执行此操作。如果您需要将Object更改为Array,则使用(array)类型转换函数。

function array_to_object($array) {
    return (object) $array;
}

答案 3 :(得分:0)

从不存在[]的json返回数据时,请在解析数据之前先做一个数组。 像样本一样:

foreach(array($YourData) as $row){
    echo $row->ValueOfYourData;
}