I have this array:
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
It has 8 value , i want to output all value after the fifth value which is 5.
so I want to output:
6 7 8
This is my code:
<?php
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
$i = 0;
while ($i < count($arr)) {
$a = $arr[$i];
echo $a ."\n";
$i++;
}
?>
Use array_slice()
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
$output = array_slice($arr, 0, 3);
Array_slice or starting your loops from the desired index:
<?php
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
$i = 5;
while ($i < count($arr)) {
$a = $arr[$i];
echo $a ."\n";
$i++;
}
?>
<?php
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
$arr = array_slice($arr,5);
$i = 0;
while ($i < count($arr)) {
$a = $arr[$i];
echo $a ."\n";
$i++;
}
?>
<?php
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
$i = 5;
while ($i < count($arr)) {
$a = $arr[$i];
echo $a ."\n";
$i++;
}
?>
Simply use array_slice along with implode function like as
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
echo implode("\n",array_slice($arr,5));
If you were testing it within browser you simply replace \n along with <br/>
Demo
Related
I need to add an item every 6th position.
So it would look like this:
[
//item1
//item2
//item3
//item4
//item5
//NEW ITEM HERE
//item7
//item8
//item9
//item10
//item11
//NEW ITEM
]
I already tried this:
foreach($ports as $key => $port)
{
if($key %9 == 2) {
$ports->splice($key, 0, [$ads]);
}
}
But that's not working any idea?
Use array_chunk and add element to each of sub-arrays:
$portsChunks = array_chunk($ports, 5); // Split array to sub-arrays of max-5 elements.
// Add new element if chunk is full length.
// Means last one will not receive new element if it's shorter than 5
array_walk($portsChunks, function (&$array) {
if (count($array) == 5) {
$array[] = 'New Item';
}
});
// Use arguments unpacking to pass all chunks to array_merge
$ports = array_merge(...$portsChunk);
Example
You can use foreach loop:
$ports = range(1,50);
$new_ports = [];
foreach ($ports as $key => $port) {
$new_ports[] = $port;
if(!(($key+1)%5))
$new_ports[] = 'New item';
}
print_r($new_ports);
$ports = [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6];
$ports = array_chunk($ports, 5);
foreach ($ports as &$port){
array_push($port, 'new value');
}
unset($port);
if(count($ports[count($ports)-1]) < 6){
array_pop($ports[count($ports)-1]);
}
$ports = array_merge(...$ports);
you just need a new array to hold all of your data. and then you iterate over your old array and after each 5th position insert a new element. something like this
$result = [];
$ports = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
$porstCount = count($ports);
for ($i = 0; $i < $portsCount; $i++) {
$result[] = $ports[$i];
if ($i > 0 && $i % 5 === 0) {
$result[] = 'new element';
}
}
// $result will be [1, 2, 3, 4, 5, 'new element', 6, 7, 8, 9, 10, 'new element', 11, 12]
I hope below case solves your problem:
$newarr = array();
$cnt = 1;
foreach($arr as $key=>$value){
$newarr[] = $value;
if($cnt%5 == 0){
$newarr[] = 'this is new item';
}
$cnt++;
}
print_r($newarr);
This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
Where am I wrong?
The code looks OK, but the function returns NULL.
$ar = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$n = 6;
$new_n = chckn($n, $ar);
echo $new_n;
function chckn($n, $ar) {
if(!in_array($n, $ar)) {
echo "===$n===\n";
return $n;
} else {
$n = rand(1, 10);
chckn($n, $ar);
}
}
$ar = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$n = 6;
$new_n = chckn($n, $ar);
echo $new_n;
function chckn($n, $ar){
if(!in_array($n, $ar)){
//echo "===$n===\n";
return $n;
} else {
$n = rand(1,10);
return chckn($n, $ar);
}
}
I have a loop which i made like this:
$arr = array(1, 2, 3, 4, 5, 6, 7);
foreach ($arr as &$value) {
echo $value;
}
My loop result shows this:
1234567
I would like this to only show the numbers 1 to 4.
And when it reaches 4 it should add a break and continue with 5671.
So an example is:
1234<br>
5671<br>
2345<br>
6712<br>
I have to make this but I have no idea where to start, all hints/tips are very welcome or comment any direction I should Google.
Here is more universal function- you can pass an array as argument, and amount of elements you want to display.
<?php
$array = array(1,2,3,4,5,6,7);
function getFirstValues(&$array, $amount){
for($i=0; $i<$amount; $i++){
echo $array[0];
array_push($array, array_shift($array));
}
echo "<br />";
}
getFirstValues($array, 4);
getFirstValues($array, 4);
getFirstValues($array, 4);
getFirstValues($array, 4);
?>
The result is:
1234
5671
2345
6712
This produces the exact results you want
$arr = array(1, 2, 3, 4, 5, 6, 7);
$k=0;
for($i=1;$i<=5;++$i){
foreach ($arr as &$value) {
++$k;
echo $value;
if($k %4 == 0) {
echo '<br />';
$k=0;
}
}
}
You are looking for array_chunk()
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
$chunks = array_chunk($arr, 4);
foreach ($chunks as $array) {
foreach ($array as $value) {
echo $value;
}
echo "<br />";
}
The output is:
1234
5678
9101112
13
$arr = array(1, 2, '...', '...', '...', 6, 7, 8, '...', 10);
array_unique creates: array(1, 2, '...', 6, 7, 8, 10);
I want the following: array(1, 2, '...', 6, 7, 8, '...', 10);
So basically, I'm looking for a fast way to remove only duplicates next to eachother.
$result = array();
$first = true;
foreach ($array as $x) {
if ($first || $x !== $previous) $result[] = $x;
$previous = $x;
$first = false;
}
Alter the $x !== $previous condition to suit your preferred definition of "duplicate". For example, array_unique does a loosely-typed comparison.
I would do it this way:
<?php
$arr = array(1, 2, '...', '...', '...', 6, 7, 8, '...', 10);
$el = $arr[0];
$out = $arr;
for ($i=1, $c = count($out); $i<$c; ++$i) {
if ($arr[$i] == $el) {
unset($out[$i]);
}
else {
$el = $out[$i];
}
}
$out = array_values($out);
foreach ($out as $i) {
echo $i."<br />";
}
Output:
1
2
...
6
7
8
...
10
I have this array:
$a = array(1, 2, 3, 4, 5, 7, 8, 10, 12);
Is there a function to convert this to:
$b = array(1, 1, 1, 1, 2, 1, 2, 2);
So basicaly:
$b = array ($a[1]-$a[0], $a[2]-$a[1], $a[3]-$a[2], ... ,$a[n]-$a[n-1]);
Here is the code I have so far:
$a = $c = array(1, 2, 3, 4, 5, 7, 8, 10, 12);
array_shift($c);
$d = array();
foreach ($a as $key => $value){
$d[$key] = $c[$key]-$value;
}
array_pop($d);
There isn't a built-in function that can do this for you, but you could turn your code into one instead. Also, rather than making a second array, $c, you could use a regular for loop to loop through the values:
function cumulate($array = array()) {
// re-index the array for guaranteed-success with the for-loop
$array = array_values($array);
$cumulated = array();
$count = count($array);
if ($count == 1) {
// there is only a single element in the array; no need to loop through it
return $array;
} else {
// iterate through each element (starting with the second) and subtract
// the prior-element's value from the current
for ($i = 1; $i < $count; $i++) {
$cumulated[] = $array[$i] - $array[$i - 1];
}
}
return $cumulated;
}
I think php has not a build in function for this. There are many ways to solve this, but you already wrote the answer:
$len = count($a);
$b = array();
for ($i = 0; $i < $len - 1; $i++) {
$b[] = $a[$i+1] - $a[$i];
}