Php Error Cannot Use For Reading
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers laravel cannot use [] for reading or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Array_push
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only array push takes a minute: Sign up Cannot use [] for reading up vote 21 down vote favorite 3 In one of my scripts, I try to do the following $data[] = self::get($row['sr_id']); // <-- line 55 However, PHP does not allow me
Php Foreach
to do this, giving me this error Fatal error: Cannot use [] for reading in /file.php on line 55 The self::get function return either a bool, or an object. Edit: The get function creates a new object which again loads data from a mysql database. php arrays fatal-error share|improve this question edited Sep 12 at 6:46 Nikhil Vaghela 1,2821422 asked Sep 29 '10 at 9:20 eriktm 3171314 2 Check what $row contains –Eiko Sep 29 '10 at 9:23 1 php array print_r($row) –Filip Ekberg Sep 29 '10 at 9:25 Can you show the surrounding code / the get method? –halfdan Sep 29 '10 at 9:30 1 Try to find out whether the error is about $data[] = or about $row['sr_id']. –Sjoerd Sep 29 '10 at 9:39 we need more context –Quamis Sep 29 '10 at 11:53 | show 2 more comments 4 Answers 4 active oldest votes up vote 11 down vote accepted Old PHP versions accepted $var[] in expressions, allowed reading out the $var content regardless of syntax. PHP 5.1 made that illegal. But sometimes the error is triggered outside of the intented context. So my guess (again: show more code) is that the preceeding line contains an unfinished expression, to which the $data[] joins. In case of object attribute you can wrap your $data var into { }, but that doesn't seem to be the problem in your case. (Else there is something in line 54, that you didn't show.) The right hand side can't reasonably trigger the error. Even array accessing [] an integer or object wouldn't trigger that fatal error. So if nothing helps, just use array_push(). Work around PHP. share|improve this answer edited Jan 29 '14 at 17:41 aksu 3,94451434 answered Sep 29 '10 at 12:36 mario 105k14138224 Thanks, the array_push() function did the trick, now it works. :) –eriktm Sep 29 '10 at 18:15 M
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up PHP: Cannot use [] for reading http://stackoverflow.com/questions/3820258/cannot-use-for-reading - but it's not [] but [$counter] up vote 1 down vote favorite in general I think I understand what the error message means. But in my case, it's a riddle I didn't succeed in solving... $keywords_all = array(); $count = 0; for ($z = 0; $z < $num_results; $z++) { $keywords_array = explode(",", $row['free_keywords']); for ($i = 0; $i < count($keywords_array); $i++) { if (in_array(strtolower(trim($keywords_array[$i])), http://stackoverflow.com/questions/13743514/php-cannot-use-for-reading-but-its-not-but-counter $keywords_all)) { $count++; } else { echo "
".$keywords_array[$i]; $keywords_all[$count] = $keywords_array[$i]; } } $row = pg_fetch_array($result); } So, what's wrong with that one? The error message pops up in the line $keywords_all[$count] = $keywords_array[$i]; I have no clue, seems to be alright to me. But guess, it's again a tiny, tiny thing I've neglected... Thanks for any hints! php arrays share|improve this question asked Dec 6 '12 at 12:19 luftikus143 553925 Split the line into two; use a temporary variable; so you can determine if the error originates right or left hand side. Also try ${'keywords_all'}[$count] etc. while you're at it (though [] operator precedence is only a problem for object props mostly). –mario Dec 6 '12 at 12:26 2 I can't reproduce this error; can you give us test values for $row['free_keywords'] and $keywords_all? –Explosion Pills Dec 6 '12 at 12:26 I split it into two lines, and it comes from "$keywords_all[$count] = $temp;". And the one in brackets {} doesn't work either. Values are just some words like "Agriculture","cereals","production","Population","female". –luftikus143 Dec 6 '12 at 12:42 foreach() might make for neater code than those for() loops. –SDC Dec 6 '12
Votes:31 Avg. Score:4.1 ± 1.0 Reproduced:30 of 30 (100.0%) Same Version:7 (23.3%) Same OS:15 (50.0%) From: https://bugs.php.net/bug.php?id=17290 mellow at mellow dot dk Assigned: Status: No Feedback Package: Scripting Engine problem PHP Version: 4.1.2 OS: Linux Private report: No CVE-ID: View Add Comment Developer Edit Have you experienced this issue? yes no don't know Rate the importance of this bug to you: high low Are you using the same PHP cannot use version? yes no Are you using the same operating system? yes no [2002-05-17 11:02 UTC] mellow at mellow dot dk I've build an array using the following in a function : ------------- $this->get_data(array("","","and D.XiParentId= 'gwak_root_element' ORDER BY D.vcOrder")); $arrCategory = $this->return_array(); for($i = 0 ; $i < sizeof($arrCategory) ; $i++) { php error cannot $this->get_data(array("","","and D.XiParentId = '" . $arrCategory[$i]["XiDataId"] . "' ORDER BY D.vcOrder")); $arrItem[$arrCategory[$i]["vcItemId"]] = $this->return_array(); } return $arrItem; ------------- This constructs an array with values like : $arrItem["project_priority"][0] = array(...) $arrItem["project_priority"][1] = array(...) etc... Later I tried to do the following to the returned $arrItem : ------------- $arrItem["project_severity"][] = array("XiDataId" => "gwak_get_all", "vcText" => "All"); ------------- This gives the error : ------------- Fatal error: Cannot use [] for reading in /home/plb/public_html/gwak/module/project/include/Task.class.php on line 160 ------------- Funny because doing the following : ------------- $arrThis["here"][] = array("XiDataId" => "asdasdas", "vcText" => "324234234"); $arrThis["here"][] = array("XiDataId" => "asdasdas", "vcText" => "324234234"); $arrThis["here"][] = array("tr" => "ert"); $arrThis["where"][] = array("er" => "ert"); $arrThis["where"][] = array("we" => "ert"); $arrThis["where"][] = array("tr" => "ert"); ------------- gives no error at all, eventhough $arrThis and $arrItem have exactly the same format. Do you guys have an idea what is going on here? The only thing i can see could cause the probl