PHP Fatal error: Cannot use object of type stdClass as array -


i need on below, know raised in past struggling figure out error cannot use object of type stdclass array on line

$score[$counter] = ($bronze * $temparray[6]) + ($silver * $temparray[5]) + ($silver * $temparray[4]); 

code:

<?php        //turning date other way around why explode date string , stored in array     $gold=$_get['gold_input'];     $silver=$_get['silver_input'];     $bronze=$_get['bronze_input'];     $gdp_value=$_get['gdp_checked'];     $link = new mysqli('localhost', 'root', '','coa123cdb');     $myarray = array();     //data format information //[{"name":"ahmet akdilek","country_name":"turkey","gdp":"773091000000","population":"72752000"}     $query = "select  *  coa123cdb.country";      $result = mysqli_query($link, $query)     or die("error: ".mysqli_error($link));     $row_cnt = $result->num_rows;     if ($result = $link->query($query)) {     $temparray = array();     $scorex=array($row_cnt);     $score=(object)$scorex;     $counter=0  ;     //while($row = $result->fetch_object()) {     while($row=mysqli_fetch_object($result)){        $temparray = $row;                 if($gdp_value==0)             {             $score[$counter]=($bronze*$temparray[6])+($silver*$temparray[5])+($silver*$temparray[4]);             }             else             {$score[$counter]=($bronze*$temparray[6]+$silver*$temparray[5]+$silver*$temparray[4])*$temparray[1]/$temparray[2]/10000;             }             array_push($temparray, $score[$counter]);             array_push($myarray, $temparray);                             $counter=$counter+1;         }            //var_dump($score);     echo json_encode($myarray);     }      $result->close();     $link->close();    ?> 

take @ how declared $score.

first $scorex=array($row_cnt); , $score=(object)$scorex;.

so $score cast object. however, in code, still addressing array, i.e. $score[$counter]. should reference object.

edit

alternatively, update definition $score following:

 $score = array_fill (0, $row_cnt, 0); 

this way, assignment $score[$counter] still work (i think in way intended).


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -