I'm trying to display results in a foreach loop.
I have an array like so:
$slides = array(
1 => $params->get('param_1'),
2 => $params->get('param_2'),
3 => $params->get('param_3')
);
If a parameter in the backend is set to yes, the value equals 1, and thus a result is displayed.
So what I'm trying to write is, foreach array value that is equal to 1
I'm aware that I can't use something like this:
foreach (slides == 1) {
// echo stuff here
}
How would I write this foreach loop? Any help or guidelines would be highly appreciated
foreach ( $slides as $slide ) {
if ( $slide != 1 ) continue;
// echo stuff here
}
Related
I have made a PHP foreach loop for my array and I have a question:
I can show only the data less than 2 executed by the calculation? Is possible?
foreach (array_combine($fattorerov, $fattorenorm) as $valore => $valore2) {
$conteggio = $valore2." X ".$valore." = ".$valore * (0.70);
echo $conteggio."<br><br>";
}
Simply put if statment in your loop -:
If($conteggio<2){
echo $conteggio;
}
I would like to search an array for one value in a key and return the content from another key in the same array.
Like this:
$cars = array
(
array("brand" => "Volvo","color" => 22),
array("brand" => "BMW","color" => 15),
array("brand" => "Saab","color" => 5),
array("brand" => "Land Rover","color" => 17)
);
// Not working, just to clarify my intention
if($cars['brand'] == 'BMW') {echo $cars['color'];}
In this example, 15 should be echoed.
How can this be accomplished?
This should do it:
foreach($cars as $car) {
if($car['brand'] == 'BMW') {
echo $car['color'];
}
}
You need to create a loop that goes through each possible value. Often, you'd just use a for loop.
for($i = 0; $i < count($cars); $i++) {
if($cars[$i]['brand'] == "BMW")
echo $cars[$i]['color'];
}
However, using a foreach loop is also an option, and it looks neater.
foreach($cars as $v) {
if($v['brand'] == "BMW")
echo $v['color'];
}
See the array, for loop and foreach loop documentation for more information
Arrays
For loop
Foreach loop
You can use the array_filter function in PHP. See this page from the PHP docs and the given example.
I have an array stored in my database. So when I try : print_r($arrayname),
It showed the result like this:
Array
(
[0] => Color,Processor
[attribute_name] => Color,Processor
)
I want to get the values of [attribute_name] => Color,Processor .
So far I made the foreach loop like this :
foreach ($arraylist as $name) {
echo $name['attribute_name];
}
But it showing the result like cc. So can someone kindly tell me how to get the values from database?
Actually you don't need to use foreach
You can access it like this.
echo $arraylist["attribute_name"];
Please try this
foreach ($arraylist as $key => $name) {
if($key == 'attribute_name')
echo $name;
}
The problem I have is that value gets overwritten in second foreach loop. I need to set the key to image thumbnail link and set value to an image path.
$img_thumbs = array('folder/thumb1.jpg','folder/thumb2.jpg','folder/thumb3.jpg');
$img_links = array('folder/image1.jpg','folder/image2.jpg','folder/image3.jpg');
$imgs = array();
foreach($img_links as $img_val)
{
foreach($img_thumbs as $thum_val)
{
$imgs[$thum_val] = $img_val
}
}
print_r($imgs);
OUTPUT (notice how image value repeats the last value):
Array (
["folder/thumb1.jpg"] => ["folder/image3.jpg"],
["folder/thumb2.jpg"] => ["folder/image3.jpg"],
["folder/thumb3.jpg"] => ["folder/image3.jpg"]
)
WHAT I NEED:
Array (
["folder/thumb1.jpg"] => ["folder/image1.jpg"],
["folder/thumb2.jpg"] => ["folder/image2.jpg"],
["folder/thumb3.jpg"] => ["folder/image3.jpg"]
)
Thanks in advance
$imgs = array_combine($img_thumbs, $img_links);
See http://php.net/array_combine
If you absolutely wanted to do that in a loop:
foreach ($img_thumbs as $i => $thumb) {
$imgs[$thumb] = $img_links[$i];
}
you just need to get rid of one of the foreach loops and increment the Images array by 1 each time you go through the loop
I'm trying to make a very basic php ORM as for a school project. I have got almost everything working, but I'm trying to map results to an array. Here's a snippet of code to hopefully assist my explanation.
$results = array();
foreach($this->columns as $column){
$current = array();
while($row = mysql_fetch_array($this->results)){
$current[] = $row[$column];
print_r($current);
echo '<br><br>';
}
$results[$column] = $current;
}
print_r($results);
return mysql_fetch_array($this->results);
This works, but the while loop only works on the first column. The print_r($results); shows the following:
Array ( [testID] => Array ( [0] => 1 [1] => 2 ) [testName] => Array ( ) [testData] => Array ( ) )
Can anybody shed some light?
Thanks in advance!
It's because you already fetched every row, and the internal pointer is at the end.
The next while, mysql_fetch_array() will immediately return false.
You can reset the pointer to the first row:
mysql_data_seek($this->results, 0);
Put this just before
while($row = mysql_...
I'm not sure you can use the -> operator in a variable name. As you trying to get the key and value out of the array $columns? If so, you want something like this:
foreach($columns as $k => $v) {
//in here, $k is the name of the field, and $v is the associated value
}