drr25281 2014-11-02 12:21
浏览 202
已采纳

php对象的数组无法获取对象属性(类stdClass的对象无法转换为字符串)

been searching for a solution all day long...

my variable name is $categoryFetchedAsObj var_dump results :

array(104) {
[0]=>
object(stdClass)#411 (1) {
["categoryHive"]=>
string(4) "asfa"
}

[1]=>
object(stdClass)#412 (1) {
["categoryHive"]=>
string(13) "ENERGY DRINKS"
}

[2]=>
object(stdClass)#413 (1) {
["categoryHive"]=>
string(7) "KETCHUP"
}

[3]=>
object(stdClass)#414 (1) {
["categoryHive"]=>
string(6) "STICKS"
}

and so on

i call a function...

createSelectBlock("category",$categoryFetchedAsObj,"onchange=\"this.form.submit()\"",null,'categoryHive');

here is the instance of the function

createSelectBlock($name,$fetchedNamesAsArrayOfObj,$selectEvent,$objPropertyNameForOptionsValue,$objPropertyNameForOptionsName)

and here is where the error raises

foreach ($fetchedNamesAsArrayOfObj as $rowArrayObj) {

                if(isset($objPropertyNameForOptionsValue) && isset($objPropertyNameForOptionsName)){
                $selectBlock.="<option value=\"$rowArrayObj->$objPropertyNameForOptionsValue\">$rowArrayObj->$objPropertyNameForOptionsName</option>";
                }
                if(!isset($objPropertyNameForOptionsValue) && isset($objPropertyNameForOptionsName)){
                $selectBlock.="<option value=\"$rowArrayObj->$objPropertyNameForOptionsName \">$rowArrayObj->$objPropertyNameForOptionsName</option>";
                }

        }               

(Object of class stdClass could not be converted to string)

if i replace the $rowArrayObj->$objPropertyNameForOptionsName with

$rowArrayObj->categoryHive

everything works but the args of the function are not the same all the time .. obviously

i search this site found some suggestions.. but nothing worked here is what i tryied

$objPropertyNameForOptionsName='$objPropertyNameForOptionsName';
$rowArrayObj->{'$objPropertyNameForOptionsName'};

any ideas?

展开全部

  • 写回答

1条回答 默认 最新

  • dskld5423 2014-11-03 10:54
    关注

    after testing and over testing .. finlay i got it working... this was the correct answer for my situation

    if(isset($objPropertyNameForOptionsValue) && isset($objPropertyNameForOptionsName)){
                         $selectBlock.="<option value=".$rowArrayObj->$objPropertyNameForOptionsValue.">".$rowArrayObj->$objPropertyNameForOptionsName."</option>";
    }
    if(!isset($objPropertyNameForOptionsValue) && isset($objPropertyNameForOptionsName)){
                    $selectBlock.="<option value=".$rowArrayObj->$objPropertyNameForOptionsName.">".$rowArrayObj->$objPropertyNameForOptionsName."</option>";
    }
    

    hope it helped someone

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部