Process array starting with second entry? - php

I have an array in PHP and would like to use foreach to process entries skipping [0], to process [1], [2], etc.
Thank you

you can use array_slice
$array = array(1,2,3);
foreach (array_slice($array,1) as $value ) {
echo $value;
}
If you don't mind losing first element you can use array_shift
array_shift($array);
foreach ( $array as $value ) {
echo $value;
}
Output
23

$i = 0;
foreach ($ar as $value) {
if ($i > 0) {
// code here
}
$i++;
}

You can keep a variable for this:
$firstSkipped = false;
foreach ($arr as $value) {
if (!$firstSkipped) {
$firstSkipped = true;
continue;
}
// code here
}
Or you could just use a regular for loop, setting the beginning counter to 1:
for ($i = 1, $count = count($arr); $i < $count; $i++) {
// code here
}

You can remove the first entry from an array with array_shift.
$array = array("a","b","c");
array_shift($array);
foreach ($array as $values)
{
echo $values; //bc
}

Try this:
$arr = array(0,1,2,3,4,5);
unset($arr[0]);
foreach($arr as $value) {
echo $value;
echo "<br />";
}
This would delete first entry from array, so it would not skip as you asked, but anyway you can try this...

Related

How to find missing number from an array without using function in php?

I have two array i.e $arr1 and $arr2 where I want to find missing value of $arr1 which is not present in $arr2 without using function like array_diff(), count(), explode(), implode() etc. So, How can I do this? Please help me.
code:
<?php
$arr1 = array('2','3','4','5');
$arr2 = array('1','6','7','8');
$array = array_diff($arr1,$arr2);
print_r($arr2);
?>
First approach:-
$missingValuesArray = array();
foreach($arr1 as $arr){
if(!in_array($arr,$arr2)){
$missingValuesArray[] = $arr;
}
}
print_r($missingValuesArray);
Output:- https://3v4l.org/UBS9G
Second approach:-
$missingValuesArray = array();
foreach($arr1 as $arr){
$counter = 0;
foreach($arr2 as $ar){
if($arr != $ar){
$counter++;
}
}
if($counter == sizeof($arr2)){
$missingValuesArray[] = $arr;
}
}
print_r($missingValuesArray);
Output:- https://3v4l.org/Uu6Ob
Requirement can be achieved by :
$arr1 = array('2','3','4','5');
$arr2 = array('1','6','7','8');
$diff = array();
$diff = $arr1;
$arrayDiff = array();
foreach($arr1 AS $value) {
foreach($arr2 AS $val) {
if ($value == $val) {
$arrayDiff[] = $value;
continue;
}
}
}
foreach ($arrayDiff AS $k=>$v) {
if (($key = array_search($v, $diff)) !== false) {
unset($diff[$key]);
}
}
print_r($diff);

PHP How to merge array element with next while maintaining order?

$array = ['coke.','fanta.','chocolate.'];
foreach ($array as $key => $value) {
if (strlen($value)<6) {
$new[] = $value." ".$array[$key+1];
} else {
$new[] = $value;
}
}
This code doesn't have the desired effect, in fact it doesn't work at all. What I want to do is if an array element has string length less than 5, join it with the next element. So in this case the array should turn into this:
$array = ['coke. fanta.','chocolate.'];
$array = ['coke.','fanta.','chocolate.', 'candy'];
$new = [];
reset($array); // ensure internal pointer is at start
do{
$val = current($array); // capture current value
if(strlen($val)>=6):
$new[] = $val; // long string; add to $new
// short string. Concatenate with next value
// (note this moves array pointer forward)
else:
$nextVal = next($array) ? : '';
$new[] = trim($val . ' ' . $nextVal);
endif;
}while(next($array));
print_r($new); // what you want
Live demo
With array_reduce:
$array = ['coke.', 'fanta.', 'chocolate.', 'a.', 'b.', 'c.', 'd.'];
$result = array_reduce($array, function($c, $i) {
if ( strlen(end($c)) < 6 )
$c[key($c)] .= empty(current($c)) ? $i : " $i";
else
$c[] = $i;
return $c;
}, ['']);
print_r($result);
demo
<pre>
$array = ['coke.','fanta.','chocolate.'];
print_r($array);
echo "<pre>";
$next_merge = "";
foreach ($array as $key => $value) {
if($next_merge == $value){
continue;
}
if (strlen($value)<6) {
$new[] = $value." ".$array[$key+1];
$next_merge = $array[$key+1];
} else {
$new[] = $value;
}
}
print_r($new);
</pre>
Updated Code after adding pop after chocolate.
<pre>
$array = ['coke.','fanta.','chocolate.','pop'];
print_r($array);
echo "<br>";
$next_merge = "";
foreach ($array as $key => $value) {
if($next_merge == $value){
continue;
}
if (strlen($value)<6 && !empty($array[$key+1])) {
$new[] = $value." ".$array[$key+1];
$next_merge = $array[$key+1];
} else {
$new[] = $value;
}
}
print_r($new);
<pre>
You need to skip the iteration for the values that you have already added.
$array = ['coke.', 'fanta.', 'chocolate.'];
$cont = false;
foreach ($array as $key => $value) {
if ($cont) {
$cont = false;
continue;
}
if (strlen($value) < 6 && isset($array[$key+1])) {
$new[] = $value.' '.$array[$key+1];
$cont = true;
}
else {
$new[] = $value;
}
}
print_r($new);

Add each element of an array after each element of another array php

