drr25281 2014-11-02 20: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 18: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

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?