Sum up array values [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is the array: http://www.stylechica.de/array.rtf (I can't copy it here)
I need to sum up ["PriceInformation"]["PriceInformation"]["PriceDetails"]["Price"] and allready tryed all kind of stuff like
foreach ($output["PriceInformation"]["PriceDetails"]["Price"] as $product)
echo array_sum($product);
but it just won't work. Any ideas?

Because $product refers to each iteration of $output["PriceInformation"]["PriceDetails"]["Price"], you can't sum the entire array like this. The best way to do it would be to add it to a variable as you go:
$your_sum = 0;
foreach($output as $value) {
$your_sum += $value['PriceInformation']['PriceDetails']['Price'];
}
echo 'Sum: ' . $your_sum;

Related

How to calculate factorials using recursion in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I know this code to use in order to find the factorial of any number but what if we wanted it to do more and change up the code, so that it can calculate and print the factorials of number less that 100 recursively. How should this code be modified? Should i use for loop or some other technique?
Try this code, as suggested by LornaJane:
function factorial($number) {
if ($number < 2) {
return 1;
}
return ($number * factorial($number-1));
}

Return array from table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have field in my table with data: DE;RO;US;
Question is how extract data in array as:
$array = array('DE', 'RO', 'US');
use explode function.
Like this :
$string = 'DE;RO;US;';
$arr = explode(';',$string);
You can use the function explode() here.
$array = explode(';', $data);
The variable data is the result of you database query.
http://us1.php.net/manual/en/function.explode.php
BTW: Your way to save things in the database is not good. Have a look at database normalization.

Using for next loop step 50 php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want steps for using for next loop.
On a vb or vb.net
for i=0 to 1000 STEP 50
....
next
How can I use this code in php?
You can use something like this:
for ($i = 0; $i <= 1000; $i += 50)
{
// Your code...
}
It is wise to read PHP for for more details.

check array element in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Could PHP check if a certain element in the array contains some words?
for example :
`[order_product_code] => I9300_cash397096569`
want to check if the element contains cash word.
If $array is your array of element:
foreach($array as $key=>$element) {
if(strpos('cash',$element) !== FALSE) echo "$key: ok";
else echo "$key: ko";
}

Line break after several results from database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am pulling out data from a table, but instead of breaking a line after each result(or set of results), I would like to break the line after 'x' amount of those results, so I don't get long pages.
How can this be done with PHP?
You haven't posted any code so your question is going to get marked as off-topic.
If you're trying to do line breaks every X number of iterations then just do it.
$i = 0;
while ($row1 = mysql_fetch_assoc($result1))
{
if ($i % 5==0) {echo '<div>Multiple of 5, line break here</div>';}
$i++;
}
You have to use a pagination. Use something like this http://stefangabos.ro/php-libraries/zebra-pagination/

Categories