PHP Looping through each item on a one dimensional array - php

If I have array like this myarray[0]['field1','field2','field3'];
I know its basically one row and has nothing to loop through, but i need it to loop through the values rather than the whole array. In this case it would need to loop 3x but if there were 10 fields, it should loop 10x.
I've been doing this but it feels too complicated for something so simple. Is there a function that is eluding me on google for this?
foreach (myarray[0][field1] as $item){
//do something
}
foreach (myarray[0][field2] as $item){
//do something
}
foreach (myarray[0][field3] as $item){
//do something
}

Use nested loops:
foreach ($myarray[0] as $field => $field_array){
foreach ($myarray[0][$field] as $item) {
//do something
}
}

You have a 2-dimensional array, but you want to only consider the second dimension? So treat the first dimension as a variable:
foreach ($myarray[0] as $item){
echo $item;
}
If you want to know the field name and value, then:
foreach ($myarray[0] as $key=>$value){
echo $key . ' = ' . $value;
}

foreach ($myarray[0] as $field => $field_array){
foreach ($field_array as $item) {
//do something
}
}

Related

Should I unset both `$key` and `$value` after every foreach statement?

All the answers I've found only talk about unsetting $value except one, which wasn't clear to me. So here I am. Say I have the line:
foreach ($data as $key => $value)
{
// do stuff
}
Should I put these two lines after?
unset($key);
unset($value);
Or can I omit unset($key)? Also, is it recommended to ALWAYS use unset after every foreach? Say I have nested foreach loops, like this:
foreach ($data as $key => $value)
{
foreach ($data[$key] as $key => $value)
{
// do stuff
}
unset($key);
unset($value);
}
unset($key);
unset($value);
Would the nested unset() functions interfere with the highest level foreach? In other words, are the nested $key and $value the same as the highest level $key and $value?
The solution to your problem is easier than you think it is.
Simply change the variable names for your nested loop, like so:
foreach ($data as $key => $value) {
foreach ($value as $subkey => $subvalue) {
// do stuff
}
}
With the above code, you can access the array data from $date[$key] (Which is actually equal to $value inside the parent loop) as $subkey and $subvalue respectively.
You can also still access the parent loop data anywhere inside the parent foreach loop with $key and $value respectively.

Can't access original array in foreach loop

I have a problem with my foreach loop. My foreach loop basically looks like this:
echo $values[0];
echo $values[1];
foreach ($values as $key => $value)
{
echo $values[0];
echo $values[1];
}
$values[0] should be "new york city" and $values[1] should be "new york". The problem is that in the foreach loop, both echos give the same value, whereas outside the loop they give the different (correct) values.
How do I access the original array ($values) normally inside of the foreach loop?
EDIT: I don't want to access the value $value. I want to be able to access any index of $values regardless of which iteration my foreach loop is on. I hope I am making sense.
Also, obviously I have alot more going on in this foreach loop, it was just meant as an example. The purpose of the foreach loop is not to print out these values.
Basically what the actual foreach loop I am using does is that it iterate through an array, and when it finds a certain value in that array, it iterates through the whole array again (inside the foreach loop). So basically I want a foreach loop inside a foreach loop, both for the same array, but it doesn't seem to work.
Try to use $val instead of $values[$i];
// Code goes here
foreach ($values as $key => $value)
{
echo $value;
}

Getting values after foreach loops

I have a foreach loop that gives me a number of values after looping through an array, like so:
842,844,841,839,838
This is the loop:
foreach ($values as $valuesKey => $value) {
echo $valuesKey . ',';
}
I need to work with those values after the loop finishes, how can I go about it? I want to put the list of values into another function. Is this even possible??
do_shortcode('[playlist type="audio" ids="/* values should go here */"][/playlist]');
It's supposed to look like this if it works:
do_shortcode('[playlist type="audio" ids="842,844,841,839,838"][/playlist]');
Thanks for anyone who can point me into the right direction!
There's no need for a loop. You can use implode() to join the array keys into a comma-separated string:
do_shortcode('[playlist type="audio" ids="'.implode(',', array_keys($values)).'"][/playlist]');
Try this
implode(",", array_keys($values))
Check it:
$val = array();
foreach ($values as $valuesKey => $value) {
$val[] = $valuesKey;
}
$val = implode(",", $val);
do_shortcode('[playlist type="audio" ids="'.$val.'"][/playlist]');

Getting the name of an array dynamically

I have an array which looks like this:
$example = [
['rendered'][0]['rendereditem1']
['rendered'][4]['rendereditem2 and more']
['rendered'][2]['rendereditem3']
]
Now I want to iterate with foreach to get the contents of 0,4,2!
Normally I would write:
foreach($example as $value){
print $value['rendered'][int which is the same everywhere];
}
But that is obviously not working because the array name is always different...how could I iterate in this case?
Simply add a second loop to iterate over the members :
foreach($example as $value) {
foreach($value['rendered'] as $key=>$item) {
// Do what you want here, $key is 0,4,2 in your example
}
}

PHP foreach of multidimensional array

I have an array and it has two arrays inside of it...I am able to access what I want for the first row by doing this...
print_r( $_SESSION['shopcart']['cart']['qty']);
How would I write that in a foreach?
Thanks,
J
foreach($_SESSION['shopcart']['cart']['qty'] as $value) {
echo $value;
}
you would do something like this:
to dump the array: $_SESSION['shopcart']['cart']
foreach($_SESSION['shopcart']['cart'] as $key=>$value){
echo $key." => ".$value."<br/>";
}
If you want to iterate through multiple dimensions, you can nest foreach as follows:
foreach($_SESSION['shopcart'] as $cart) {
foreach ($cart as $qty) {
// do something
}
}
Though I'd need a little more information about the array structure and what you really want to do in order to provide usable code, this is probably in the right ballpark.
I would recommend you you do do like this:
foreach($_SESSION['shopcart'] as $key=>$value){
if(is_array( $value ) ){
foreach($value => k1 => $v1){
//do something here if array
echo $k1." => ".$v1."<br/>";
}
}else{
//do something here if not array
}
}

Categories