Simplify looping through an array which could be flat or multidimensional - php

Often, I will have an array holding a set of values each of which I need to process. Sometimes, the array will only hold a single set, in which case each value needs to be put through the process. Other times, the array will hold many sets, in which case each value will be an array and each value of those arrays will need to be processed. Here is an example:
foreach($array as $key => $value) {
if(is_array($value)) {
foreach($value as $subkey => $subvalue) {
//Process $subvalue here
}
}
else {
//Process $value here
}
}
The problem is that the code for processing $value/$subvalue is identical except that it operates on a different variable. One way to simplify this would be to put that code into a function but it still seems inelegant to have to call it twice. Furthermore, that would leave quite a lot of code (the foreach loops and array test) outside of that function. For example, say the process is validation, I don't want to have to write two foreach loops and an array test whenever I want to call my validation function.
Is there a simpler way of doing this?

You can use a RecursiveArrayIterator to iterate over the values in the array, e.g.
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($yourArray),
RecursiveIteratorIterator:SELF_FIRST);
Then just foreach over it. Further reading:
http://www.phpro.org/tutorials/Introduction-to-SPL.html#7

Wrap the single value into an array, then proceed as usual:
foreach($array as $key => $value) {
if(!is_array($value)) $value = array($value);
foreach($value as $subkey => $subvalue) {
//Process $value/$subvalue here
}
}
Alternatively you can create a function that handles the processing of single items, then call that same function from each branch. You'll still end up saving writing the process twice.

Related

How to avoid to duplicate foreach if the logic is the same for all array?

This question is about how to produce a better code in PHP.
I've multiple arrays (5 to be exact). I've to apply the same logic to all of them.
How can I avoid to duplicate the code for all of them ?
$cpus = getCPUs(); // Get array 1 dimension with key
$rams = getRAMs(); // Get array 1 dimension with key
I mean, the code inside, I just have to create a function. That's OK.
But I still have to declare one foreach loop for each array...
Is there a way to avoid that ? It's like to have my foreach loop parameters from variables.
foreach ($cpus as $key_cpu => &$cpu) {
// FUNCTION XXX
}
foreach ($rams as $key_ram => &$ram) {
// FUNCTION XXX
}
Regards
You could simply use two foreachs:
foreach ([&$cpus, &$rams] as &$components) {
foreach ($components as $key => &$component) {
// FUNCTION XXX
}
}
Note that all those references are needed to be able to assign another value to $component and have it also modify the original values (like the question suggested). Ideally you'd want to avoid doing that if you can. If these components are arrays, explore using objects instead.
Thanks to #AterLux for the helpful comments below.
write common function/method for loop all array and return whatever your want...
function loop_array($array){
$return_data=array()
foreach ($array as $key => &$data) {
// FUNCTION XXX
}
return $return_data;
}

Manipulate an array when looping

So $tr['tree'] is an array. $dic is an array stored as key values. I want to add the key source to that those arrays. It looks like the following code doesn't work as expected as I'm guessing $dic is a new instance of the array object inside $tr['tree'].
foreach($tr['tree'] as $dic){
$dic['source'] = $tr['source']." > ".$dic['name'];
}
Note, I'm coming from python where this would work brilliantly. So how would I do this in PHP?
foreach() creates copies of the items you're looping on, so $dic in the loop is detached from the array. If you want to modify the parent array, the safe method is to use:
foreach($array as $key => $value) {
$array[$key] = $new_value;
}
You could use a reference:
foreach($array as &$value) {
^---
$value = $new_value;
}
but that can lead to stupidly-hard-to-find bugs later. $value will REMAIN a reference after the foreach terminates. If you re-use that variable name later on for other stuff, you'll be modifying the array, because the var still points at it.

How can I implement scoped foreach in PHP?

I get data from an excel spreadsheet, loop over it, save it to an array. Then, I loop over that data twice.
I do:
foreach($someData as $key => $value) {
}
and I will need to foreach that same array again. Is there any way I can make it so i can use $key => $value again without causing any problems?
It sounds like you only need one loop, but to answer your question, you can just do this:
foreach($someData as $key => $value) {
#do stuff here
}
foreach($someData as $key => $value) {
#do more stuff here
}
$key and $value will get overwritten during each iteration of each loop, so there's no danger here.
Your question is a little unclear, though—if you have a foreach inside of another foreach and want to use the same set of variable names for each loop's keys and values, use a function. That's the only way to create a new local scope in PHP:
function nested_loop($arr) {
foreach($arr as $key => $value) {
#do more stuff here
}
}
foreach($someData as $key => $value) { #same names, different variables
#do stuff here
nested_loop($value);
}

Using foreach effectively in PHP

