I have this array. I want to duplicate all records form an array. I tried array_unique but it's removing duplicate but doesn't remove orignal value.
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 1
[4] => 6
[5] => 1
[6] => 23
[7] => 2
)
I want to remove all duplicate value like 1 and 2 and I want this output :
Array
(
[0] => 3
[1] => 6
[2] => 23
)
You could use a combination of array_filter and array_count_values.
$values = [1,2,3,1,6,1,23,2];
$result = array_filter(array_count_values($values), function($x) {
return $x === 1;
});
print_r(array_keys($result));
Result:
Array
(
[0] => 3
[1] => 6
[2] => 23
)
You can also use array_intersect with array_count_values.
Array_intersect returns values that is 1, and array_keys returns the keys (values).
$values = [1,2,3,1,6,1,23,2];
$result = array_keys(array_intersect(array_count_values($values), [1]));
var_dump($result); //[3,6,23]
https://3v4l.org/cHU5E
Another option is to use array_unique and the use array_diff_assoc() to get a list of what has been removed.
Using that array list in array_diff results in the values that is not duplicated.
$values = [1,2,3,1,6,1,23,2];
$diff = array_diff_assoc($values, array_unique($values));
$result = array_diff($values, $diff);
var_dump($result); //[3,6,23]
https://3v4l.org/XM5sk
Related
I need to remove values = 99 from array. But after the array_diff the Highcharts doesn't show any data anymore.
$a = array ("$object->hdc_wed_01", "$object->hdc_wed_02", "$object->hdc_wed_03", "$object->hdc_wed_04", "$object->hdc_wed_05", "$object->hdc_wed_06", "$object->hdc_wed_07", "$object->hdc_wed_08", "$object->hdc_wed_09", "$object->hdc_wed_10", "$object->hdc_wed_11", "$object->hdc_wed_12", "$object->hdc_wed_13", "$object->hdc_wed_14", "$object->hdc_wed_15", "$object->hdc_wed_16", "$object->hdc_wed_17", "$object->hdc_wed_18", "$object->hdc_wed_19", "$object->hdc_wed_20", "$object->hdc_wed_21", "$object->hdc_wed_22", "$object->hdc_wed_23", "$object->hdc_wed_24", "$object->hdc_wed_25", "$object->hdc_wed_26", "$object->hdc_wed_27", "$object->hdc_wed_28", "$object->hdc_wed_29", "$object->hdc_wed_30", "$object->hdc_wed_31", "$object->hdc_wed_32", "$object->hdc_wed_33", "$object->hdc_wed_34", "$object->hdc_wed_35");
$b = array (99);
$c = array_diff($a, $b);
$handicaps = json_encode($c);
$handicaps_clean = str_replace('"',"", $handicaps);
If I'm using $handicaps = json_encode($a); (the unfiltered array) it works fine, but the values equal to 99 are in it. I want to remove them.
As I clearly understood you there's an array of values like $ar = [3,5,2,5,6,7,99,6,5,7,8,99] and some specific array with non-desired values like $remove = [99].
If so, then you can use unset() function to remove such elements from $ar:
$ar = [3,5,2,5,6,7,99,6,5,7,8,99];
$remove = [99];
foreach($ar as $ind => $val){
if (in_array($val,$remove)) unset($ar[$ind]);
}
Outputs:
Array
(
[0] => 3
[1] => 5
[2] => 2
[3] => 5
[4] => 6
[5] => 7
[7] => 6
[8] => 5
[9] => 7
[10] => 8
)
Demo
Hope you've figured out the idea.
How do I find the keys array of disciplines that have appropriate values?
For Example:
$arr1 = [2, 4, 12];
$result = [...] // Var_dump in link
in_array($arr1, $result);
Regardless of their order, I need to see if there is a set of keys or a no.
But in_array() does not work.
Thanks!
Dump array
Update (01.03.2017)
This is my version of the solution to this problem
$findResult = array_filter($result, function($val)use($get){
$requiredDisciplines = [1, $get['disciplines']['second'], $get['disciplines']['third'], $get['disciplines']['four']]; // запрос
$activePriorities = [];
foreach ($val['disciplines'] as $discipline) {
if (in_array($discipline['id'], $requiredDisciplines)) {
$activePriorities[] = $discipline['priority'];
}
}
for ($i = 0; $i<3; $i++){
if(!in_array($i, $activePriorities))
return false;
}
/*if(in_array(0, $activePriorities) && in_array(1, $activePriorities) && in_array(2, $activePriorities) != true)
return false;*/
// print_r($activePriorities);
return true;
});
I've got a versatile one-liner that will give you all of the arrays containing matches. (so you can derive the keys or the count from that). (demo)
This function is only set to compare the values between the needle and the haystack, but can be set to search keys-values pairs by replacing array_intersect with array_intersect_assoc and adding ,ARRAY_FILTER_USE_BOTH to the end of the filter function (reference: 2nd snippet # https://stackoverflow.com/a/42477435/2943403)
<?php
// haystack array
$result[]=array(1,2,3,4,5,6,7,8,9,10,11,12);
$result[]=array(1,3,5,7,9,11);
$result[]=array(2,4,6,8,10,12);
// needle array
$arr1=array(2,4,12);
//one-liner:
$qualifying_array=array_filter($result,function($val)use($arr1){if(count(array_intersect($val,$arr1))==count($arr1)){return $val;}});
/*
// multi-liner of same function:
$qualifying_array=array_filter(
$result,
function($val)use($arr1){ // pass in array to search
if(count(array_intersect($val,$arr1))==count($arr1)){ // total pairs found = total pairs sought
return $val;
}
}
);*/
echo 'Total sub-arrays which contain the search array($arr1): ',sizeof($qualifying_array),"<br>";
echo 'Keys of sub-arrays which contain the search array($arr1): ',implode(',',array_keys($qualifying_array)),"<br>";
echo 'Is search array($arr1) in the $result array?: ',(sizeof($qualifying_array)?"True":"False"),"<br>";
echo "<pre>";
print_r($qualifying_array);
echo "</pre>";
The output:
Total sub-arrays which contain the search array($arr1): 2
Keys of sub-arrays which contain the search array($arr1): 0,2
Is search array($arr1) in the $result array?: True
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
[11] => 12
)
[2] => Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
[5] => 12
)
)
Does anyone know how I can create an Array?
$string = '3-1-0-1.11,3-1-1-1.12,3-1-2-1.13,3-1-3-1.14,3-2-0-1.02,3-2-1-1.03,3-2-2-1.04,3-2-3-1.05,3-2-4-1.06,3-3-0-3.23,3-3-1-3.24,3-3-2-3.25,3-3-3-3.26';
$array = explode(',', $string);
$last_entry = null;
foreach ($array as $current_entry) {
$first_char = $current_entry[2]; // first Sign
if ($first_char != $last_entry) {
echo '<h2>'. $first_char . '</h2><br>';
}
echo $current_entry[4] . '<br>';
$last_entry = $first_char;
}
I need an Array like this:
Array
(
[1] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[2] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
[3] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
The first number 3 and other numbers 3 after comma are not important.
Important numbers are second and third numbers in values of $array.
I need categories. Example: if the first (second) number is 1 create Category 1 and subcategory 1 where first (second) number actual is 1.
To create an an array, you need to declare it using array() feature. Below I have created a blank array.
$array = array();
An array with values looks like this
$array = array("string", "string2", "string3");
To add values in an array, you use the array_push method.
array_push($array, "string4");
On multidimensional arrays, declare the array then add the inner array, below is objct oriented
$array = array("string"=>array("innerstring", "innerstring2"), "string2" => array("innerstring3", "innerstring4"), "string3" => array("innerstring5", "innerstring6"));
and procedural
$array=array(array("string", "innerstring", "innerstring2",), array("string2", "innerstring3", "innerstring4"), array("string3", "innerstring5", "innerstring6"));
Try next script:
$string = '3-1-0-1.11,3-1-1-1.12,3-1-2-1.13,3-1-3-1.14,3-2-0-1.02,3-2-1-1.03,3-2-2-1.04,3-2-3-1.05,3-2-4-1.06,3-3-0-3.23,3-3-1-3.24,3-3-2-3.25,3-3-3-3.26';
foreach(explode(',', $string) as $tpl) {
$tpl = explode('-', $tpl);
$tpl[3] = explode('.', $tpl[3]);
$result[$tpl[1]][$tpl[2]][$tpl[3][0]] = !empty($tpl[3][1]) ? $tpl[3][1] : null;
}
var_dump($result);
This question already has answers here:
Cartesian Product of N arrays
(10 answers)
Closed 8 years ago.
Let's assume I have an two arrays and I want to merge every value with the other value of the array.
Array 1
array (size=2)
0 => 1
1 => 2
Array 2
array (size=2)
0 => 3
1 => 4
Wanted result array / string:
array (size=4)
0 => '1,3'
1 => '1,4'
2 => '2,3'
3 => '2,4'
I can't get my head around it. Obviously I would need to merge every one array key/value with the other ones. Is there a more elegant way then doing this in a while/foreach loop?
You need a foreach loop inside a foreach loop. (Actualy, you will have to loop through both arrays to get a concatenated product of both arrays, you don't actually need two foreach loops). You could mix: whiles, foreach, for, or php filter/intersect array functions
Example
$array1 = array(1,2);
$array2 = array(3,4);
$result = array();
foreach ($array1 as $item1){
foreach($array2 as $item2){
$result[] = $item1.','.$item2;
}
}
https://eval.in/215001
your result array Length will be array1.Length * array2.Length
2d arrays
You could also put an array inside an array like this:
$array1 = array(1,2);
$array2 = array(3,4);
$result = array();
foreach ($array1 as $item1){
foreach($array2 as $item2){
$result[] = array($item1, $item2);
}
}
//$result[0][0] = 1 -- $result[0][1] = 3
//$result[1][0] = 1 -- $result[1][1] = 4
//$result[2][0] = 2 -- $result[2][1] = 3
//$result[3][0] = 2 -- $result[3][1] = 4
We call this a 2d (2 dimensional) array, because you could grapicly display this as a grid, like displayed here above. If you would put an Array, inside an Array inside an Array, you would call this a 3 dimensional array, etc.
print_r($result); in php:
Array
(
[0] => Array
(
[0] => 1
[1] => 3
)
[1] => Array
(
[0] => 1
[1] => 4
)
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 2
[1] => 4
)
)
try
$a= array ('0' => 1,'1' => 2);
$b= array ('0' => 3,'1' => 4);
for($i=0; $i<count($a); $i++) {
for($j=0; $j<count($b); $j++) {
$newarr[]= $a[$i].','.$b[$j];
}
}
print_r($newarr);//Array ( [0] => 1,3 [1] => 1,4 [2] => 2,3 [3] => 2,4 )
$a=array('1','2');
$b=array('3','4');
$res=array();
for($i=0;$i<count($a);$i++)
{
foreach($b as $bb)
{
$res[]=strval($a[$i].','.$bb);
}
}
print_r($res);//output=Array ( [0] => 1,3 [1] => 1,4 [2] => 2,3 [3] => 2,4 )
I have 2 arrays: Array1 and Array2. As you can see in Array1 I have 2 duplicate values. So what I want to do, is unset one of dublicates (doesn't matter which one) and as a result I need to unset value from Array2 with the same key as already unset value in Array1
Array1
(
[0] => 1331-14-2-45
[1] => 1344-1-4-22
**[2] => 1409-1-1-4**
[4] => 1312-14-1-23
**[5] => 1409-1-1-4**
[6] => 1365-10-3-30
)
AND
Array2
(
[0] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[1] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[2] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[4] => deviceNotActive#nemodel.GPON.4.6
[5] => deviceNotActive#nemodel.GPON.4.6
[6] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
)
<?php
$array1 = array
(
0 => '1331-14-2-45',
1 => '1344-1-4-22',
2 => '1409-1-1-4',
4 => '1312-14-1-23',
5 => '1409-1-1-4',
6 => '1365-10-3-30',
);
$array1_tmp = $array1;
$array2 = array
(
0 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
1 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
2 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
4 => 'deviceNotActive#nemodel.GPON.4.6',
5 => 'deviceNotActive#nemodel.GPON.4.6',
6 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
);
$array1 = array_unique($array1);
$remove_keys = array_keys(array_diff_key($array1_tmp, $array1));
foreach($remove_keys as $k => $v) {
unset($array2[$v]);
}
You can use the array_unique() function, and maybe take a look at the Array functions list.
Use array_unique function and array_diff
$uniqueValues = array_unique($inputArray); //your first array
$deletedValues = array_diff($inputArray, $uniqueValues);
foreach($deletedValues as $key => $deletedValue){
unset($secondIput[$key]); //you second array
}