How to get repeated value counting in my array? - php

I have already try like this
<?php
echo '<pre>';
$arrayName = array(1,2,3,4,5,6,2,3,1 );
$arr= array_count_values(array_column($arrayName,'5'));
print_r($arr);
?>
I just want to counting of repeated value

This script counts the amount of each value.
<?php
echo '<pre>';
$arrayName = [1,2,3,4,5,6,2,3,1];
$arr = [];
foreach ($arrayName as $item) {
if (empty($arr[$item]))
$arr[$item] = 0;
$arr[$item] += 1;
}
print_r($arr);

if you want count the values
$arrayName = array(1,2,3,4,5,6,2,3,1 );
$arr =array_count_values($arrayName);
print_r($arr);

Why are you using array_column if you only want to count repeated values
you can do this
<?php
echo '<pre>';
$arrayName = array(1,2,3,4,5,6,2,3,1 );
$arr= array_count_values($arrayName);
print_r($arr);
?>

Related

PHP split array into 2 arrays by value

I'm having a hard time figuring out how to implement this so here it is. I have an array
$arr = array("purchase_order_details_id"=>array(
0=>"POD1",
1=>"POD1",
2=>"POD2",
),
"quantity_received"=>array(
0=>5,
1=>10,
2=>20
)
);
I want to split the arrays into two. Into something like this.
$pod_2 = array("purchase_order_details_id"=>array(
0=>"POD1",
1=>"POD1"
),
"quantity_received"=>array(
0=>5,
1=>10
));
$pod_1 = array("purchase_order_details_id"=>array(
2=>"POD2"
),
"quantity_received"=>array(
2=>20
));
Anyone has an idea on how to do this ? Any thoughts is appreciated. Thanks
I use array_intersect to find the POs in a loop of unique POs.
Then I use array_inyersect_key to get the quantity.
This requires only one iteration per unique Purchase_order_detali_id.
Meaning it has a much better performance than looping the full array.
Edit: added extract to create the two variables. But I would rather keep them in the array if I was you.
$pods = array_unique($arr["purchase_order_details_id"]);
Foreach($pods as $pod){
$PO = array_intersect($arr["purchase_order_details_id"], [$pod]);
$qt = array_intersect_key($arr["quantity_received"], $PO);
$new[$pod] = ["purchase_order_details_id" => $PO, "quantity_received" => $qt];
}
Var_dump($new);
extract($new);
https://3v4l.org/dBpuJ
Try with below code:
$array = array();
foreach($arr['purchase_order_details_id'] as $key => $val)
{
$array[$val]['purchase_order_details_id'][] = $val;
$array[$val]['quantity_received'][] = $arr['quantity_received'][$key];
}
echo "<pre>";
print_r($array);
echo "</pre>";
extract($array);
echo "<pre>";
print_r($POD1);
echo "</pre>";
echo "<pre>";
print_r($POD2);
echo "</pre>";
foreach ($arr as $key => $val) {
$size = ceil(count($val) / 2);
$arr2 = array_chunk($val, $size, true);
$pod_2[$key] = $arr2[0];
$pod_1[$key] = $arr2[1];
}
var_dump($pod_2);
var_dump($pod_1);

array_combine function with duplicate value

<?php
$codes = 'test1,test1,test3';
$names = '1226261693assistenza-pc-1-1.jpeg,1226261693cobinhood.png,1226261693a.png';
$array = array_combine ( explode(',',$codes), explode(',', $names) );
foreach($array as $k=>$v)
{
echo $k."<br>";
echo $v."<br>";
}
?>
How to get duplicate value in array combine function.
I would appreciate any help and tips.

Find difference value from two array in php [duplicate]

