I am new on coding can you help me with accessing this I am stuck here:
foreach($questions_answers as $correct) {
dd($correct->answers);
}
And this is what I am getting so far but I want to access correct how can I achieve that..?
You loop over the items in the collection and then you can access the individual properties:
foreach($questions_answers as $questions_answer) {
dd($questions_answer->correct);
}
try:
foreach ($questions_answers as $correct) {
foreach ($correct->answers as $answer) {
if ($answer->correct == 1) {
dd($answer);
}
}
}
Related
So I have a function that currently has a foreach and it works amazing, but I'm being forced to change it to a while loop:
PLEASE NOTE: The developers at my company don't want to use the foreach and they think that a while loop would be more efficient, but I'm not understanding how that would be executed, so I need some help.
So I have the following function ($post_blocks is an array of arrays):
public function parse_block_data(string $block_name, string $selector, $post_id)
{
if (!has_blocks($post_id)) {
return false;
}
$post_blocks = parse_blocks(get_the_content('', false, $post_id));
foreach ($post_blocks as $block) {
if ($block_name != $block['blockName']) {
continue;
}
if (!isset($block['attrs']['id'])) {
return false;
}
if (isset($block['attrs']['data'][$selector])) {
return $block['attrs']['data'][$selector];
} else {
break;
}
}
return false;
}
It uses the parameters to build up an array as shown below:
Output
So I started building a while loop inside the function, but I'm clueless on how to achieve it without using a foreach or if it's even possible, so I replaced the foreach with:
// I get the 9 counts of $post_blocks correctly.
$block = 0;
while ($block < count($post_blocks))
// If the $block_name doesn't match `blockName` value inside the multi-dimensional array, then continue iterating until the end and then return false.
// If ['attrs']['id'] is not set, return false.
// At last, if we have a blockName and a ID and the selector is set, return ['attrs']['data'][$selector]
}
All help will be appreciated! It makes no sense to me, but if someone can assist, I'd be forever grateful!
It's basically the same as your foreach loop, you just set the iteration variable by indexing the array, and increment the index manually.
$block_num = 0;
while ($block_num < count($post_blocks)) {
$block = $post_blocks[$block_num];
if ($block_name == $block['blockName']) {
if (!isset($block['attrs']['id'])) {
return false;
}
if (isset($block['attrs']['data'][$selector])) {
return $block['attrs']['data'][$selector];
} else {
break;
}
}
$block_num++;
}
I'm not sure why your colleagues think this is preferable.
If there's a company coding style they want you to follow, why don't you ask them what it should be?
I have the following code to iterate through json array and change the value in the array.
<?php
$json='[{"type":"dropdown","label":"Is the property tenanted ?","req":0,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[{"NoteLabel":"Real Estate Agency ?","NoteValue":"","reqNote":"1"},{"NoteLabel":"Agents Mobile Number:","NoteValue":"","reqNote":"1"},{"NoteLabel":"Agents Email:","NoteValue":"","reqNote":"0"},{"PhotoLabel":"Attach a photo","PhotoValues":"","reqPhoto":"1"},{"NoteLabel":"Tenants Contact Number:","NoteValue":"","reqNote":"0"}]},{"label":"No","sel":1,"notification":0,"subOptions":[{"NoteLabel":"Clients Contact Number:","NoteValue":"","reqNote":"1"},{"PhotoLabel":"Attach a photo","PhotoValues":"","reqPhoto":"1"}]},{"label":"N\/A","sel":0,"notification":0,"subOptions":[]}]}]';
echo $json."<br/>";
echo "<br/><br/><br/>****************<br/><br/><br/>";
$json=json_decode($json,true);
foreach($json as $kSub => $vSub)
{
if( in_array($vSub['type'], ["dropdown"]))
{
if($vSub['label']=="Is the property tenanted ?")
{
$choices=&$vSub['choices'];
foreach($choices as $keyChoice=>&$valChoice)
{
if($valChoice['label']=="Yes")
{
$subOptions=&$valChoice['subOptions'];
foreach($subOptions as $kop=>&$Opval)
{
foreach($Opval as $kn=>&$vn)
{
if($kn=="NoteLabel")
{
if($vn=="Real Estate Agency ?")
{
$subOptions[$kop]['NoteValue']="DOMAIN";
}
}
}
}
}
}
}
}
}
echo json_encode($json)."<br/>";
I want to change the NoteValue inside subOptions array if the conditions are met. I am not sure whether I am doing it right or not, but the value is not changing. Please help me to sort out what I am doing wrong! I was also wondering whether I can reduce the number of code lines to get the result?
You need to use &vSub to make it a reference variable.
You can shorten the code by getting rid of the last loop, and just access the NoteLabel index directly. You can also combine the first two tests with &&. And you don't need any of the index variables in your foreach loops.
foreach($json as &$vSub)
{
if($vSub['type'] == "dropdown" && $vSub['label']=="Is the property tenanted ?")
{
$choices=&$vSub['choices'];
foreach($choices as &$valChoice)
{
if($valChoice['label']=="Yes")
{
$subOptions=&$valChoice['subOptions'];
foreach($subOptions as &$Opval)
{
if (isset($Opval['NoteLabel']) && $Opval['NoteLabel'] == "Real Estate Agency ?")
{
$Opval['NoteValue']="DOMAIN";
}
}
}
}
}
}
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?
}
}
I would like to loop through the contents of a query object update certain values and return the object.
function clearAllIds($queryObject)
{
foreach($queryObject->result() as $row)
{
$row->id = 0;
}
return $queryObject
}
In this example I would like to zero out all of the ID values. How can I accomplish this within the foreach loop?
Please excuse the formatting.
This entirely depends on what the class of your query object is, and whether or not you'll be able to Pass by reference.
Assuming your $queryObject->result() can be delivered in a write-context, you could preface the $row with an ampersand to pass it by reference, like so:
foreach($queryObject->result() as &$row)
{
$row->id = 0;
}
function clearAllIds($queryObject)
{
foreach($queryObject->result() as &$row)
{
$row->id = 0;
}
return $queryObject
}
Use the & operator to get $row as a reference.
Edit: This will work if $queryObject is an array. You should probably do
$data = $queryObject->result();
foreach($data as &$row) { ... }
return $data;
function trim_spaces($object)
{
foreach (get_object_vars($object) as $property=> $value)
{
$object->$property=trim($value);
}
}
//no need to return object as they are passed by reference by default