This question already has answers here:
Multiply each integer in a flat array by 60
(5 answers)
Closed 9 years ago.
i want to mutiply all the values in a foreach array by itself
here is an example of the array
Array ( [0] => 2.4 [1] => 6 )
, all i want to do is mutiply 2.4*6
foreach($pp_events as $key=>$value)
{
#echo $value;
$pick=mysql_query("select * from pick where eventid='$value'");
$row=mysql_fetch_array($pick);
$sum*=$row['startPrice'];
}
There's no need to implement this yourself. PHP has a built-in function for multiplying all the values in an array - array_product(). Use that instead:
$products = array();
foreach ($pp_events as $key => $value) {
$pick = mysql_query("select * from pick where eventid='$value'");
$row = mysql_fetch_array($pick);
$products[] = $row['startPrice'];
}
echo array_product($products);
See also:
Why shouldn't I use mysql_* functions in PHP?
You need to initialize the variable to 1, then your loop should work.
$product = 1;
foreach ($pp_events as $key => $value) {
$pick = mysql_query("select * from pick where eventid='$value'");
$row = mysql_fetch_array($pick);
$product *= $row['startPrice'];
}
echo $product;
You can also do a single query, instead of a separate query for each value:
$events = implode(',', array_map(function($x) { return "'$x'"; }, $pp_events));
$pick = mysql_query("select startPrice from pick where eventid IN ($events)");
$product = 1;
while ($row = mysql_fetch_assoc($pick)) {
$product = $row['startPrice'];
}
echo $product;
Related
This question already has an answer here:
unserialize data from mysql
(1 answer)
Closed 7 months ago.
I want to break the line I pulled from the database and list the ip addresses
Sql Table export
a:3{i:0;s:13:"213.74.219.18";i:1;s:13:"321.32.321.32";i:2;s:14:"321.315.212.55";}
$set=mysqli_query($con,"SELECT * FROM simple_stats_options where option='ignored_ips'");
$value = mysqli_fetch_array($set,MYSQLI_ASSOC);
function arasinial($str,$birinci,$ikinci,$i) {
$bolum = explode ($birinci,$str);
$bolum = explode ($ikinci,$bolum[$i]);
return $bolum[0];
}
$array = explode('/', $var);
foreach ($array as $values)
{
}
$metin=$value["value"];
for ($x = 1; $x <= 10; $x++) {
echo arasinial($metin,':"','";',$x)."<br>";
}
Your data is serialized data, so you can just use unserialize():
<?php
$r = 'a:3:{i:0;s:13:"213.74.219.18";i:1;s:13:"321.32.321.32";i:2;s:14:"321.315.212.55";}';
print_r(unserialize($r));
will output
Array
(
[0] => 213.74.219.18
[1] => 321.32.321.32
[2] => 321.315.212.55
)
This question already has answers here:
Finding the subsets of an array in PHP
(5 answers)
Closed 7 years ago.
I will do my best to explain this idea to you. I have an array of values, i would like to know if there is a way to create another array of combined values. So, for example:
If i have this code:
array('ec','mp','ba');
I would like to be able to output something like this:
'ec,mp', 'ec,ba', 'mp,ba', 'ec,mp,ba'
Is this possible? Ideally i don't want to have duplicate entries like 'ec,mp' and 'mp,ec' though, as they would be the same thing
You can take an arbitrary decision to always put the "lower" string first. Once you made this decision, it's just a straight-up nested loop:
$arr = array('ec','mp','ba');
$result = array();
foreach ($arr as $s1) {
foreach ($arr as $s2) {
if ($s1 < $s2) {
$result[] = array($s1, $s2);
}
}
}
You can do it as follows:
$arr = array('ec','mp','ba', 'ds', 'sd', 'ad');
$newArr = array();
foreach($arr as $key=>$val) {
if($key % 2 == 0) {
$newArr[] = $val;
} else {
$newArr[floor($key/2)] = $newArr[floor($key/2)] . ',' . $val;
}
}
print_r($newArr);
And the result is:
Array
(
[0] => ec,mp
[1] => ba,ds
[2] => sd,ad
)
Have you looked at the function implode
<?php
$array = array('ec','mp','ba');
$comma_separated = implode(",", $array);
echo $comma_separated; // ec,mp,ba
?>
You could use this as a base for your program and what you are trying to achieve.
This question already has answers here:
Count number of values in array with a given value
(8 answers)
Closed 3 years ago.
I have an array named $uid. How can I check to see how many times the value "12" is in my $uid array?
Several ways.
$cnt = count(array_filter($uid,function($a) {return $a==12;}));
or
$tmp = array_count_values($uid);
$cnt = $tmp[12];
or any number of other methods.
Use array_count_values(). For example,
$freqs = array_count_values($uid);
$freq_12 = $freqs['12'];
Very simple:
$uid= array(12,23,12,4,2,5,56);
$indexes = array_keys($uid, 12); //array(0, 1)
echo count($indexes);
Use the function array_count_values.
$uid_counts = array_count_values($uid);
$number_of_12s = $uid_counts[12];
there are different solution to this:
$count = count(array_filter($uid, function($x) { return $x==12;}));
or
array_reduce($uid, function($c, $v) { return $v + ($c == 12?1:0);},0)
or just a for loop
for($i=0, $last=count($uid), $count=0; $i<$last;$i++)
if ($uid[$i]==12) $count++;
or a foreach
$count=0;
foreach($uid as $current)
if ($current==12) $count++;
$repeated = array();
foreach($uid as $id){
if (!isset($repeated[$id])) $repeated[$id] = -1;
$repeated[$id]++;
}
which will result for example in
array(
12 => 2
14 => 1
)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php - get numeric index of associative array
$array = ('a'=>'a', 'b'=>'b');
foreach($array as $key => $value ){
//echo $position ( 1,2 )
}
Can I get the position in the array with a simple function ?
Try:
$i = 0;
$array = ('a'=>'a', 'b'=>'b');
foreach($array as $key => $value ){
$i++;
echo $i;
}
$array = array('a'=>'a', 'b'=>'b');
for ($x = 0; $x < count($array);$x++)
echo $x."<br >";
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 7 months ago.
(Very useful when querying DB).
If I have a multy dim array
[['id'=>1],['id'=>2],['id'=>34],['id'=>67]]
and what I want is [1,2,34,67]
I know how to do it in code, just asking if there is a built in way in PHP (or may be in PDO) to do this.
I think you need PDO::FETCH_COLUMN mode in fetchAll, which returns only a single column as array:
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the values of the first column */
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
var_dump($result);
?>
From the manual:
To return an array consisting of all values of a single column from the result set, specify PDO::FETCH_COLUMN. You can specify which column you want with the column-index parameter.
a very easy way is to flatten your multi dimensional array
this code was extracted from http://php.net/manual/en/function.array-values.php
<?php
function array_flatten($array, $flat = false)
{
if (!is_array($array) || empty($array)) return '';
if (empty($flat)) $flat = array();
foreach ($array as $key => $val) {
if (is_array($val)) $flat = array_flatten($val, $flat);
else $flat[] = $val;
}
return $flat;
}
// will get your flattened array
print_r( array_flatten( $vals ) );
?>
Could you not just make the array you want?
$old = array( array( 'id'=>1 ), array( 'id'=>2 ), array( 'id'=>34 ), array( 'id'=>67 ) );
$arr = array();
foreach( $old as $item ) {
$arr[] = $item['id'];
}
//$arr = [ 1, 2, 34, 67 ];
Maybe array_values(array_values($array));