I've got 2 arrays.
I would like to add each element in array2 after each element in array1.
Could you please help me how to figure out this in PHP.
Array1[] = ("Test1","Test2","Test3","Test4");
Array2[] = ("ADD1","ADD2","ADD3","ADD4");
FinalArray[] =("Test1","ADD1","Test2","ADD2","Test3","ADD3","Test4","ADD4");
Thanks.
You can do it using foreach
$Array1 = array("Test1","Test2","Test3","Test4");
$Array2 = array("ADD1","ADD2","ADD3","ADD4");
foreach ($Array1 as $key => $value) {
$FinalArray[] = $value;
if(isset($Array2[$key]))
$FinalArray[] = $Array2[$key];
}
print_r($FinalArray);
See the result
try this
<?php
$Array1 = array("Test1","Test2","Test3","Test4");
$Array2 = array("ADD1","ADD2","ADD3","ADD4");
for($i=0 ;$i<count($Array1);$i++ ){
$a[] = $Array1[$i];
$a[] = $Array2[$i];
}
print_r($a);
?>
Try it:
$k=0;
$j=0;
$final_arr = array();
for($i=1;$i<=count($arr1);$i++)
{
if($i%2!=0)
{
array_push($final_arr,$arr1[$k]);
$k++;
}
else
{
array_push($final_arr,$arr2[$j]);
$j++;
}
}
Try Using This Code:
<?php
$arr1 = array("Test1","Test2","Test3","Test4");
$arr2 = array("ADD1","ADD2","ADD3","ADD4");
$final_arr = array();
$index1=0;
$index2=0;
for($i=1;$i<=count($arr1)+count($arr2);$i++)
{
if($i%2==0)
{
array_push($final_arr,$arr2[$index2]);
$index2++;
}
else
{
array_push($final_arr,$arr1[$index1]);
$index1++;
}
}

php array issue with break array

I got an Array:
$myArrays = array(5,4,3,2,1);
foreach($myArrays as $myArray)
{
echo $myArray;
$val = 3;
if($myArray == $val)
{
break;
}
}
Output: 5,4,3
I would want it to be like
output: 3,2,1
is this possible?
You can use the following snippet.
The first element of array is always the left one, when you define it.
<?php
foreach($myArrays as $myArray)
{
if ($myArray <= 3) {
echo $myArray;
}
}
$myArrays = array(5,4,3,2,1);
foreach($myArrays as $myArray)
{
$val = 3;
if($myArray > $val)
{
continue;
}
echo $myArray;
}
Simply have a boolean variable outside loop to keep track of wheter you got the element you're looking for.
Skip the loop (By using continue keyword) until you find that element.
So your will look something like this,
$foundelement=false;
foreach($myArrays as $myArray)
{
$val = 3;
if(!$foundelement && $myArray != $val)
{
continue;
} else {
$foundelement=true;
}
if($foundelement) {
echo $myArray;
}
}
Demo: https://eval.in/620081

php array , delete a value if matching

I have an array like the following:
5-9-21,
5-10-22,
5-10-22,
5-11-23,
3-17-29,
3-19-31,
3-19-31,
1-25-31,
7-30-31
I wil get a value dynamically. Then I have to compare that value with the middle part of array.
9,
10,
10,
11,
17,
19,
19,
25,
30
If it's matching then I have to remove the whole part from array.
For example. If I am getting a value dynamically is 19, then I wil match with that array. And 3-19-31 is there two times. So it will remove all 3-19-31. After exploding with "-".
How can I do this?
foreach($array as $key=>$value){
$parts = explode('-', $value);
if($parts[1] == $search) {
unset($array[$key]);
}
}
Or if your search is an array
foreach($array as $key=>$value){
$parts = explode('-', $value);
if(in_array($parts[1], $search)) {
unset($array[$key]);
}
}
You could use array_filter to get a new array.
$new_arr = array_filter($old_arr, function($var) use ($input) {
$ret = explode('-', $var);
return !(isset($ret[1]) && $ret[1] === $input);
});
Or use a normal loop and then use unset to remove the values.
for ($arr as $key => $value) {
$ret = explode('-', $value);
if (isset($ret[1]) && $ret[1] === $input) {
unset($arr[$key]);
}
}
use this function, this will give you all the keys which are matched:
function custom_array_search($keyword,$array){
if(!is_array($array)){
return false;
}
$ret_keys = array();
foreach($array as $key=>$value){
if(strpos("-{$keyword}-",$value)!==false){
$ret_keys[] = $key;
}
}
return $ret_keys;
}
This function will give you all keys in an array.
Now you can delete those i.e. unset all keys from that array. :)
<?php
$arr = array('5-9-21', '5-10-22', '5-10-22', '5-11-23', '3-17-29', '3-19-31', '3-19-31', '1-25-31', '7-30-31');
$k = '10';
#print_r($arr);
foreach ($arr as $key => $value)
{
$t = explode('-',$value);
if($t[1] == $k)
{
unset($arr[$key]);
#echo "deleted<br>";
}
}
#print_r($arr);
?>
You can try this...
$arr; // your array
$value = 19;
foreach ($arr as $key=>$a)
{
if(strpos($a, "-".$value."-") !== false)
unset($arr[$key]);
}
There are few ways you can do this. If you are going to have only one digit always in the first eliment of your triplet, the following code should work;
$triplet_array = array(5-9-21, 5-10-22, 5-10-22, 5-11-23, 3-17-29, 3-19-31, 3-19-31, 1-25-31, 7-30-31);
$i = 0;
foreach($triplet_array as triplet){
$middle = substring($triplet,2,0);
if($middle == $my_dynamic_value) unset($triplet_array[$i]);
$i++
}
but, if the first part is not going to contain only one digit always;
foreach($triplet_array as triplet){
$this_triplet = explode('-',$triplet);
if($this_triplet[1] == $my_dynamic_value) unset($triplet_array[$i]);
$i++
}
hope this helps :-)

Categories