Object iteration using for loop - php

Object iteration with foreach is easy:
foreach ($item->attributes as $attribute) {
// echo $attribute->name;
}
.. but I wonder if its possible to do the same with for instead:
for ($j=0; $j < count($item->attributes); $j++) {
// echo $item->attributes->$j->$name ?
}
Although I can create a counter outside foreach and increment it, but just wanted to know if for works for objects.
For reference, the object(s) I'm working with looks like this.

for loops works for arrays which have incremented or decremented numeric index, and for associative arrays you have to use foreach unless you have separate arrays of keys, for example:
$count = count($keys);
for($i=0; $i < $count; $i++) {
echo $arr[$keys[$i]];
}
or you can reindex your assocative arrays, using array_values
$arr = array_values($assoc_array);
$count = count($arr);
for($i=0; $i < $count; $i++) {
echo $arr[$i];
}
for objects, they are properties, which can't be start from numbers, therefore you have to convert your object to array and reindex keys.
$arr = array_values(json_decode(json_encode($object), true));
$count = count($arr);
for($i=0; $i < $count; $i++) {
echo $arr[$i];
}
try to avoid above and use foreach instead.

for ($j=0; $j < count((array)$item->attributes); $j++) {
echo $item->attributes[$j];
}
Is this what you mean? You directly access this object that way, same as above, and you cast it beforehand to count.

Related

For Loop Display Fact

I'm trying to figure out why I am getting a undefined offset of 1 for this for loop I'm writting. I have an array ($facts) that has specific key values pairs and I'm trying to see if on each iteration the $i matches one of the keys in the array. If the key isset and in the array I need to display the value of that key.
for ($i = 1; $i <= 100; $i++) {
if (isset($i) && in_array($i, $facts[$i])) {
echo $facts[$i];
}
echo $i;
}
UPDATE: Use the function isset to test if the incremented value equals one of the keys in the $facts array. If there is a key that matches, display the value after the number.
I think this is the correct way of checking (removing the in_array()).
for ($i = 1; $i <= 100; $i++) {
echo $i; // Now the number is first.
if (isset($facts[$i])) {
// This is only echoed if $i exists as a key.
echo $facts[$i];
}
}
If you only want to show the number if the fact exists, move echo $i inside the if-statement (or better yet, use foreach($facts as $key => $value) in that case).
You could check $facts[$i]
for ($i = 1; $i <= 100; $i++) {
if (isset($facts[$i]) && in_array($i, $facts[$i])) {
echo $facts[$i];
}
echo $i;
}

Loop arrays and merge to a master array while not knowing names of arrays

I know how to merge arrays manually, but I need to merge arrays in a loop, while not knowing the names of the arrays or how many times they will loop.
I can do this manually:
$masterarray = array_merge_recursive($searchcustomers1, $searchcustomers2);
but how do I do it in a loop. This is what I have:
$pages is how many times it needs to loop
for ( $i = 1; $i <= $pages; $i += 1) {
$searchcustomers[$i] = $sc->call.....//an API call
}
How would I merge or append all the $searchcustomers[$i] to each other into a master array.
Maybe this can help you
$allCustomers = [];
for ($i = 1; $i <= $pages; $i += 1) {
$allCustomers = array_merge($allCustomers, $searchcustomers[$i]);
}

Making associative array

My goal is to make ant assoc array from the values of for loop.
//$from_time value is 6 and $to_time value is 23
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = $i;
}
echo json_encode($working_time_array);
The output I get on AJAX success, and when I console.log it, I get result as such :
["6",7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
Preferred result is
["6","7","8","9","10"]... etc
The only difference between the two results is one result set contains integers and the other contains strings. If you want those values to be strings just cast them when assigning them to the array:
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = (string) $i;
}
This really shouldn't be necessary unless your client side is expecting strings only.
You would need to cast $i to a string before pushing it to the array.
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = (string)$i;
}
why would you convert int to string?
for your goal this should work
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = "$i";
}
echo json_encode($working_time_array);

for loop with assignment in php

When I want to iterate an array I usually do:
foreach ($array as $a)
{
//do something with $a
}
I just want to know if is possible do something like this with a for loop:
for ($i = 0; $i < count($array); $a = $array[$i]; $i ++)
{
//do something with $a
}
Edit: When I test the code above, the for syntax is not valid.
skip the $a = $array[$i] part of the for-loop (This is assigned INSIDE of the loop and not in the declaration of the loop)
You should do like this instead:
for ($i = 0; $i < count($array); $i ++)
{
$a = $array[$i]; //Gets value of element in array $array
//Do something with it...
}
You say you want control of the element of an array.
You can do the equalivalent by doing like this (adding a key-value to the foreach-loop)
foreach ($array as $key=>$i)
{
$a = $i[$key];
}
Replace the third semicolon with a comma. It's not the entire solution (assignment is done too late), but it should explain sufficiently how a for statement works.
The for loop doesn't need to handle the $a = $array[$i]; as part of its arguments.
You can just move it to the body of the loop, something like this:
for ($i = 0; $i < count($array); $i++){
$a = $array[$i];
//do something with $a
}
for ($i = 0; $i < count($array); $i ++){
$a = $array[$i];
}
The for loop definition takes three statements you provided four. Move the assignment of $a into the actual loop.

Nested foreach loops for associative array combinations

I have an associative array as follows:
$myarray = array('a'=>array(), 'b'=>array(), 'c'=>array(), 'd'=>array());
I want to be able to get all pairs of elements in the array. If it wasn't an associative array, I would use nested for loops, like:
for($i=0; $i<count($myarray); $i++) {
for($j=$i+1; $j<count($myarray); $j++) {
do_something($myarray[$i], $myarray[$j]);
}
}
I have looked at using foreach loops, but as the inner loop goes through ALL elements, some pairs are repeated. Is there a way to do this?
Thanks!
The array_values() function returns an integer-indexed array containing all the values, so you can use it to obtain a list that you can iterate with a for.
Otherwise you can 'destroy' the array this way:
while($k = array_pop($my_array)) {
foreach($my_array as $j){
do_something($k, $j);
}
}
Try:
$keys = array_keys($myarray);
$c = count($myarray);
foreach ($keys as $k => $key1) {
for ($i = $k + 1; $i < $c; $i ++) {
dosomething($myarray[$key1], $myarray[$keys[$i]]);
}
}

Categories