How do I count occurrence of duplicate items in array [duplicate] - php

This question already has answers here:
How to detect duplicate values in PHP array?
(13 answers)
Closed 7 months ago.
I would like to count the occurrence of each duplicate item in an array and end up with an array of only unique/non duplicate items with their respective occurrences.
Here is my code; BUT I don't where am going wrong!
<?php
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
//$previous[value][Occurrence]
for($arr = 0; $arr < count($array); $arr++){
$current = $array[$arr];
for($n = 0; $n < count($previous); $n++){
if($current != $previous[$n][0]){// 12 is not 43 -----> TRUE
if($current != $previous[count($previous)][0]){
$previous[$n++][0] = $current;
$previous[$n++][1] = $counter++;
}
}else{
$previous[$n][1] = $counter++;
unset($previous[count($previous)-1][0]);
unset($previous[count($previous)-1][1]);
}
}
}
//EXPECTED VALUES
echo 'No. of NON Duplicate Items: '.count($previous).'<br><br>';// 7
print_r($previous);// array( {12,1} , {21,2} , {43,6} , {66,1} , {56,1} , {78,2} , {100,1})
?>

array_count_values, enjoy :-)
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$vals = array_count_values($array);
echo 'No. of NON Duplicate Items: '.count($vals).'<br><br>';
print_r($vals);
Result:
No. of NON Duplicate Items: 7
Array
(
[12] => 1
[43] => 6
[66] => 1
[21] => 2
[56] => 1
[78] => 2
[100] => 1
)

if you want to try without 'array_count_values'
you can do with a smart way here
<?php
$input= array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$count_values = array();
foreach ($input as $a) {
#$count_values[$a]++;
}
echo 'Duplicates count: '.count($count_values);
print_r($count_values);
?>