So I don't think I'm making full use of the foreach loop. Here is how I understand foreach.
It goes like foreach(arrayyouwanttoloopthrough as onevalueofthatarray)
No counter or incrementing required, it automatically pulls an array, value by value each loop, and for that loop it calls the value whatever is after the "as".
Stops once it's done with the array.
Should basically replace "for", as long as dealing with an array.
So something I try to do a lot with foreach is modify the array values in the looping array. But in the end I keep finding I have to use a for loop for that type of thing.
So lets say that I have an array (thing1, thing2, thing3, thing4) and I wanted to change it....lets say to all "BLAH", with a number at the end, I'd do
$counter = 0;
foreach($array as $changeval){
$counter++;
$changeval = "BLAH".$counter;
}
I would think that would change it because $changeval should be whatever value it's at for the array, right? But it doesn't. The only way I could find to do that in a] foreach is to set a counter (like above), and use the array with the index of counter. But to do that I'd have to set the counter outside the loop, and it's not even always reliable. For that I'd think it would be better to use a for loop instead of foreach.
So why would you use foreach over for? I think I'm missing something here because foreach has GOT to be able to change values...
Thanks
OH HEY One more thing. Are variables set in loops (like i, or key) accessible outside the loop? If I have 2 foreach loops like
foreach(thing as value)
would I have to make the second one
foreach(thing2 as value2) ]
or else it would have some problems?
You can use a reference variable instead:
foreach ($array as &$value)
{
$value = "foo";
}
Now the array is full of foo (note the & before $value).
Normally, the loop variable simply contains a copy of the corresponding array element, but the & (ampersand) tells PHP to make it a reference to the actual array element, rather than just a copy; hence you can modify it.
However, as #Tadeck says below, you should be careful in this case to destroy the reference after the loop has finished, since $value will still point to the final element in the array (so it's possible to accidentally modify it). Do this with unset:
unset($value);
The other option would be to use the $key => $value syntax:
foreach ($array as $key => $value)
{
$array[$key] = "foo";
}
To answer your second question: yes, they are subsequently accessible outside the loop, which is why it's good practice to use unset when using reference loop variables as in my first example. However, there's no need to use new variable names in subsequent loops, since the old ones will just be overwritten (with no unwanted consequences).
You want to pass by reference:
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value)
{
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
The extended foreach syntax is like this:
foreach ($array as $key => $value) {
}
Using the $key, you can index the original array:
foreach ($array as $key => $value) {
$array[$key] = "BLAH";
}
Incrementing from 0 to ...
foreach($array as $changeval){
if (!isset($counter)) { $counter = 0; }
$counter++;
$changeval = "BLAH".$counter;
}
Using the index/key of the ARRAY
foreach($array as $key => $changeval){
$changeval = "BLAH".$key;
}
You can use the key when looping with foreach:
foreach ($array as $key => $value)
{
$array[$key] = "foo";
}
But note that using a reference like Will suggested will be faster for such a case - but anyway, the $key => $value-syntax is quite useful sometimes.

How do you remove an array element in a foreach loop?

I want to loop through an array with foreach to check if a value exists. If the value does exist, I want to delete the element which contains it.
I have the following code:
foreach($display_related_tags as $tag_name) {
if($tag_name == $found_tag['name']) {
// Delete element
}
}
I don't know how to delete the element once the value is found. How do I delete it?
I have to use foreach for this problem. There are probably alternatives to foreach, and you are welcome to share them.
If you also get the key, you can delete that item like this:
foreach ($display_related_tags as $key => $tag_name) {
if($tag_name == $found_tag['name']) {
unset($display_related_tags[$key]);
}
}
A better solution is to use the array_filter function:
$display_related_tags =
array_filter($display_related_tags, function($e) use($found_tag){
return $e != $found_tag['name'];
});
As the php documentation reads:
As foreach relies on the internal array pointer in PHP 5, changing it within the loop may lead to unexpected behavior.
In PHP 7, foreach does not use the internal array pointer.
foreach($display_related_tags as $key => $tag_name)
{
if($tag_name == $found_tag['name'])
unset($display_related_tags[$key];
}
Instead of doing foreach() loop on the array, it would be faster to use array_search() to find the proper key. On small arrays, I would go with foreach for better readibility, but for bigger arrays, or often executed code, this should be a bit more optimal:
$result=array_search($unwantedValue,$array,true);
if($result !== false) {
unset($array[$result]);
}
The strict comparsion operator !== is needed, because array_search() can return 0 as the index of the $unwantedValue.
Also, the above example will remove just the first value $unwantedValue, if the $unwantedValue can occur more then once in the $array, You should use array_keys(), to find all of them:
$result=array_keys($array,$unwantedValue,true)
foreach($result as $key) {
unset($array[$key]);
}
Check http://php.net/manual/en/function.array-search.php for more information.
if you have scenario in which you have to remove more then one values from the foreach array in this case you have to pass value by reference in for each:
I try to explain this scenario:
foreach ($manSkuQty as $man_sku => &$man_qty) {
foreach ($manufacturerSkus as $key1 => $val1) {
// some processing here and unset first loops entries
// here dont include again for next iterations
if(some condition)
unset($manSkuQty[$key1]);
}
}
}
in second loop you want to unset first loops entries dont come again in the iteration for performance purpose or else then unset from memory as well because in memory they present and will come in iterations.
There are already answers which are giving light on how to unset. Rather than repeating code in all your classes make function like below and use it in code whenever required. In business logic, sometimes you don't want to expose some properties. Please see below one liner call to remove
public static function removeKeysFromAssociativeArray($associativeArray, $keysToUnset)
{
if (empty($associativeArray) || empty($keysToUnset))
return array();
foreach ($associativeArray as $key => $arr) {
if (!is_array($arr)) {
continue;
}
foreach ($keysToUnset as $keyToUnset) {
if (array_key_exists($keyToUnset, $arr)) {
unset($arr[$keyToUnset]);
}
}
$associativeArray[$key] = $arr;
}
return $associativeArray;
}
Call like:
removeKeysFromAssociativeArray($arrValues, $keysToRemove);

Categories