I had got this notice, when I try to foreach all my objects in Array:
Notice: Trying to get property of non-object With Array of objects
What can I do to get all row of my Array to put them into a table?
foreach ($this->CollectionArray[$IdVisitor] as $row) {
echo'<tr>';
echo'<td>'.$row->number.'</td>';
echo'<td>'.$this->CollectionArray[$IdVisitor]->date.'</td>';
I got error in echo'<td>'.$row->number.'</td>';
ÉDIT: When I use only collectionArray without foreach it works, but only the last row is showed . It’s why I use foreach to get all rows
Try to use it like this:
foreach ($this->CollectionArray as $row) {
echo'<tr>';
echo'<td>'.$row->number.'</td>';
echo'<td>'.$row->date.'</td>';
}
It will iterate over $this->CollectionArray, so you don't have to specify the index.
Related
I have a json_decode array which I can access the values fine with a foreach like this :
foreach ($prodvariants["result"]["sync_variants"] as $variant) {
echo $product_name = $variant['product']['name'];
}
This works great.
But what if I do not want a foreach? How can I access the same values but without the forloop?
I tried this
$variant =$prodvariants["result"]["sync_variants"];
echo $product_name = $variant['product']['name'];
But when I try it like this,without the foreach I get error
Notice: Undefined index: product
You missed one key there.
echo $product_name = $variant[0]['product']['name'];
^^^
If JSON has numeric keys, it will be [0]. If you're using another keys, just change it.
But without any loop (for, foreach, while) you can't access to all variants in one line of code. You can choose just a single record. It's a non-sense to get loop out.
I'm trying to create an associative array in PHP using an Object. The Object is values from a database, called Category. It only has two values, an id and a name field.
This is what I have:
$category = $em->getRepository('AppBundle:Category')->findAll();
$stuff = array();
foreach($category as $cat) {
$stuff[$cat->getName()] = $stuff[$cat->getId()];
}
But I get this nasty error:
Notice: Undefined offset: 1
I should say that I am using Symfony 3. Any help would be great.
You're on the right track but you need to remove echo since you're not trying to output anything. You're also attempting to assign a value from the array you're trying to enter a value into.
$stuff[ $cat->getName() ] = $cat->getId();
Sorry I just omitted the value and replace with $cat->getId() in stead of $stuff[$cat->getId()] and it worked
You $stuff array has no index 1, so it will cause that issue.
This is caused by $stuff[$cat->getId()];, which should be $cat->getId();
foreach($category as $cat) {
$stuff[$cat->getName()] = $cat->getId();
}
I don't have enough knowledge about this criteria:
I want to loop inside the for each loop to get all values in a particular column.
For example: I got the values from DB through get_result and store the result in $results.
After that use:
for each($results as $result)
❴
$output = $result->message
❵
Where message is a column in DB.
I want to loop over all the messages instead of storing last one by replacing.
Can you please give me suggestions on how to loop inside for each?
Try this:
$output[] = $result ->message;
Now $output will contain all messages on index 0, 1, 2 ...
You are facing the issue because:
$output=$result ->message;
the above line is present inside the loop, and each new iteration onerride the old value.
Well if you just looking for foreach inside foreach then you can try the following.
<?php
foreach($results as $result){
$output=$result->message;
foreach($output as $messages){
echo $messages;
}
}
?>
You don't need to put the message into another variable. You can do whatever you need to do inside the loop. For example, if you are displaying the messages, you can get it done inside the loop:
foreach ($results AS $result) {
echo $results->message . "<br>";
}
I have a foreach loop here:
foreach($decode_rewardsAPIResponse->rewardUnlockStatus as $key=>$value){
echo $value->rewardID;
}
what I am trying to achieve is to be able to get the next item in the loop's rewardID. I have tried using next($value->rewardID) but had no luck.
Here is a sample of the data i'm receiving:
{"currentMilestone":2,"rewardUnlockStatus":[{"rewardID":1,"stepsRequired":0,"unlockedForUser":true},{"rewardID":2,"stepsRequired":0,"unlockedForUser":true},{"rewardID":3,"stepsRequired":2200,"unlockedForUser":false},{"rewardID":4,"stepsRequired":5200,"unlockedForUser":false},{"rewardID":5,"stepsRequired":8200,"unlockedForUser":false},{"rewardID":6,"stepsRequired":11200,"unlockedForUser":false},{"rewardID":7,"stepsRequired":14200,"unlockedForUser":false},{"rewardID":8,"stepsRequired":17200,"unlockedForUser":false},{"rewardID":9,"stepsRequired":20200,"unlockedForUser":false},{"rewardID":10,"stepsRequired":23200,"unlockedForUser":false},{"rewardID":11,"stepsRequired":26200,"unlockedForUser":false},{"rewardID":12,"stepsRequired":29200,"unlockedForUser":false},{"rewardID":13,"stepsRequired":32200,"unlockedForUser":false},{"rewardID":14,"stepsRequired":35200,"unlockedForUser":false},{"rewardID":15,"stepsRequired":38200,"unlockedForUser":false}]}
I am trying to access to this object $results, and I can't figure out why the first echo and the print_r give me the expected output, but the last echo gives me the error "Undefined property".
foreach($results as $number => $hotel){
echo $number;
print_r($hotel);
echo $results->{$number};
}
-----------------------------------Better explanation---------------------------------
I noticed that you need some other informations to help me, so:
$results is an object containing results of a query: it contains a lis of hotels (objects) and everyone of them is descripted by some fields->value.
In order to modify this object (delete one hotel from the object $results) I was looking for the method to reference an item of the object $results, and I thought that the way was $results->{$number}
So the thing that i really want to do is:
foreach($results as $number => $hotel){
foreach($hotel as $field => $value) {
if($field == 'id' && $value == '18'){
// here I wanna delete the hotel from the object $results
// but unset($results->{$number}) doesn't work.
}
}
}
The foreach as you show is normaly use with an array. So to access the items you need to use the good operator that is []
In the foreach the $number is the key , the $hotel are items in the array.
So to acces an hotel you need $result[$number]