How to get an array repeat in foreach loop - php

This is my $settings array.... and I would like to look this array in foreach so that I can read the $settings['node']['type'][$value['type']]['modifiers'] values in foreach loop and print them.

I'm can only guess you want to iterate over each element in your array.
foreach($settings as $node=>$types){
foreach($types as $values=>$modifiers){
\\ etc etc etc
echo "Settings: $node $types $values $modifiers";
}
}
Just keep nesting the foreach loops for each element you want to iterate.

Related

PHP Looping through each item on a one dimensional array

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
}
}

PHP foreach insert array

I have an array of arrays, and I am trying to foreach loop through and insert new item into the sub arrays.
take a look below
$newarray = array(
array("id"=>1,"quantity"=>2),
array("id"=>1,"quantity"=>2),
array("id"=>1,"quantity"=>2),
);
foreach($newarray as $item){
$item["total"] = 9;
}
echo "<br>";
print_r($newarray);
The result just give me the original array without the new "total". Why ?
Because $item is not a reference of $newarray[$loop_index]:
foreach($newarray as $loop_index => $item){
$newarray[$loop_index]["total"] = 9;
}
The foreach() statement gives $item as an array: Not as the real value (consuming array). That meaning it can be read but not changed unless you then overwrite the consuming array.
You could use the for() and loop through like this: see demo.
Note: This goes all the way back to scopes, you should look into that.

Add a value to array inside loop

While I map all the links on a page that is contained in an array, I want to check if each of the links is inserted in this array and, if not, insert it.
I'm trying to use the code bellow without success because "foreach $arr" doesn't pass by in the new values.
include_once('simple_html_dom/simple_html_dom.php');
$arr = array('http://www.domain.com');
foreach ($arr as $key => &$item) {
$html = file_get_html($item);
// Find category links
foreach($html->find('a[href^=http://www.domain.com/dep/]') as $element) {
if (!in_array($element->href, $arr))
$arr[] = $element->href;
}
}
print_r($arr);
Important: I need to search and add value in the original array, not in the copy (foreach).
First of all
In foreach ($arr as $key => &$item) { every $item is a STRING. (As a warning told you). So you shouldn't use $item[] here.
Next pitfall: if you want to add new items to your $arr array symtax should be
$arr[] = $some_var;
But you shouldn't do this because every time you add items to $arr, this array increases and you iterate not over two elements array, but for example 3-elements or 4 elements. Do you expect this?
You should find new values, put them in some other array and then merge both arrays.
Or use #splash58 solution. It's even simplier.

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;
}

print multidimensional array with foreach loop 3

msgs is a 2 dimension array
Is it possible to do:
foreach ($msgs['error'] as $msg)
?
I want to print only the value in "error":
msgs['error']['first value']
msgs['error']['second value']
msgs['error']['third value']
etc...
Just like this:
foreach ($msgs as $msg_outer)
print_r($msg_outer);
PHP will loop through the outer values. If you want to access the inner values, just add another foreach in the foreach:
foreach ($msgs as $msg_outer)
foreach ($msg_outer as $msg_inner)
print_r($msg_inner);
foreach ($msgs['error'] as $msg)
{
print $msg;
}
Check the array keys if you're having trouble accessing them with foreach();
print_r(array_keys($msgs));
Then work forwards from there.
Src: http://www.php.net/manual/en/function.array-keys.php

Categories