This question already has answers here:
Compare two array and get all differences
(2 answers)
Closed last year.
I want to get difference value from array and also want to compare that difference value is from which array.
First Array => Variable : $first_array
Array {607,608,609}
Second Array => Variable : $second_array
Array {607,608,609,610}
want to get output like Difference value : 610...... From Array :- $second_array
How can i get ? please help me ....
You can use array_diff()
$result=array_diff($first_array,$second_array);
print_r($result);
Try this :
$first_array=array(607,608,609);
$second_array=array(607,608,609,610);
$result=calculate_diff($second_array,$first_array);
print_r($result);
function calculate_diff($array1,$array2)
{
$diff = [];
$larger_array = $array2;
$smaller_array = $array1;
if(count($array1) > count($array2))
{
$larger_array = $array1;
$smaller_array = $array2;
}
foreach($larger_array as $ele)
{
if(!in_array($ele,$smaller_array))
{
$diff[] = $ele;
}
}
return $diff;
}
it will work
<?php
$a=Array(607,608,609,610);
$b=Array (607,608,609);
$result=array_diff($a,$b);
print_r($result);
?>
or else try this
<?php
$array2 = array(607,608,609);
$array1 = array(607,608,609,275);
foreach ($array1 as $value)
{
if(in_array($value, $array2))
{
$key = array_search($value, $array2);
$key1 = array_search($value, $array1);
unset($array2[$key]);
unset($array1[$key1]);
//echo "yes<br>";
}
}
$merge = array_merge($array1,$array2);
print_r($merge);
?>
Use array_diff
<?php
$a=Array(607,608,609,610);
$b=Array (607,608,609);
$res=array_diff($a,$b);
print_r($res); // output 610
?>
Try this:
$a = array(607,608,609,610);
$b = array(607,608,609);
$c = array_diff($a,$b);
print_r($c);
Try This:
$a1=array(607,608,609,610);
$a2=array(607,608,609);
$result=array_diff($a1,$a2);
print_r($result);
Output :- Array ( [3] => 610 )
Try this by merging them and then using array_diff, array_diff_assoc and array_unique.
https://3v4l.org/Z1vpe
$first = Array(607,608,609,610);
$second = Array(800,607,608,609);
$Fcount = count($first);
$arr = array_merge($first, $second);
$arrc= array_diff($arr, array_diff_assoc($arr, array_unique($arr)));
foreach ($arrc as $key => $value){
if($key < $Fcount){
echo "first array ". $value . "\n";
}else{
echo "second array " . $value . "\n";
}
}
Edited to add how to find which array the value is in. https://3v4l.org/W7rch
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue","r"=>"black");
$d = array_merge($a1,$a2);
output:array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"red","f"=>"green","g"=>"blue","r"=>"black")
$result=array_diff($d,array_intersect($a2,$a1));
print_r($result);
output:array("d"=>"yellow","r"=>"black");

How to compare two array in php

