Remove duplicate arrays - php

I have following arrays :
$t[0] = array('one'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4));
$t[1] = array('two'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4));
$t[2] = array('one'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4));
$t[3] = array('three'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4));
I want to remove duplicate arrays from above arrays, the result should be :
$t[0] = array('one'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4));
$t[1] = array('two'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4));
$t[2] = array('three'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4));
Thanks

$t[0] = array('one'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4));
$t[1] = array('two'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4));
$t[2] = array('one'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4));
$t[3] = array('three'=>array('a'=>2,'b'=>3,'c'=>2,'e'=>4));
$array = array();
foreach ($t as $key=>$value) {
if (!in_array ($value, $array)) {
$array [$key] = $value;
}
}
print_r ($array);

From what I understand what you want to do is remove duplicate keys from a multi-dimensional array.
Something like this should do the trick:
$new_array = array();
foreach($t as $array) {
foreach($array as $k => $v) {
if(!array_key_exists($k, $new_array)) {
$new_array[$k] = $v;
}
}
}

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);

How to group same array values in PHP?

I want to group in subarray the identical values ​​while preserving the original order of each group of values.
I want this :
array('a','b','b','c','c','c','a','a');
to become :
array( array('a'),array('b','b'),array('c','c','c'),array('a','a'));
$source = array('a','b','b','c','c','c','a','a');
$tempvalue = false;
$temparr = array();
$new = array();
foreach ($source as $value) {
if ($tempvalue && $value != $tempvalue){
$new[] = $temparr;
$temparr = array();
}
$temparr[] = $value;
$tempvalue = $value;
}
$new[] = $temparr;
echo json_encode($new);
output:
[["a"],["b","b"],["c","c","c"],["a","a"]]

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);

How to combine an array with the same index

I have an array as follows
[{"2":[2]},{"9":[4,10,11,12,13,14,15,16,17,19,20,21,22,23,24,25,26,27,28,30,32,33,34,36,41]},{"2":[5,6,9]},{"7":[7,8]},{"9":[3]}]
is there any possible way to combine the value for the same index ?
Yes, it is possible decoding the json string and using array_reduce:
$arr = json_decode('[{"2":[2]},{"9":[4,10,11,12,13,14,15,16,17,19,20,21,22,23,24,25,26,27,28,30,32,33,34,36,41]},{"2":[5,6,9]},{"7":[7,8]},{"9":[3]}]');
$arr = array_reduce($arr, function ($carry, $item) {
$key = key($item);
if (isset($carry[$key])) {
$carry[$key]->$key = array_merge($carry[$key]->$key, $item->$key);
} else {
$carry[$key] = $item;
}
return $carry;
});
echo json_encode(array_values($arr));
The result is:
[{"2":[2,5,6,9]},{"9":[4,10,11,12,13,14,15,16,17,19,20,21,22,23,24,25,26,27,28,30,32,33,34,36,41,3]},{"7":[7,8]}]
<?php
$json = '[{"2":[2]},{"9":[4,10,11,12,13,14,15,16,17,19,20,21,22,23,24,25,26,27,28,30,32,33,34,36,41]},{"2":[5,6,9]},{"7":[7,8]},{"9":[3]}]';
$multi_array = json_decode($json, true);
$result = [];
foreach($multi_array as $arr)
{
foreach($arr as $k => $v)
{
$temp = isset($result[$k]) ? $result[$k] : [];
$result[$k] = array_merge($temp, $v);
}
}
print_r($result);
This is another option:
$old = json_decode('[{"2":[2]},{"9":[4,10,11,12,13,14,15,16,17,19,20,21,22,23,24,25,26,27,28,30,32,33,34,36,41]},{"2":[5,6,9]},{"7":[7,8]},{"9":[3]}]', true);
$new = [];
array_map(function ($arr) use (&$new) {
$key = key($arr);
$new[$key] = array_merge(current($arr), !empty($new[$key]) ? $new[$key] : []);
}, $old);
echo json_encode($new);
Output:
{"2":[5,6,9,2],"9":[3,4,10,11,12,13,14,15,16,17,19,20,21,22,23,24,25,26,27,28,30,32,33,34,36,41],"7":[7,8]}

How to get values in an array this way with PHP?

From:
$arr = array(array('key1'=>'A',...),array('key1'=>'B',...));
to:
array('A','B',..);
$output = array();
foreach ($arr as $array_piece) {
$output = array_merge($output, $array_piece);
}
return array_values($output);
On the other hand, if you want the first value from each array, what you want is...
$output = array();
foreach ($arr as $array_piece) {
$output[] = array_unshift($array_piece);
}
But I'm thinking you want the first one.
Relatively simple conversion by looping:
$newArray = array()
foreach ($arr as $a) {
foreach ($a as $key => $value) {
$newArray[] = $value;
}
}
Or, perhaps more elegantly:
function flatten($concatenation, $subArray) {
return array_merge($concatenation, array_values($subArray));
}
$newArray = array_reduce($arr, "flatten", array());
John's solution is also nice.
Something like this should work
<?
$arr = array(array('key1'=>'A','key2'=>'B'),array('key1'=>'C','key2'=>'D'));
$new_array = array();
foreach ($arr as $key => $value) {
$new_array = array_merge($new_array, array_values($value));
}
var_export($new_array);
?>
If you want all the values in each array inside your main array.
function collapse($input) {
$buf = array();
if(is_array($input)) {
foreach($input as $i) $buf = array_merge($buf, collapse($i));
}
else $buf[] = $input;
return $buf;
}
Above is a modified unsplat function, which could also be used:
function unsplat($input, $delim="\t") {
$buf = array();
if(is_array($input)) {
foreach($input as $i) $buf[] = unsplat($i, $delim);
}
else $buf[] = $input;
return implode($delim, $buf);
}
$newarray = explode("\0", unsplat($oldarray, "\0"));

Categories