This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 3 years ago.
I want to loop through an associate array.
foreach($details as $key=>$value){
echo $details['image1'];
}
Above code works fine.
What i want if i can replace the 1 in $details['image1'] to 2,3,4 ..etc
what i tried
$j=i;
foreach($details as $key=>$value){
echo $details['image.$j'];
$j++;
}
But it does not work.
It there a way to dynamically change the key of associate array.
like
'$details['image2'];
$details['image3'];'
You should use double quote mark
$j=i;
foreach($details as $key=>$value){
echo $details["image{$j}"];
$j++;
}
This is one way to do it
$j = $i;
$newArray = [];
foreach ($details as $key => $value) {
$newArray['image'. $j] = $value;
$j++;
}
In echo $details['image.$j']; $j is inserted as a literal.
You can either use
echo $details['image'.$j]; or
echo $details["image{$j}"];
to correctly concat.
Although you actually do not need a foreach loop using this syntax. A simple for-loop would be sufficient.
for ($i = 0; $i < count($details); $i++)
{
echo $details["image.{$i}"];
}
Using foreach you probably do not need to count up $i ... but that depends on your array.
Have a look at https://www.php.net/manual/en/control-structures.foreach.php
Related
This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 5 years ago.
Output must be "This is a testing string sample". it is correct for small array filter with index value if filter array value are more than 100 values we can't assign static index number.How can be loop to filter to my base array. i know can use array_diff but i just learn how work with for loop.
<?php
$arr = array("This","is","testing","a","string",";","sample");
$filter = array(";","a");
for($i=0; $i < count($arr); $i++){
if($arr[$i] == $filter[0] || $arr[$i] == $filter[1]){
continue;
}
echo "$arr[$i] ";
}
?>
You could filter multiple values from an array using array_diff. For this case, you don't need an loop.
$filtered = array_diff($arr, $filter);
In general, there's an function, called array_filter to filter values from an array given a predicate.
$filtered = array_filter($arr, function ($item) use ($filter) {
return !in_array($item, $filter);
});
To print your result, you could just use join to combine the whole array with a "glue".
echo join(' ', $filtered);
To fix your example, you could also loop over your filter and use continue 2, to continue the outer loop. But this is very bad practice and leads to unreadable code. So don't do this! A better solution would be an "found" flag and another check after the inner loop, if the flag is set...
for($i=0; $i < count($arr); $i++){
for ($j = 0; $j < count($filter); $j++) {
if ($arr[$i] == $filter[$j]) {
continue 2;
}
}
echo "$arr[$i] ";
}
Use in_array
foreach ($arr as $item) {
if (in_array($item, $filter) {
continue;
}
echo $item, ' ';
}
This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 8 years ago.
I am facing problem when I am trying to insert values from array to mysql database.
foreach ( $_POST['product_id'] as $key=>$value AND $_POST['discount'] as $key1=>$discount) { }
check the above given code where I am going wrong?
You can use a regular for loop as long as the indexes match:
$count = count($_POST['product_id']);
for($i = 0; $i < $count; $i++) {
echo $_POST['product_id'][$i];
echo $_POST['discount'][$i];
}
use array_map this will loop through all keys in all arrays provided simultaneously.
array_map(function(){
$args = func_get_args();
foreach($args as $k => $v) {
echo $v;
}
}, $arr1, $arr2 ...);
This question already has answers here:
php looping through multiple arrays [duplicate]
(8 answers)
Closed 9 years ago.
How can I iterate through two arrays at the same time that have equal sizes ?
for example , first array $a = array( 1,2,3,4,5);
second array $b = array(1,2,3,4,5);
The result that I would like through iterating through both is having the looping process going through the same values to produce a result like
1-1
2-2
3-3
4-4
5-5
I tried to do it this way below but it didn't work , it keeps going through the first loop again
foreach($a as $content) {
foreach($b as $contentb){
echo $a."-".$b."<br />";
}
}
Not the most efficient, but a demonstration of SPL's multipleIterator
$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($a));
$mi->attachIterator(new ArrayIterator($b));
$newArray = array();
foreach ( $mi as $value ) {
list($value1, $value2) = $value;
echo $value1 , '-' , $value2 , PHP_EOL;
}
Use a normal for loop instead of a foreach, so that you get an explicit loop counter:
for($i=0; $i<count($content)-1; $i++) {
echo $content[$i].'-'.$contentb[$i];
}
If you want to use string based indexed arrays, and know that the string indexes are equal between arrays, you can stick with the foreach construct
foreach($content as $key=>$item) {
echo $item.'-'.$contentb[$key];
}
If they're the same size, just do this:
foreach($a as $key => $content){
$contentb = $b[$key];
echo($content."-".$contentb."<br />");
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Multiple index variables in PHP foreach loop
Can we echo multiple arrays using single foreach statement?
Tried doing it in following way but wasn't successful:
foreach($cars, $ages as $value1, $value2)
{
echo $value1.$value2;
}
assuming both arrays have the same amount of elements, this should work
foreach(array_combine($cars, $ages) as $car => $age){
echo $car.$age;
}
if the arrays are not guaranteed to be the same length then you can do something like this
$len = max(count($ages), count($cars));
for($i=0; $i<$len; $i++){
$car = isset($cars[$i]) ? $cars[$i] : '';
$age = isset($ages[$i]) ? $ages[$i] : '';
echo $car.$age;
}
if you just want to join the two arrays, you can do it like this
foreach(array_merge($cars, $ages) as $key => $value){
echo $key . $value;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to find the repeating elements in an array?
If I have this array : array("hey", "test", "hey");
And I want to count how many times I have the word "hey", how can I do that?
Wouldn't it be great if there were a function like array_count_values?
</sarcasm>
Some example code of usage:
$arr = array(...);
$valCounts = array_count_values( $arr );
echo $valCounts['hey'];
I highly recommend browsing php.net and, in-particular, learning the array functions.
$count = 0;
foreach($array as $item) {
if($item == 'hey') {
$count++;
}
}
print $count;
The command is array_count_values. Check
http://www.php.net/manual/en/function.array-count-values.php
You just need to loop over the array.
$x = 0;
foreach (array("hey","test","hey") as $value) {
if ($value === "hey") $x++;
}
For a less efficient, but shorter, solution, you could use array_count_value.
$counts = array_count_values(array("hey","test","hey"));
$x = $counts["hey"];