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";
Related
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);
$a = ["a","b","c","a","b"];
$b = ["a","c"];
array_diff($a,$b);
//output will be [b,b]
but that is not a proper difference it can also be
//output [b,a,b] <- this is what I am trying to achieve
I tried foreach loop and for loop but failed to get it...
Foreach example I tried
$a = ["a","b","c","a","b"];
$b = ["a","c"];
echo array_diff_custom($a,$b),"<br>";
function array_diff_custom($a,$b){
$result =0;
foreach($a as $key=>$val){
foreach($b as $key2=>$val2){
if($val == $val2){
unset($a[$key]);
}
}
}
$result = count($a);
return $result;
}
echo array_diff_custom($b,$a);
for loop example, I tried
$a = ["a","b","c","a","b"];
$b = ["a","c"];
echo array_diff_custom($a,$b),"<br>";
function array_diff_custom($a,$b){
$result =0;
for($i=0;$i<count($a);$i++){
for($j=0;$j<count($b);$j++){
//echo $a[$i]."-".$b[$j]."<br>";
if($a[$i] == $b[$j]){
unset($a[$i]);
}
}
}
$result = count($a);
return $result;
}
echo array_diff_custom($b,$a);
I am using count($resut) in example function I created but you can just simply return $a and can print_R(array_Diff_custom)
to check the output...
You can just unset items, presented in the 2nd array, from the first one only once
function array_diff_custom($a,$b){
foreach($b as $x){
if($k = array_keys($a, $x)) {
unset($a[$k[0]]);
}
}
return $a;
}
print_r(array_diff_custom($a,$b));
demo
i just want ask how i can use for loop to print
123
456
i'm try with this code:
<?php
$a = array(1,2,3,4,5,6);
foreach($a as $r){
for($q = 0; $q < 3; $q++) {
echo $r;
}
echo "<br />";
}
?>
But the problem is, it prints:
111
222
333
444
555
666
You're printing the value of $r three times for each value of $r; what you want is to print each value and print a break after every third.
Something like this would work:
foreach ($a as $i => $r) {
echo $r;
// insert break after every third value
if ($i > 0 && ($i + 1) % 3 == 0) {
echo '<br />';
}
}
Or, you could use array_chunk() to split the array up in chunks of three and print each of those.
foreach (array_chunk($a, 3) as $chunk) {
foreach ($chunk as $nr) {
echo $nr;
}
echo '<br />';
}
Use array_chunk($a, 3) and then use implode , that will give you required result.
$a = array(1,2,3,4,5,6,7,8,9);
$result='';
$i = 1;
foreach($a as $r)
{
$result.=$r;
if($i%3 == 0)
{
echo $result."<br />";
$result='';
}
$i++;
}
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.
I'm trying to sort everything out using array & loops
lets say this is an array,
$arr = array('One','Two','Three','Four','Five','Six');
i want to select first three of them to be used as
$arr[0], $arr[1] and $arr[2]
this ends up with one,two,three
i want to use the same for next 3..
something to break it and use the same 0 1 2 index again to return second three
here's an example to explain more,
$arr = array('One','Two','Three','Four','Five','Six');
echo $arr[0].$arr[1].$arr[2]; // OneTwoThree
break; // < ??
echo $arr[0].$arr[1].$arr[2]; // FourFiveSix
also, i want to use it with foreach loop
$arr = array('One','Two','Three','Four','Five','Six');
foreach($arr as $num){
echo $num; // OneTwoThree
if($something == 3){ // < ??
break; // < ??
}
echo $num; // FourFiveSix
}
and what im trying to do is using each three arrays of the array to use them later
Use array_chunk and loop over the result:
foreach(array_chunk($arr, 3) as $a) {
echo $a[0], $a[1], $a[2];
}
Here it is with added linebreaks:
foreach(array_chunk($arr, 3) as $a) {
echo $a[0], $a[1], $a[2], "<br>\n";
}
Note: The last element may have less than three elements! A possible fix for this is to use implode to concatenate the elements:
foreach(array_chunk($arr, 3) as $a) {
echo implode('', $a), "<br>\n";
}
Edit: array_chunk() is a lot more sensible...early mornings
<?php
$arr = array('One','Two','Three','Four','Five','Six');
for($i = 0, $i < count($arr); ++$i)
{
if( ! ($i + 1) % 3)
{
continue;
}
echo $arr[$i - 2] . $arr[$i - 1] . $arr[$i]
}