while($row = $update_post->fetch_array()){
//Explodes checkbox values
$res = $row['column_name'];
$field = explode(",", $res);
$arr = array('r1','r2,'r3','r4','r5','r6');
if (in_array($arr, $field)) {
echo "<script>alert('something to do')</script>";
}else{
echo "<script>alert('something to do')</script>";
}
}
How to check if value of $arr is equal to the value of $field.
Thank you in Advance.
If you want to match two array than you need to use array_intersect() here.
If you want to check specific value with in_array() than you need to use loop here as:
<?php
$res = $row['column_name'];
$field = explode(",", $res);
$arr = array('r1','r2','r3','r4','r5','r6');
foreach ($arr as $value) {
if(in_array($value, $field)) {
echo "success";
}
else{
echo "failed";
}
}
?>
According to manual: in_array — Checks if a value exists in an array
Also note that, you have a syntax error in your array:
$arr = array('r1','r2,'r3','r4','r5','r6'); // missing quote here for r2
Update:
If you want to use array_intersect() than you can check like that:
<?php
$arr1 = array('r1','r2');
$arr2 = array('r1','r2','r3','r4','r5','r6');
$result = !empty(array_intersect($arr1, $arr2));
if($result){
echo "true";
}
else{
echo "false";
}
?>
DEMO
Update 2:
If you want to check which value are you getting by using array_intersect() than you can use like:
<?php
$arr1 = array('r2');
$arr2 = array('r1','r2','r3','r4','r5','r6');
$result = array_intersect($arr1, $arr2);
if(count($result)){
echo "Following ID(s) found: ".implode(",",$result);
}
?>
DEMO
compare two array by array_intersect then check with count to know matches array value has...
array_intersect
Compare the values of two arrays, and return the matches:
while($row = $update_post->fetch_array()){
//Explodes checkbox values
$res = $row['column_name'];
$field = explode(",", $res);
$arr = array('r1','r2','r3','r4','r5','r6');
if (count(array_intersect($arr, $field)) > 0) {
echo "<script>alert('duplicate array')</script>";
}else{
echo "<script>alert('something to do')</script>";
}
}

Increasing array elements while in foreach loop in php? [duplicate]

This question already has answers here:
How does PHP 'foreach' actually work?
(7 answers)
Closed 8 years ago.
Consider the code below:
<?php
$arr = array();
$arr['b'] = 'book';
foreach($arr as $key=>$val) {
print "key=>$key\n";
if(!isset($arr['a']))
$arr['a'] = 'apple';
}
?>
It is not displaying 'a'. How foreach works with hash-table(array), to traverse each element. If lists are implement why can't I add more at run time ?
Please don't tell me that I could do this task with numeric based index with help of counting.
Foreach copies structure of array before looping(read more), so you cannot change structure of array and wait for new elements inside loop. You could use while instead of foreach.
$arr = array();
$arr['b'] = 'book';
reset($arr);
while ($val = current($arr))
{
print "key=".key($arr).PHP_EOL;
if (!isset($arr['a']))
$arr['a'] = 'apple';
next($arr);
}
Or use ArrayIterator with foreach, because ArrayIterator is not an array.
$arr = array();
$arr['b'] = 'book';
$array_iterator = new ArrayIterator($arr);
foreach($array_iterator as $key=>$val) {
print "key=>$key\n";
if(!isset($array_iterator['a']))
$array_iterator['a'] = 'apple';
}
I think you need to store array element continue sly
Try
<?php
$arr = array();
$arr['b'] = 'book';
foreach($arr as $key=>$val) {
print "key=>$key\n";
if(!isset($arr['a']))
$arr['a'][] = 'apple';
}
print_r($arr);
?>
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.
http://cz2.php.net/manual/en/control-structures.foreach.php
Try this:
You will get values.
<?php
$arr = array();
$arr['b'] = 'book';
foreach($arr as $key=>$val) {
print "key=>$key\n";
if(!isset($arr['a']))
$arr['a'] = 'apple';
}
echo '<pre>';
print_r($arr);
?>
Output:
key=>b
<pre>Array
(
[b] => book
[a] => apple
)
If you want to check key exist or not in array use array_key_exists function
Eg:
<?php
$arr = array();
$arr['b'] = 'book';
print_r($arr); // prints Array ( [b] => book )
if(!array_key_exists("a",$arr))
$arr['a'] = 'apple';
print_r($arr); // prints Array ( [b] => book [a] => apple )
?>
If you want to use isset condition try like this:
$arr = array();
$arr['b'] = 'book';
$flag = 0;
foreach($arr as $key=>$val) {
print "key=>$key\n";
if(!isset($arr["a"]))
{
$flag = 1;
}
}
if(flag)
{
$arr['a'] = 'apple';
}
print_r($arr);
How about using for and realtime array_keys()?
<?php
$arr = array();
$arr['b'] = 'book';
for ($x=0;$x<count($arr); $x++) {
$keys = array_keys($arr);
$key = $keys[$x];
print "key=>$key\n";
if(!isset($arr['a']))
$arr['a'] = 'apple';
}

Categories