Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the below given array
Array
(
[0] => 2,3
[1] => 2,3
)
I want to sum up and get result in a variable. E.g. variable a = 4 (2+2=4) and variable b = 6 (3+3=6). I am coding in php.
Use EXPLODE inside foreach
<?php
$yourArr = array('2,3','2,3');
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
$tempnew = explode(",",$temp);
$a += $tempnew[0];
$b += $tempnew[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
?>
Try this
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
$a += $temp[0];
$b += $temp[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
Output
a = 4
b = 6
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I don't know if there is a way out of this...... How can three different values be assigned to the same variable differently and echo or print the variable? Just like the code below.
$A = 'A';
$A = 'B';
$A = 'C';
echo $A;
If I echo $A we all know it going to get the last variable, so how am I going to get all the values once.
You could use an array of values like this:
$A = [];
$A[] = 'A';
$A[] = 'B';
$A[] = 'C';
echo $A[0];
...
echo $A[2];
you have to use array not variable, a variable can hold single value at a time.
there is different way to achieve this
$A = ['A', 'B', 'C'];
print_r($A);
OR
$A[] = 'A';
$A[] = 'B';
$A[] = 'C';
print_r($A);
OUTPUT
Array
(
[0] => A
[1] => B
[2] => C
)
OR
$A = 'A';
$A .= 'B';
$A .= 'C';
echo $A;
OUTPUT
ABC
simple and ugly way
<?php
$A = 'A';
$A .= 'B';
$A .= 'C';
echo $A;
OUTPUT
ABC
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have two arrays.
$a = array("one","two","three");
$b = array ( 0,1,2,3,4,5,6,7,8,9);
Look at the user contributed example in http://www.php.net/manual/en/class.infiniteiterator.php
$obj = new stdClass();
$obj->Mon = "Monday";
$obj->Tue = "Tuesday";
$obj->Wed = "Wednesday";
$obj->Thu = "Thursday";
$obj->Fri = "Friday";
$obj->Sat = "Saturday";
$obj->Sun = "Sunday";
$infinate = new InfiniteIterator(new ArrayIterator($obj));
foreach (new LimitIterator($infinate, 0, 14) as $value ) {
print($value . PHP_EOL);
}
You could create a mapping function and use array_map as follows:
function numToName($num) {
return array("zero","one","two","three","four","five",
"six","seven","eight","nine")[$num];
}
$b = array (0,8,2,3,7,5,6,0,4,1);
$result = array_map("numToName", $b);
print_r($result);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
There is an array of numbers from 0 to 10000
$a = array();
$a = range(0,10000);
I have some values which are dynamic coming from database are in array like
$b = array("100-200","400-500","700-900");
so basically i want an array that will look like
array("0-100","200-400","500-700","900-10000");
for example-> if i started a value from 0 so it will break on 100.so i will get 0-100 as first element of an array,then nothing will happen until 200.Again 200 the value will start and go to 400 and will stop then i get 200-400.After that nothing will happen until 500.it will again start with 500 and will stop on 700.so i will get third element as 500-700 and so on...
Anybody can help?
if you want your ranges to be as string element of array, try this:
<?php
$b = array("400-500","700-900","100-200");
asort($b);//new line to sort the ranges
$MIN = 0;
foreach($b as $rang){
$limits = explode('-', $rang);
$result[] = $MIN." - ".$limits[0];
$MIN = $limits[1];
}
$result[] = $MIN." - 10000";
print_r($result);
?>
You can try something like this
<?php
$b = array( "100-200","400-500","700-900" );
$c = array();
$starting = 0;
$ending = 100000;
$last = $starting;
$a = array(); // not being used
$a = range( $starting, $ending ); // not being used
foreach( $b as $k => $v )
{
$values = explode( '-' , $v);
if ( $values[0] > $starting && $values[0] < $ending )
{
$c[] = $last.'-'.$values[0];
$last = $values[1];
if ( $last <= $ending && $k == count( $b ) -1 )
{
$c[] = $last.'-'.$ending;
}
}
}
print_r( $c );
?>
Please bear in mind that I did not use the original $a array for anything. I don't understand it's purpose, unless it's not actually generated from a range, and if so this could should be changed as well
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This loop just gives me the output Array instead of the value that shows when I use the print_r function.
print_r gives me this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 1
)
but the echo just array
for($i=0; $i<($n*$n); $i++){
for($j=0; $j<($n*$n); $j++){
$number = "column" . $i . $j;
$plan = $field[$i][$j] = $_POST[$number];
$myvariable[] = $field[$i][$j];
}
echo $myvariable;
}
but if I remove the [] from $myvariable it prints out the values. the problem is that I need to use the array with an unique array
$unique = array_unique($myvariable);
if (count($unique) != count($myvariable)) {
echo ="no uniques";
}
Any tips?
You mean this ?
$unique = array_unique($myvariable);
foreach($unique as $value)
{
echo $value;
}
or make use of a typical for
$unique = array_unique($myvariable);
for($i=0;$i<count($unique);$i++)
{
echo $unique[$i];
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two array and two check boxes. The array values are
$a={1,2,3,4,5} --->It' for first Check box
$b={5,6,7,8,9} --->It' for Second Check box
When I check the first check box only I want the result is
$c[0]=1,$c[1]=2,$c[2]=3,$c[3]=4,$c[4]=5
And I check the second check box only I want the result is
$c[0]=5,$c[1]=6,$c[2]=7,$c[3]=8,$c[4]=9
And I have check both check boxes I want the result is
$c[0]={1+5},$c[1]={2+6},$c[2]={3+7},$c[3]={4+8},$c[4]={5+9}
It's Possible?
You can achieve this using a for() loop. Please note that this code assumes both arrays are the same size:
$c = array();
for($i = 0; $i < count($a); $i++)
{
$c[] = ($a[$i] + $b[$i]);
}
print_r($c);
Yes you can do it following way
<?php
$a = array(1,2,3,4,5);
$b = array(5,6,7,8,9);
$c = array(); //for result
if(isset($_POST['checkbox_1'])){
$c = $a;
}
else if(isset($_POST['checkbox_2'])){
$c = $b;
} else if(isset($_POST['checkbox_1']) && isset($_POST['checkbox_2'])){
if(count($a) == count($b)){
for($i=0;$i<count($a);$i+}){
$c[]=$a[i] + $b[i];
}
}
}else{
echo 'Error Contact Admin';
}
?>