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

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题