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

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?

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