I have an object in PHP and I'm using a
foreach ($items as $item)
to iterate through the items. However, $item contains another object called $item, which contains the $type variable whose value I need to access. How can I access the value of $type? What I want to do is:
foreach($items as item){
if($item->item->type == 'this'){
//do this
} elseif ($item->item->type == 'that'){
//do that
}
}
But $item->item->type isn't picking up that value. How can I access it and use it for my function?
have you tired:
foreach($items as item){
if($item->type == 'this'){
//do this
} elseif ($item->type == 'that'){
//do that
}
}
or you can debug your code to find out what is going on:
foreach($items as item){
print_r($item);
}
and then you can see what kind of childs this $item has.
Related
I have large, single MongoDB collections and I want to add a timestamp to each of its fields. I do it that way:
$automation = Automation::all();
foreach ($automation as $workflowindex => $workflow)
{
foreach ($workflow['workflow'] as $itemindex => $items)
{
foreach ($items['subscribers'] as $index => $item)
{
foreach ($items as $name => $mail)
{
if ($name != 'subscribers') {
if ($mail['delay'] == "0" && $mail['delay_time'] == 'now') {
$automation[$workflowindex]['workflow'][$itemindex]['subscribers'][$index][$name] = round(time()/60)*60;
$automation->save();
}
}
}
}
}
}
However, if I try to save this collection I get an error saying
Indirect modification of overloaded element of App\Automation has no effect
If I use the method
Automation::all()->toArray();
I still cannot save, beause the save method doesn't work on arrays. Is there a better option to access the field in my foreach loops? If no, then how can I save this collection?
I've got a PHP object. One of the values within the object is in array. I'm trying to get a foreach to just read the array within the object but it doesn't seem to be working.
PHP:
foreach ($object->ArrayName as $value) {
echo $value;
}
is there any way of doing this?
well, if you have an object but you don't know which property is an array, you need to catch them all and verify if they are an array:
// get each value of the object and call it as property
foreach(get_object_vars($object) as $property) {
// if this property is an array...
if (is_array($property) {
// ... access to every value of that array
foreach($property as $value) {
// $property is your array and $value is every value
// here you can execute what you need
}
}
}
Use
is_array($obj)
to validate whether it's an array or not.
you said your php object contains arrays, hence you need to use 2 foreach loops like this.
<?php
foreach($object->ArrayName as $value) {
foreach($value as $item) {
echo $item;
}
}
?>
if the loop is withing the object wich mean inside the class use $this instead :
foreach ($this->ArrayName as $value) {
echo $value;
}
Please help! I have been staring at this for too long. I have a property of an object that is an array of objects. I want to pass in an object to a method of the parent object and search through that array property for a match, and if one is found return the index. Otherwise, I need it to return -1. For some reason, it is not iterating. If I echo out what should be the $order->product property (where the index is pointing during the loop), it is unchanging. I have dumped the array and I know it contains different values. I can show you a big var dump, but I figured I would first ask if there is a simple error or something else that is obvious to you that I have missed.
public function getItemIndex($prod) {
if (isset($this->orders)){
foreach($this->orders as $key => $order) {
if ($order->product == $prod) { //if I echo this $order->product to the screen, it is unchanging
return $key;
} else { return -1; }
}
}
else {
return -1;
}
}
If anyone has any ideas, I am open to discuss and post more information as needed. Thank you for your time.
You are ALWAYS returning a value on the first iteration, either the $key or -1. Try removing the else statement that you currently have. This will allow you to fully iterate over the entire array.
public function getItemIndex($prod) {
if (isset($this->orders)){
foreach($this->orders as $key => $order) {
if ($order->product == $prod) { //if I echo this $order->product to the screen, it is unchanging
return $key;
}
}
}
return -1;
}
This will ONLY return -1 once it has iterated over everything and found nothing to match. It will still return $key if it finds a match.
In a multidimensional array, I'm trying to select all descendant arrays with a certain key, no matter what their parent arrays are. I know the following syntax doesn't work, but hopefully it will help illustrate what I'm trying to accomplish:
<?php
foreach ($array[*][*]['descendant'] as $descendent) {
// do stuff
}
?>
Similarly, I need to figure out whether sibling arrays do not contain this array key. Something like this (again, I know the syntax is horribly wrong):
<?php
foreach ($array[*][*]['descendant'] < 1 as $descendent) {
// do stuff
}
?>
If there are always 3-dimensional array, you can use nested loop:
foreach($array as $lv1) {
foreach($lv1 as $lv2) {
foreach($lv2['descendant'] as $descendent) {
// do stuff
}
}
}
If you want to support unlimited number of dimension, you can use this ugly code
function drill($arr) {
if (isset($arr) && is_array($arr)) {
foreach($arr as $key => $value) {
if ($key == 'descendant') {
foreach($value as $descendent) {
// do stuff here
}
} else {
drill($value);
}
}
}
}
drill($array);
I have an array of objects I'm using to create a menu, and each object has the properties id, video_id and chapter_id.
I'd like to make a for each loop such as
foreach($menu_objects as $object WHERE $object->chapter == $variable)
Is there a way of doing this?
PHP doesn't offer syntax like that, however, you could always make it an if-statement as the first line in the loop:
foreach ($menu_objects as $object) {
if ($object->chapter != $variable) continue;
... process as normal ...
just nest an if in your loop:
foreach($menu_objects as $object){
if($object->chapter == $variable){
// do something here
}
}
Few ways
foreach(array_filter($menu_objects, function($o) { return $o->chapter == $variable}) as $object)
Or
foreach($menu_objects as $o)
{
if ($o->chapter == $variable)
{
//Code here
}
}
Just add an if?
foreach($menu_objects as $object) {
if ($object->chapter == $variable) {
// Do Something?
}
}