Simplest solution (that's where PHP rocks) - ONLY duplicates:
$r = array_filter(array_count_values($array), function($v) { return $v > 1; });
and check:
print_r($r);
Result $r:
[43] => 6
[21] => 2
[78] => 2

If you have a multi-dimensional array you can use on PHP 5.5+ this:
array_count_values(array_column($array, 'key'))
which returns e.g.
[
'keyA' => 4,
'keyB' => 2,
]

I actually wrote a function recently that would check for a substring within an array that will come in handy in this situation.
function strInArray($haystack, $needle) {
$i = 0;
foreach ($haystack as $value) {
$result = stripos($value,$needle);
if ($result !== FALSE) return TRUE;
$i++;
}
return FALSE;
}
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
for ($i = 0; $i < count($array); $i++) {
if (strInArray($array,$array[$i])) {
unset($array[$i]);
}
}
var_dump($array);

You can also use it with text items array, u will get number of duplicates properly, but PHP shows
Warning: array_count_values(): Can only count STRING and INTEGER
values!
$domains =
array (
0 => 'i1.wp.com',
1 => 'i1.wp.com',
2 => 'i2.wp.com',
3 => 'i0.wp.com',
4 => 'i2.wp.com',
5 => 'i2.wp.com',
6 => 'i0.wp.com',
7 => 'i2.wp.com',
8 => 'i0.wp.com',
9 => 'i0.wp.com' );
$tmp = array_count_values($domains);
print_r ($tmp);
array (
'i1.wp.com' => 2730,
'i2.wp.com' => 2861,
'i0.wp.com' => 2807
)

Count duplicate element of an array in PHP without using in-built
function
$arraychars=array("or","red","yellow","green","red","yellow","yellow");
$arrCount=array();
for($i=0;$i<$arrlength-1;$i++)
{
$key=$arraychars[$i];
if($arrCount[$key]>=1)
{
$arrCount[$key]++;
} else{
$arrCount[$key]=1;
}
echo $arraychars[$i]."<br>";
}
echo "<pre>";
print_r($arrCount);

There is a magical function PHP is offering to you it called in_array().
Using parts of your code we will modify the loop as follows:
<?php
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$arr2 = array();
$counter = 0;
for($arr = 0; $arr < count($array); $arr++){
if (in_array($array[$arr], $arr2)) {
++$counter;
continue;
}
else{
$arr2[] = $array[$arr];
}
}
echo 'number of duplicates: '.$counter;
print_r($arr2);
?>
The above code snippet will return the number total number of repeated items i.e. form the sample array 43 is repeated 5 times, 78 is repeated 1 time and 21 is repeated 1 time, then it returns an array without repeat.

You can do it using foreach loop. (Demo)
$array = array(1,2,3,1,2,3,1,2,3,4,4,5,6,4,5,6,88);
$set_array = array();
foreach ($array as $value) {
$set_array[$value]++;
}
print_r($set_array);
Output:
Warning: Undefined array key 1 in /in/aGPqe on line 6
Warning: Undefined array key 2 in /in/aGPqe on line 6
Warning: Undefined array key 3 in /in/aGPqe on line 6
Warning: Undefined array key 4 in /in/aGPqe on line 6
Warning: Undefined array key 5 in /in/aGPqe on line 6
Warning: Undefined array key 6 in /in/aGPqe on line 6
Warning: Undefined array key 88 in /in/aGPqe on line 6
Array
(
[1] => 3
[2] => 3
[3] => 3
[4] => 3
[5] => 2
[6] => 2
[88] => 1
)

I came here from google looking for a way to count the occurrence of duplicate items in an array. Here is the way to do it simply:
$colors = ["red", "green", "blue", "red", "yellow", "blue"];
$unique_colors = array_unique($colors); // ["red", "green", "blue", "yellow"]
$duplicates = count($colors) - count($unique_colors); // 6 - 4 = 2
if ($duplicates == 0) {
echo "There are no duplicates";
}
echo "No. of Duplicates: " . $duplicates;
// Output: No. of Duplicates are: 2
How array_unique() works?
It elements all the duplicates.
ex:
Lets say we have an array as follows -
$cars = array( [0]=>"lambo", [1]=>"ferrari", [2]=>"Lotus", [3]=>"ferrari", [4]=>"Bugatti");
When you do $cars = array_unique($cars);
cars will have only following elements.
$cars = array( [0]=>"lambo", [1]=>"ferrari", [2]=>"Lotus", [4]=>"Bugatti");
To read more: https://www.php.net/manual/en/function.array-unique.php

this code will return duplicate value in same array
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
foreach($arr as $key=>$item){
if(array_count_values($arr)[$item] > 1){
echo "Found Matched value : ".$item." <br />";
}
}

$search_string = 4;
$original_array = [1,2,1,3,2,4,4,4,4,4,10];
$step1 = implode(",", $original_array); // convert original_array to string
$step2 = explode($search_string, $step1); // break step1 string into a new array using the search string as delimiter
$result = count($step2)-1; // count the number of elements in the resulting array, minus the first empty element
print_r($result); // result is 5

$input = [1,2,1,3,2,4,10];
//if give string
//$input = "hello hello how are you how hello";
//$array = explode(' ',$input);
$count_val = [];
foreach($array as $val){
$count_val[$val]++;
}
print_r($count_val);
//output ( [1] => 2 [2] => 2 [3] => 1 [4] => 1 [10] => 1 )

Related

How to find unique values in array

Here i want to find unique values,so i am writing code like this but i can't get answer,for me don't want to array format.i want only string
<?php
$array = array("kani","yuvi","raja","kani","mahi","yuvi") ;
$unique_array = array(); // unique array
$duplicate_array = array(); // duplicate array
foreach ($array as $key=>$value){
if(!in_array($value,$unique_array)){
$unique_array[$key] = $value;
}else{
$duplicate_array[$key] = $value;
}
}
echo "unique values are:-<br/>";
echo "<pre/>";print_r($unique_array);
echo "duplicate values are:-<br/>";
echo "<pre/>";print_r($duplicate_array);
?>
You can use array_unique() in single line like below:-
<?php
$unique_array = array_unique($array); // get unique value from initial array
echo "<pre/>";print_r($unique_array); // print unique values array
?>
Output:- https://eval.in/601260
Reference:- http://php.net/manual/en/function.array-unique.php
If you want in string format:-
echo implode(',',$final_array);
Output:-https://eval.in/601261
The way you want is here:-
https://eval.in/601263
OR
https://eval.in/601279
I am baffled by your selection as the accepted answer -- I can't imagine how it can possibly satisfy your requirements.
array_count_values() has been available since PHP4, so that is the hero to call upon here.
Code: (Demo)
$array = array("kani","yuvi","raja","kani","mahi","yuvi") ;
$counts = array_count_values($array); // since php4
foreach ($counts as $value => $count) {
if ($count == 1) {
$uniques[] = $value;
} else {
$duplicates[] = $value;
}
}
echo "unique values: " , implode(", ", $uniques) , "\n";
echo "duplicate values: " , implode(", ", $duplicates);
Output:
unique values: raja, mahi
duplicate values: kani, yuvi
To get unique values from array use array_unique():
$array = array(1,2,1,5,10,5,10,7,9,1) ;
array_unique($array);
print_r(array_unique($array));
Output:
Array
(
[0] => 1
[1] => 2
[3] => 5
[4] => 10
[7] => 7
[8] => 9
)
And to get duplicated values in array use array_count_values():
$array = array(1,2,1,5,10,5,10,7,9,1) ;
print_r(array_count_values($array));
Output:
Array
(
[1] => 3 // 1 is duplicated value as it occurrence is 3 times
[2] => 1
[5] => 2 // 5 is duplicated value as it occurrence is 3 times
[10] => 2 // 10 is duplicated value as it occurrence is 3 times
[7] => 1
[9] => 1
)
<?php
$arr1 = [1,1,2,3,4,5,6,3,1,3,5,3,20];
print_r(arr_unique($arr1)); // will print unique values
echo "<br>";
arr_count_val($arr1); // will print array with duplicate values
function arr_unique($arr) {
sort($arr);
$curr = $arr[0];
$uni_arr[] = $arr[0];
for($i=0; $i<count($arr);$i++){
if($curr != $arr[$i]) {
$uni_arr[] = $arr[$i];
$curr = $arr[$i];
}
}
return $uni_arr;
}
function arr_count_val($arr) {
$uni_array = arr_unique($arr1);
$count = 0;
foreach($uni_array as $n1) {
foreach($arr as $n1) {
if($n1 == $n2) {
$count++;
}
}
echo "$n1"." => "."$count"."<br>";
$count=0;
}
}
?>

How do I get sum of distinct value from php list that was exploded? [duplicate]

This question already has answers here:
How to detect duplicate values in PHP array?
(13 answers)
Closed 7 months ago.
I would like to count the occurrence of each duplicate item in an array and end up with an array of only unique/non duplicate items with their respective occurrences.
Here is my code; BUT I don't where am going wrong!
<?php
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
//$previous[value][Occurrence]
for($arr = 0; $arr < count($array); $arr++){
$current = $array[$arr];
for($n = 0; $n < count($previous); $n++){
if($current != $previous[$n][0]){// 12 is not 43 -----> TRUE
if($current != $previous[count($previous)][0]){
$previous[$n++][0] = $current;
$previous[$n++][1] = $counter++;
}
}else{
$previous[$n][1] = $counter++;
unset($previous[count($previous)-1][0]);
unset($previous[count($previous)-1][1]);
}
}
}
//EXPECTED VALUES
echo 'No. of NON Duplicate Items: '.count($previous).'<br><br>';// 7
print_r($previous);// array( {12,1} , {21,2} , {43,6} , {66,1} , {56,1} , {78,2} , {100,1})
?>
array_count_values, enjoy :-)
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$vals = array_count_values($array);
echo 'No. of NON Duplicate Items: '.count($vals).'<br><br>';
print_r($vals);
Result:
No. of NON Duplicate Items: 7
Array
(
[12] => 1
[43] => 6
[66] => 1
[21] => 2
[56] => 1
[78] => 2
[100] => 1
)
if you want to try without 'array_count_values'
you can do with a smart way here
<?php
$input= array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$count_values = array();
foreach ($input as $a) {
#$count_values[$a]++;
}
echo 'Duplicates count: '.count($count_values);
print_r($count_values);
?>
Simplest solution (that's where PHP rocks) - ONLY duplicates:
$r = array_filter(array_count_values($array), function($v) { return $v > 1; });
and check:
print_r($r);
Result $r:
[43] => 6
[21] => 2
[78] => 2
If you have a multi-dimensional array you can use on PHP 5.5+ this:
array_count_values(array_column($array, 'key'))
which returns e.g.
[
'keyA' => 4,
'keyB' => 2,
]
I actually wrote a function recently that would check for a substring within an array that will come in handy in this situation.
function strInArray($haystack, $needle) {
$i = 0;
foreach ($haystack as $value) {
$result = stripos($value,$needle);
if ($result !== FALSE) return TRUE;
$i++;
}
return FALSE;
}
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
for ($i = 0; $i < count($array); $i++) {
if (strInArray($array,$array[$i])) {
unset($array[$i]);
}
}
var_dump($array);
You can also use it with text items array, u will get number of duplicates properly, but PHP shows
Warning: array_count_values(): Can only count STRING and INTEGER
values!
$domains =
array (
0 => 'i1.wp.com',
1 => 'i1.wp.com',
2 => 'i2.wp.com',
3 => 'i0.wp.com',
4 => 'i2.wp.com',
5 => 'i2.wp.com',
6 => 'i0.wp.com',
7 => 'i2.wp.com',
8 => 'i0.wp.com',
9 => 'i0.wp.com' );
$tmp = array_count_values($domains);
print_r ($tmp);
array (
'i1.wp.com' => 2730,
'i2.wp.com' => 2861,
'i0.wp.com' => 2807
)
Count duplicate element of an array in PHP without using in-built
function
$arraychars=array("or","red","yellow","green","red","yellow","yellow");
$arrCount=array();
for($i=0;$i<$arrlength-1;$i++)
{
$key=$arraychars[$i];
if($arrCount[$key]>=1)
{
$arrCount[$key]++;
} else{
$arrCount[$key]=1;
}
echo $arraychars[$i]."<br>";
}
echo "<pre>";
print_r($arrCount);
There is a magical function PHP is offering to you it called in_array().
Using parts of your code we will modify the loop as follows:
<?php
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$arr2 = array();
$counter = 0;
for($arr = 0; $arr < count($array); $arr++){
if (in_array($array[$arr], $arr2)) {
++$counter;
continue;
}
else{
$arr2[] = $array[$arr];
}
}
echo 'number of duplicates: '.$counter;
print_r($arr2);
?>
The above code snippet will return the number total number of repeated items i.e. form the sample array 43 is repeated 5 times, 78 is repeated 1 time and 21 is repeated 1 time, then it returns an array without repeat.
You can do it using foreach loop. (Demo)
$array = array(1,2,3,1,2,3,1,2,3,4,4,5,6,4,5,6,88);
$set_array = array();
foreach ($array as $value) {
$set_array[$value]++;
}
print_r($set_array);
Output:
Warning: Undefined array key 1 in /in/aGPqe on line 6
Warning: Undefined array key 2 in /in/aGPqe on line 6
Warning: Undefined array key 3 in /in/aGPqe on line 6
Warning: Undefined array key 4 in /in/aGPqe on line 6
Warning: Undefined array key 5 in /in/aGPqe on line 6
Warning: Undefined array key 6 in /in/aGPqe on line 6
Warning: Undefined array key 88 in /in/aGPqe on line 6
Array
(
[1] => 3
[2] => 3
[3] => 3
[4] => 3
[5] => 2
[6] => 2
[88] => 1
)
I came here from google looking for a way to count the occurrence of duplicate items in an array. Here is the way to do it simply:
$colors = ["red", "green", "blue", "red", "yellow", "blue"];
$unique_colors = array_unique($colors); // ["red", "green", "blue", "yellow"]
$duplicates = count($colors) - count($unique_colors); // 6 - 4 = 2
if ($duplicates == 0) {
echo "There are no duplicates";
}
echo "No. of Duplicates: " . $duplicates;
// Output: No. of Duplicates are: 2
How array_unique() works?
It elements all the duplicates.
ex:
Lets say we have an array as follows -
$cars = array( [0]=>"lambo", [1]=>"ferrari", [2]=>"Lotus", [3]=>"ferrari", [4]=>"Bugatti");
When you do $cars = array_unique($cars);
cars will have only following elements.
$cars = array( [0]=>"lambo", [1]=>"ferrari", [2]=>"Lotus", [4]=>"Bugatti");
To read more: https://www.php.net/manual/en/function.array-unique.php
this code will return duplicate value in same array
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
foreach($arr as $key=>$item){
if(array_count_values($arr)[$item] > 1){
echo "Found Matched value : ".$item." <br />";
}
}
$search_string = 4;
$original_array = [1,2,1,3,2,4,4,4,4,4,10];
$step1 = implode(",", $original_array); // convert original_array to string
$step2 = explode($search_string, $step1); // break step1 string into a new array using the search string as delimiter
$result = count($step2)-1; // count the number of elements in the resulting array, minus the first empty element
print_r($result); // result is 5
$input = [1,2,1,3,2,4,10];
//if give string
//$input = "hello hello how are you how hello";
//$array = explode(' ',$input);
$count_val = [];
foreach($array as $val){
$count_val[$val]++;
}
print_r($count_val);
//output ( [1] => 2 [2] => 2 [3] => 1 [4] => 1 [10] => 1 )

php dynamic number array add missing values defined by a range

I have looked and googled many times I found a few posts that are simular but I can not find the answer Im looking for so I hope you good people can help me.
I have a function that returns a simple number array. The array number values are dynamic and will change most frequently.
e.g.
array(12,19,23)
What I would like to do is take each number value in the array, compare it to a set range and return all the lower value numbers up to and including the value number in the array.
So if I do this:
$array = range(
(11,15),
(16,21),
(22,26)
);
The Desired output would be:
array(11,12,16,17,18,19,22,23)
But instead I get back all the numbers in all the ranges.
array(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26)
What would be a simple solution to resolve this?
Try this code
$range = array(
array(11,15),
array(16,21),
array(22,26),
);
$array = array(12,19,23);
$result = array();
foreach($range as $key=>$value)
{
//$range1 =$range[$key];
$min = $range[$key][0];
$max = $range[$key][1];
for($i = $min;$i<=$max;$i++)
{
if($i <= $array[$key])
{
array_push($result,$i);
}
}
}
echo "<pre>";print_r($result);
Iterate over each element, find the the start and end values you need to include, and append them to the output array:
$a = array(12,19,23);
$b = array(
range(11,15),
range(16,21),
range(22,26)
);
$c = array();
foreach ($a as $k => $cap) {
$start = $b[$k][0];
$finish = min($b[$k][count($b[$k])-1], $cap);
for ($i = $start; $i <= $finish; $i++) {
$c[] = $i;
}
}
print_r($c);
prints
Array
(
[0] => 11
[1] => 12
[2] => 16
[3] => 17
[4] => 18
[5] => 19
[6] => 22
[7] => 23
)
My solution is probably not the most efficient, but here goes:
$numbers = array(12,19,23);
$ranges = array(
array(11,15),
array(16,21),
array(22,26)
);
$output = array();
// Loop through each of the numbers and ranges:
foreach($numbers as $num) {
foreach($ranges as $r) {
if ($num >= $r[0] && $num <= $r[1]) {
// This is the correct range
// Array merge to append elements
$output = array_merge($output, range($r[0], $num));
break;
}
}
}
// Sort the numbers if you wish
sort($output, \SORT_NUMERIC);
print_r($output);
Produces:
Array
(
[0] => 11
[1] => 12
[2] => 16
[3] => 17
[4] => 18
[5] => 19
[6] => 22
[7] => 23
)

Increment integer in set range

If I want to loop through an array and then use them as looped increment counters, how would I do that?
E.g. I have up to 5 values stored in an array. I want to loop through them, and in the forst loop I want to use a specific value, then for the second another specific value.
Pseudo code below, but how do I bring in the second array into the picture? The first range is going to dynamic and empty or have up to 5 values. The second will be fixed.
$array = array(2,6,8); // Dynamic
$array2 = array(11,45,67,83,99); Fixed 5 values
foreach ($array as $value) {
// First loop, insert or use both 2 and 11 together
// Second loop, insert or use both 6 and 45
// Third loop, insert or use both 8 and 67
}
Use $index => $val:
foreach ($array2 as $index => $value) {
if ( isset($array[ $index ]) ) {
echo $array[ $index ]; // 2, then 6, then 8
}
echo $value; // 11, then 45, then 67, then 83, then 99
}
See it here in action: http://codepad.viper-7.com/gpPmUG
If you want it to stop once you're at the end of the first array, then loop through the first array:
foreach ($array as $index => $value) {
echo $value; // 2, then 6, then 8
echo $array2[ $index ]; // 11, then 45, then 67
}
See it here in action: http://codepad.viper-7.com/578zfQ
You can try this-
foreach ($array as $index => $value) {
echo $array[ $index ]; // 2, then 6, then 8
echo $array2[ $index ]; // 11, then 45, then 67
}
Here's a clean and simple solution, that does not uses useless and heavy non standard libraries:
$a = count($array);
$b = count($array2);
$x = ($a > $b) ? $b : $a;
for ($i = 0; $i < $x; $i++) {
$array[$i]; // this will be 2 the first iteration, then 6, then 8.
$array2[$i]; // this will be 11 the first iteration, then 45, then 67.
}
We just use $i to identify the same position of the two arrays inside the main for loop in order to use them together. The main for loop will iterate the correct number of times so that none of the two arrays will use undefined indexes (causing notices errors).
Determine the minimum length of both arrays.
Then loop your index i from 1 to the minimum length.
Now you can use the i-th element of both arrays
Here is what I think you want:
foreach($array as $value){
for($x = $value; $array[$value]; $x++){
//Do something here...
}
}
You can use a MultipleIterator:
$arrays = new MultipleIterator(
MultipleIterator::MIT_NEED_ANY|MultipleIterator::MIT_KEYS_NUMERIC
);
$arrays->attachIterator(new ArrayIterator([2,6,8]));
$arrays->attachIterator(new ArrayIterator([11,45,67,83,99]));
foreach ($arrays as $value) {
print_r($value);
}
will print:
Array ( [0] => 2 [1] => 11 )
Array ( [0] => 6 [1] => 45 )
Array ( [0] => 8 [1] => 67 )
Array ( [0] => [1] => 83 )
Array ( [0] => [1] => 99 )
If you want it to require both arrays to have a value, change the flags to
MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_NUMERIC
which will then give
Array ( [0] => 2 [1] => 11 )
Array ( [0] => 6 [1] => 45 )
Array ( [0] => 8 [1] => 67 )

PHP::How merge 2 arrays when array 1 values will be in even places and array 2 will be in odd places? [duplicate]

This question already has answers here:
Transpose and flatten two-dimensional indexed array where rows may not be of equal length
(4 answers)
Closed 10 months ago.
How can I merge two arrays when array 1 values will be in even places and array 2 will be in odd places?
Example:
$arr1=array(11, 34,30);
$arr2=array(12, 666);
$output=array(11, 12, 34, 666,30);
This will work correctly no matter the length of the two arrays, or their keys (it does not index into them):
$result = array();
while(!empty($arr1) || !empty($arr2)) {
if(!empty($arr1)) {
$result[] = array_shift($arr1);
}
if(!empty($arr2)) {
$result[] = array_shift($arr2);
}
}
Edit: My original answer had a bug; fixed that.
try this
$arr1=array(11,34,30,35);
$arr2=array(12,666,23);
$odd= array_combine(range(0,2*count($arr1)-1,2), $arr1);
$even = array_combine(range(1,2*count($arr2)-1,2), $arr2);
$output=$odd+$even;
ksort($output);
echo "<pre>";
print_r($output);
returns
Array
(
[0] => 11
[1] => 12
[2] => 34
[3] => 666
[4] => 30
[5] => 23
[6] => 35
)
Assuming $arr1 and $arr2 are simple enumerated arrays of equal size, or where $arr2 has only one element less that $arr1.
$arr1 = array(11, 34);
$arr2 = array(12, 666);
$output = array();
foreach($arr1 as $key => $value) {
$output[] = $value;
if (isset($arr2[$key])) {
$output[] = $arr2[$key];
}
}
Go through array with more items, use loop index to access both arrays and combine them into resulting one as required...
$longer = (count($arr1) > count($arr2) ? $arr1 : $arr2);
$result = array();
for ($i = 0; $i < count($longer); $i++) {
$result[] = $arr1[i];
if ($arr2[i]) {
$result[] = $arr2[i];
} else {
$result[] = 0; // no item in arr2 for given index
}
}

Categories