Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Trying to fetch the first URL field from an array of them that comes from a JSON I have decoded but I get this error:
Parse error: syntax error, unexpected '[' in C:\blabla
foreach($data-> images as $data2) {
print_r(images[0]['url']);
}
I hope its enough of my code to work out what I am doing wrong?
Added: I would like the first "url" and it was getting the last one hence why I am changing the code and trying to debug it here.
Within your foreach you use the variable name you specified in the definition:
So something like...
foreach($data->images as $data2) {
print_r($data2[0]['url']);
}
Although, depending on the structure of the array, I'd imagine that you don't need the number, so it might be:
foreach($data->images as $data2) {
print_r($data2['url']);
}
If you wanted to loop through the values by a number, you'd use a for loop
for ($i = 0; $i <= count($data->images); $i++)
{
print_r($data->images[$i]);
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 13 days ago.
Improve this question
I am converting all my php scripts due to moving to a new server. I am stumped as to why $row[0] is not working.
I am correctly getting $row populated as an array, and if I run a foreach on it, I get all the values populated just fine. But if, instead, I try to directly access the first value of the array as $row[0], I get nothing. Anyone know what?
$sql = "DESCRIBE USER";
$result = $dbh->query($sql);
$count=0;
while($row = $result->fetch_assoc()) {
print $row[0]; // this prints nothing
foreach($row as $column) {
print "$column"; // this works as expected
}
} #<-- while
fetch_assoc returns your results as a key value of column names and values, so you need to look at the $row['myColumn'] to get the value.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I want to enter an multiple field entered data in table with for loop
but I am getting an error in the post method.
The error is:
Use of undefined constant i - assumed 'i' (this will throw an Error in a future version of PHP)
controller code:
$degree = $request->degree;
if($degree > 0)
{
for($i=0;$i<count($degree);$i++){
$edu = new education;
$edu->degree = $request->degree[i];
$edu->clg = $request->clg[i];
$edu->yoc = $request->yoc[i];
$edu->save();
}
}
so, please suggest me what I can do.
here there is a silly mistake bro you not remember to use $i inside loop for the degree, clg and yoc
$degree = $request->degree;
if($degree > 0)
{
for($i=0;$i<count($degree);$i++){
$edu = new education;
$edu->degree = $request->degree[$i];
$edu->clg = $request->clg[$i];
$edu->yoc = $request->yoc[$i];
$edu->save();
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The title is a bit confusing, but the error which I have is -
$xml="<Contacts>";
for($i=0;$i<count($results['records']);$i++){
$xml. = "<Contact>
<Name>".$results['records'][$i]['name']."</Name>";
}
$xml.="</Contacts>";
When I try to add something to string (concatenate) I get 500 Internal server error. I believe that problem is in " $results['records'][$i]['name']". I think the solution is to replace JSON value with variable and enclose it in {}...maybe I am wrong, i don't know.
UPD:
if I "echo ".$results['records'][$i]['name'].""; It works fine.
You have a string concatenation (.=) syntax error. Change
$xml. = "<Contact>
to
$xml .= "<Contact>
I'm strictly answering the question about the "string concatenation" PHP error. No downvotes for anything except this point, please. But yes, try not to generate XML manually. I closed </Contact>s for you below.
$xml="<Contacts>";
for ($i = 0; $i < count($results['records']); $i++) {
$xml .= "<Contact><Name>".$results['records'][$i]['name']."</Name></Contact>";
}
$xml.="</Contacts>";
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hello i am hoping someone can help me, when i use print_r all only get the last result from the mysqli query, my code is below.
//Fetch data from sql results
while($row = $rs->fetch_assoc()){
//Put results in a array
$page_query=array($row['name']=>$row['system']);
}
}
you are overwriting your $page_query everytime in loop, change to:
while($row = $rs->fetch_assoc()){
//Put results in a array
$page_query[] =array($row['name']=>$row['system']);
}
You need to add it to the array - not replace the entire variable with what you have in that row.
while($row = $rs->fetch_assoc())
{
//Put results in a array
$page_query[]=array($row['name']=>$row['system']);
}
The short syntax for the function you are looking Array_push is simply to pop a set of empty square brackets behind the variable and then say =something;. This appends another element to the end of the array. This function will increment the index numerically.
because every time inside while you reinitialize $page_query so you should push them inside array to collect. use array_push()
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Im trying to json_decode the following:
"main":{
"temp":9.04,
"temp_min":9.04,
"temp_max":9.04,
"pressure":938.13,
"sea_level":1037.57,
"grnd_level":938.13,
"humidity":87
},
"weather":[
{
"id":801,
"main":"Clouds",
"description":"few clouds",
"icon":"02d"
}
],
$result = json_decode($json);
$temp = $result->main->temp; //is displaying the data
however
$id = $result->weather->id; //is not displaying anything
all i can see is difference that te second one have an extra "[]"
can you help me telling how can i get weather->id from that json, thank you
The weather element is an array: it contains a list of elements. To get what you want from that exact example you would want:
$id = $result->weather[0]->id;
You also might want to think about what you want to happen if there is more than one element in that array, or if there are zero.