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;
}
Related
I'm trying to display embedded inside the foreach loop value can display outside the loops,
but I got only one value, anyone one solve my issue
public function profit_loss_account1()
{
$deposit_amount = 0;
$discount = 0;
$deposit_fine = 0;
$deposit_date='';
$deposite=$this->studentfeemaster_model->fetch_fees_deposite();
foreach ($deposite as $deposite_value) {
$amount_detail=json_decode($deposite_value->amount_detail);
foreach ($amount_detail as $amount_detail_key=>$amount_detail_value) {
$deposit_amount= $amount_detail_value->amount;
$deposit_date=$amount_detail_value->date;
$deposit_fine = $amount_detail_value->amount_fine."<br>";
$deposit_amount .' - '.$deposit_date."<br>"; // i needed data dispaly correct here but i need outside the loops
}
}
echo $deposit_amount; // got one value here
}
display here i got correct output
$deposit_amount .' - '.$deposit_date."<br>"; // i needed data dispaly correct here but i need outside the loops
i expected output like this but outside the loops how to do
output:
1000 - 2020-02-05
12000 - 2020-02-07
0 - 2020-02-07
0 - 2020-02-07
12600 - 2019-06-29
8000 - 2019-03-12
3200 - 2019-07-26
200.00 - 2020-02-06
4000 - 2019-05-24
6000 - 2019-12-02
outside the loops
echo $deposit_amount; // got one value here
i got output like this
output :
1000 - 2020-02-05
define your $deposit_amount as empty string before the foreach loop if you want concenate it with string, dont forget to add =
for example
$deposit_amount = '';
foreach($deposit as $value) {
$deposit_amount .= ' - '.$value->date. '<br>';
}
echo $deposit_amount;
Init your var before the loop. The loop update var. So you can use it outside the loop.
example :
$var = 0;
foreach ($values as $value) {
$var ++;
}
echo $var;
I am trying to retreive some information outside of an array I have created inside of a loop.
If I hard code the array location things print out as expected. If I add the variable inside of the loop to print each array, I get nothing.
$num = 0;
foreach ( $repeatable_fields as $field ) {
$num++;
$field_playback_format = $field['playback_format'];
print_r($field_playback_format[$num]);
}
Prints nothing. But if I replace $num with a value, say 1, things echo onto the screen.
But if I do:
$num = 0;
foreach ( $repeatable_fields as $field ) {
$num++;
$field_playback_format = $field['playback_format'];
print_r($field_playback_format[1]);
}
This works as expected and prints out what I need. The reason I need to do this inside of a loop is because I have constructed multiple arrays $field_playback_format[1] and $field_playback_format[2] contain different values, but I need to retrieve them both inside the loop.
Am I not able to use a variable in this spot??
Array starts from 0, your script increment immediately the index and you have an error because the last loop try to get an element of array that doesn't exist because the index is +1 than the real value.
try this:
$num = 0;
foreach ( $repeatable_fields as $field ) {
$field_playback_format = $field['playback_format'];
print_r($field_playback_format[$num]);
$num++;
}
Your second script works because you print an existing element. But if you try to print with a major index retrieve an error and you can't see the print_r
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
}
$array['2']['21'] = null;
I want to get 21 from array above without looping, is it possible? I want to do it for those arrays which have ONLY 1 record in them. Obviously I'll have to use loop for others.
foreach ($array['2'] as $key => $value)
{
echo $key;
//I know this does the job wondering if there is simple function
}
I looked at extract() but doesn't do my job.
try this :
$arr_keys = array_keys($array['2']);
echo "<pre>";
print_r($arr_keys);
Ref: http://php.net/manual/en/function.array-keys.php
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
}