I want to check the longest side of an triangle:
The lenghts and the sides are saved in an array ($allSides)
Now I want to display the lagest side of all! I Want to check
the longest with an "foreach-action":
<?php
triangle(1, 5, 5);
function triangle($a, $b, $c) {
$allSides = array(
'a' => $a,
'b' => $b,
'c' => $c
);
if($a < $b + $c || $b < $a + $c || $c < $a + $b) {
echo " | Das Dreieck ist konstruierbar! <br>";
$U = $a + $b + $c;
echo " | Umfang: ".$U."<br>";
$longestSide = max($allSides);
$allSidesKeys = getSideKey($longestSide, $allSides);
$keys = implode(", ", $allSidesKeys);
echo "| Längste Seite(".$keys."): ".$longestSide."<br>";
} else {}
}
function getSideKey($longestSide, $allSides) {
$allSidesKeys = array();
foreach($allSides as $key => $value) {
echo $key.$value;
if($value == $longestSide) {
array_push($allSidesKeys, $key);
print_r($allSidesKeys);
return $allSidesKeys;
}
}
}
?>
But when I check, if the $value of the side (lenght) == to the longest side, the if
stops. And if there are 2 sides with the same lenght I want to display them TWO.
I want to save both of the sides with the same lenght in an array too.
You need to move return $allSidesKeys; outside of the if and for blocks. Otherwise it returns on the first match.
This is how your function will look like:
function getSideKey($longestSide, $allSides) {
$allSidesKeys = array();
foreach($allSides as $key => $value) {
echo $key.$value;
if($value == $longestSide) {
array_push($allSidesKeys, $key);
print_r($allSidesKeys);
}
}
return $allSidesKeys;
}
Its much easier. Just get the max as you have and then us array_keys to get the keys that have that value:
function triangle($a, $b, $c) {
$allSides = get_defined_vars();
$longestSide = max($allSides);
echo $longestSide . PHP_EOL;
$allSidesKeys = array_keys($allSides, $longestSide);
echo implode(',', $allSidesKeys) . PHP_EOL;
return $allSidesKeys;
}
$result = triangle(1, 5, 5);
Related
$a = array('a','b','c','d','e','f');
$b = array('1','2');
$count = 0;
$d = 0 ;
$input = array('ina', 'inb','inc');
foreach ($a as $key => $v) {
$count++;
echo $v;
echo $input[$key];
if ($count%3 == 0){
echo $b[$d++];
reset($input);
}
}
I want like this output
1
a-ina
b-inb
c-inc
2
d-ina
e-inb
f-inc
Actually I want $input two times in a foreach loop. $a have 6 items $input have 3 items and $b have 2 items. I need
To make it more applicable, Demo
$a = array('a','b','c','d','e','f');
$input = array('ina', 'inb','inc');
$loop = 1;
$input_length = count($input); // TODO process the length with 0 case.
foreach($a as $index => $value){
if(!($i = $index % $input_length)){
echo $loop . PHP_EOL;
$loop++;
}
echo $value . "_" . $input[$i] . PHP_EOL;
}
You're keeping a few variables that you don't really need as they can be derived from the $key value from $a. To get the output you want, you could do this:
$a = array('a','b','c','d','e','f');
$b = array('1','2');
$input = array('ina', 'inb','inc');
$len = count($input);
foreach ($a as $key => $v) {
$idx = $key % $len;
if ($idx == 0){
echo $b[floor($key/3)] . PHP_EOL;
}
echo $v . "-";
echo $input[$idx] . PHP_EOL;
}
Output:
1
a-ina
b-inb
c-inc
2
d-ina
e-inb
f-inc
Demo on 3v4l.org
I want to add some extra values by using foreach loop.
foreach($a as $b) {
echo $b; // this will print 1 to 6
}
Now I want to edit last item with custom text
and print like this
1
2
3
4
5
6 this is last.
how can i do this? Please help I am new in PHP.
You can use end of array
<?php
$a = array(1,2,3,4,5,6);
foreach($a as $b) {
echo $b; // this will print 1 to 6
if($b == end($a))
echo "this is last.";
echo "<br>";
}
EDIT
as # alexandr comment if you have same value you can do it with key
<?php
$a = array(6,1,2,3,4,5,6);
end($a); // move the internal pointer to the end of the array
$last_key = key($a);
foreach($a as $key=>$b) {
echo $b; // this will print 1 to 6
if($key == $last_key)
echo "this is last.";
echo "<br>";
}
<?php
$a = array(1,2,3,4,5,6);
$last = count($a) - 1;
foreach($a as $k => $b) {
echo $b; // this will print 1 to 6
if($k == $last)
echo "this is last.";
echo "<br>";
}
You can declare inc variable, and use with array count
<?php
//$b is your array
$i=1;
foreach($a as $b) {
if(count($a)==$i){
echo $b; // this is last
}
$i++;
}
?>
Use count, the thats way you get the size and you can use in a condicional like if
$size=count($a);
foreach($a as $b) {
if ($b==$size)
{
echo $b. "This is the last"; // this will print 6 and text
}
else
{
echo $b; // this will print 1 to 5
}
}
You can use array_slice which is a good way to slice up an array.
You can set it to get the last item(s) with negative numbers.
$arr = array(1,2,3,4,5,6);
$last = array_slice($arr, -1, 1)[0]; // 6
$other = array_slice($arr, 0, -1); // [1,2,3,4,5]
foreach($other as $item){
echo $item;
}
echo $last . " this is the last";
There are multiple variables say $a,$b,$c,$d having boolean value.
So what i am trying to do is .
if($a){echo 1;}
if($b){echo 2;}
if($c){echo 3;}
if($d){echo 4;}
and so on.. 50 variables
is there any better way to do this?
Note: More than one variable can be true.
You can use this code to iterate :
$a= $b = $c = TRUE;
$array = array(0=> $a,1=>$b,2=> $c);
foreach($array as $key => $value){
if($value){
echo $key;
}
}
Maybe put all boolean variable inside an boolean array, and iterate the array to check the value
$boolArray = array();
$boolArray[0] = array($a, 1);
$boolArray[1] = array($b, 2);
$boolArray[2] = array($c, 3);
...
for($x = 0; $x < count($boolArray); $x++) {
if ($boolArray[x][1]) {
echo (string)$boolArray[x][2];
}
}
I think you're looking for something like this.
<?php
# Define the settings you already have
$a = true;
$b = true;
$c = true;
# Create an array with letters from a to z
# Instead of this, you can create an array of field names like array('a', 'b', 'c', 'd');
$options = range('a', 'z');
# Loop in the letters from above
foreach($options as $id => $letter) {
# Check if variable exists and boolean is true
if(isset(${$letter}) && ${$letter} === true) {
# Output number from id
echo $id + 1;
}
}
?>
I want to get the highest value, the second highest value and the third highest value
For example, I have an array like:
$n = array(100,90,150,200,199,155,15,186);
I know the method to get the max value and its index:
echo max($n); //200
$maxs = array_keys($n, max($n));
echo $maxs[0]; //3
I want to get the top 3 values and their index like : value: 200, 199, 186 index:3,4,7
How can i get them?
Try this:
$n = array(100,90,150,200,199,155,15,186);
rsort($n);
$top3 = array_slice($n, 0, 3);
echo 'Values: ';
foreach ($top3 as $key => $val) {
echo "$val\n";
}
echo '<br>';
echo 'Keys: ';
foreach ($top3 as $key => $val) {
echo "$key\n";
}
Output:
Values: 200 199 186
Keys: 0 1 2
This should do the trick:
function maxNitems($array, $n = 3){
asort($array);
return array_slice(array_reverse($array, true),0,$n, true);
}
Use like:
maxNitems(array(100,90,150,200,199,155,15,186));
You can achieve it by using arsort() and array_keys() functions:
arsort() sorts an array in reverse order and maintains index association
array_keys() returns all the keys or a subset of the keys of an array
Process array:
$n = array(100,90,150,200,199,155,15,186);
arsort($n);
$keys = array_keys($n);
Get top 3 values:
echo $n[$keys[0]];
echo $n[$keys[1]];
echo $n[$keys[2]];
$n = array(100,90,150,200,199,155,15,186);
arsort($n);
$x = 0;
while (++$x <= 3)
{
$key = key($n);
$value = current($n);
next($n);
echo "Key : " . $key . " Value : " . $value . '<br>' ;
}
Easier I would think:
arsort($n);
$three = array_chunk($n, 3, true)[0];
//or
$three = array_slice($n, 0, 3, true);
try this:
public function getTopSortedThree(array $data, $asc = true)
{
if ($asc) {
uasort($data, function ($a, $b) { return $a>$b;});
} else {
uasort($data, function ($a, $b) { return $a<$b;});
}
$count = 0;
$result = [];
foreach ($data as $key => $value) {
$result[] = $data[$key];
$count++;
if ($count >= 3){
break;
}
}
return $result;
}
send false for desc order and nothing for asc order
This functionality doesn't losing keys.
Instead of just 1, how can I pick the 4 highest values from an array using max()?
You could use an SplMaxHeap
function maxN(array $numbers, $n)
{
$maxHeap = new SplMaxHeap;
foreach($numbers as $number) {
$maxHeap->insert($number);
}
return iterator_to_array(
new LimitIterator($maxHeap, 0, $n)
);
}
Usage (demo):
print_r( maxN( array(7,54,2,4,26,7,82,4,34), 4 ) );
You could try this:
$a = array(3,5,6,1,23,6,78,99);
asort($a);
var_dump(array_slice($a, -4));
HTH.
This will do it in Θ(n) time:
$a = $b = $c = $d = null;
foreach($array as $v) {
if(!isset($a) || $v > $a) {
$d = $c;
$c = $b;
$b = $a;
$a = $v;
}elseif(!isset($b) || $v > $b) {
$d = $c;
$c = $b;
$b = $v;
}elseif(!isset($c) || $v > $c) {
$d = $c;
$c = $v;
}elseif(!isset($d) || $v > $d) {
$d = $v;
}
}
$result = array($a, $b, $c, $d);
function maxs($ar, $count=4)
{
$res = array();
foreach ($ar as $v)
{
for ($i = 0;$i < $count;$i++)
{
if ($i >= count($res) || $v > $res[$i])
{
do
{
$tmp = $res[$i];
$res[$i] = $v;
$v = $tmp;
$i++;
}
while ($i < $count);
break;
}
}
}
return $res;
}
A simple method using php predefined functions.
<?php
$arr = array(6, 8, 3, 2, 7, 9);
rsort($arr);
$first = array_shift($arr);
$second = array_shift($arr);
$third = array_shift($arr);
echo $first; // print 9
echo $second; // print 8
echo $third; // print 7
?>
While storing itself you can maintain another array as soon as the new item is inserted check with the max value in the inner array if the item being inserted is greater insert this item. During the item pop do viceversa. From the inner maintained array you can get as many max numbers as possible.