PHP - count array based other array (array('a','d')=array('d','a')) - php

I have array like this:
$array1 = array(Array('a','d'),
Array('c','a'),
Array('d','a'),
Array('a','b','c','d','e'),
);
$array2 = array(array('a','d'), array('a','b','c','d','e')) ;
$result = array();
Here's my code:
foreach ($array2 as $part) {
$key = implode(', ', $part);
if( ! array_key_exists ($key, $array1)) {
$result[$key] = 0;
}
$result[$key] = $result[$key] + 1;
}
foreach ($result as $key => $value) {
echo "$value of {$key}<br/>";
}
I want to count values $array2 based on $array1
I got this one:
1 of a,d
1 of a,b,c,d,e
But I want a result like this:
3 of a,d
1 of a,b,c,d,e
If anybody wonders why there's (3 of a,d), it count from array('a','d'), array('d','a') also counted as array('a','d') and array('a','b','c','d','e')

Try this. Here is a working demo https://eval.in/117810
<?
$array1 = array(array('a','d'),
array('c','a'),
array('d','a'),
array('a','b','c','d','e'),
);
$array2 = array(array('a','d'), array('a','b','c','d','e')) ;
$result = array();
foreach ($array2 as $key=>$part2) {
sort($part2);
if(!isset($result[$key]))$result[$key]=0;
foreach($array1 as $part1) {
$intersect = array_intersect($part1, $part2);
sort($intersect);
if ($intersect === $part2) {
$result[$key]++;
}
}
}
foreach($result as $k=>$v) {
echo $v . " of " . implode(',', $array2[$k]) . "<br/>";
}
?>

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

reshuffle array value in foreach loop

I am adding other value array inside foreach loop which working fine for me.
$i = true;
$array = array('red', 'blue');
foreach($array as $key => & $value) {
echo $value . '<br />';
if ($i === true) {
$others = array('white', 'yellow');
foreach($others as $key => & $other_value) {
$array[] = $other_value;
}
}
$i = false;
}
Output
red
blue
white
yellow
but i want to reshuffle array value inside foreach loop need output like below
red
white
yellow
blue
You won't be able to do it on $array without some serious array_slice()ing. So, just assign to another array $result and you will get the $other array inserted between the first and second elements of $array:
$i = true;
$array = array('red', 'blue');
foreach($array as $value) {
$result[] = $value; // here...
if ($i === true) {
$others = array('white', 'yellow');
foreach($others as $other_value) {
$result[] = $other_value; // and here...
}
}
$i = false;
}
If needed (for whatever reason) $array = $result;
A cool solution would be like this:
$array = array('red', 'blue');
$others = array('white', 'yellow');
$temp = array_combine($array,$others);
$final = array();
foreach($temp as $key => $value) {
array_push($final,$key,$value);
}
$array = $final;
$i = true;
$array = array('red', 'blue');
foreach($array as $key => & $value) {
echo $value . '<br />';
if ($i === true) {
$a1= $array;
$a2= array($value);
$result=array_diff($a1,$a2);
$others = array('white', 'yellow');
$array = array_merge($others,$result);
}
$i = false;
}
see output

How to check for duplicates in an array and then do something with their values in PHP?

I have an array for example ("1:2","5:90","7:12",1:70,"29:60") Wherein ID and Qty are separated by a ':' (colon), what I want to do is when there's a duplicate of IDs the program will add the qty and return the new set of arrays so in the example it will become ("1:72","5:90","7:12","29:60").
Ex.2
("1:2","5:90","7:12","1:70","29:60","1:5")
becomes
("1:77","5:90","7:12","29:60").
<?php
$arr = array("1:2","5:90","7:12", "1:70","29:60");
$newArr = array();
foreach($arr as $key => $value) {
list($id, $quantity) = explode(':', $value);
if(isset($newArr[$id]))
$newArr[$id] += $quantity;
else
$newArr[$id] = $quantity;
}
foreach($newArr as $key => $value) {
$newArr[$key] = "$key:$value";
}
print_r($newArr);
Simple step by step:
$arr = array("1:2","5:90","7:12","1:70","29:60");
$tmp = array();
foreach($arr as $item)
{
list($id, $value) = explode(':', $item);
if (isset($tmp[$id]))
$tmp[$id] += $value;
else
$tmp[$id] = $value;
}
$arr = array();
foreach($tmp as $id => $value)
array_push($arr, $id . ':' . $value);
var_dump($arr);
Assuming the data format is an actual array, or you can parse it into one:
$data = array("1:2","5:90","7:12","1:70","29:60","1:5");
$values = array();
foreach ($data as $value) {
list($id, $qty) = explode(':', $value);
if (isset($values[$id])) {
$values[$id] += $qty;
} else {
$values[$id] = $qty;
}
}
foreach ($values as $id => &$qty) {
$qty = "$id:$qty";
}
$values = array_values($values);
Try to do like this
$array = array("1:2","5:90","7:12","1:70","29:60","1:5");
$result = array();
$sum = array();
foreach($array as $value)
{
list($k,$v) = explode(':',$value);
$sum[$k] += $v;
$result[$k] = $k.':'.$sum[$k];
}
unset($sum);
print_r($result);

How do i get unique value from an array?

$a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
From the above array i need the value Dog alone. how can i get the unique value from an array?. is there any functions in php?...
Thanks
Ravi
Have a look at:
http://php.net/function.array-unique
and maybe:
http://php.net/function.array-count-values
$a = array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
$counted = array_count_values($a);
$result = array();
foreach($counted as $key => $value) {
if($value === 1) {
$result[] = $key;
}
}
//$result is now an array of only the unique values of $a
print_r($result);
function getArrayItemByValue($search, $array) {
// without any validation and cheking, plain and simple
foreach($array as $key => $value) {
if($search === $value) {
return $key;
}
}
return false;
}
then try using it:
echo $a[getArrayitembyValue('Dog', $a)];
Try with:
$a = array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
$aFlip = array_flip($a);
$unique = array();
foreach ( array_count_values( $a ) as $key => $count ) {
if ( $count > 1 ) continue;
// $unique[ array_search($key) ] = $key;
$unique[ $aFlip[$key] ] = $key;
}
Use following function seems to be working & handy.
<?php
$array1 = array('foo', 'bar', 'xyzzy', 'xyzzy', 'xyzzy');
$dup = array_unique(array_diff_assoc($array1, array_unique($array1)));
$result = array_diff($array1, $dup);
print_r($result);
?>
You can see its working here - http://codepad.org/Uu21y6jf
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
$result = array_unique(a);
print_r($result);
try this one...

Categories