I'm currently looking for a solution to this problem:
if I have something like that:
Array
(
[0] => Array
(
[id] => 1
[name] => Timer
)
[1] => Array
(
[id] => 2
[name] => Tub
)
[3] => Array
(
[id] => 1
[name] => Paper
)
[4] => Array
(
[id] => 4
[name] => Puppy
)
)
The goal I'd like to reach here is create a new array which contains the arrays with the same ID. So basically, at the end I'm gonna have two arrays: the first will contain element with different IDs and the second will contain element with the same ID.
Any tips? Thank you in advance!
One way is by mainly using array_filter()
// Gather ids and count
$id = array_count_values(array_column($array, 'id'));
// Filter not unique
$notUnique = array_filter($array, function($e) use ($id) {
return ($id[$e['id']] > 1);
});
// Filter unique
$unique = array_filter($array, function($e) use ($id) {
return !($id[$e['id']] > 1); // or ($id[$e['id']] == 1)
});
// Print result
print_r($notUnique);
echo '<br>';
print_r($unique);
Try this
<?php
$array = array(
array('id' => 1,'name' => 'Timer'),
array('id' => 2,'name' => 'Tub'),
array('id' => 1,'name' => 'Paper'),
array('id' => 4,'name' => 'Puppy')
);
$new = array();
foreach($array as $r)$new[$r['id']][] = $r['name'];
echo '<pre>';print_r($new);
?>
Output
Array
(
[1] => Array
(
[0] => Timer
[1] => Paper
)
[2] => Array
(
[0] => Tub
)
[4] => Array
(
[0] => Puppy
)
)
Related
I've an php array that I've got from Excel file, for example:
$arrayOne
Array
{
[0] => Array
{
[0] => 0_age
[1] => 1_academic id
[2] => 2_name
[3] => 3_sex
}
[1] => Array
{
[0] => 0_18
[1] => 1_110291
[2] => 2_Jason
[3] => 3_Male
}
}
and in the mid of proccess, the array value from index [0] that consist data from Excel Header set into duallistbox for elimination and sorting then set into new array $newArray. So then, I got this array result:
$newArray
Array
{
[0] => Array
{
[0] => 2_name
[1] => 1_academic id
[2] => 3_sex
}
}
And I expect the system also can eliminate and sorting the data of array from index [1] that consist of Excel Data.
So the expected result is like this:
$expectedArray
Array
{
[0] => Array
{
[0] => 2_Jason
[1] => 1_110291
[2] => 3_Male
}
}
anyone know idea or how to solve this case? I've add an id(e.g: 0_) in each of array value that maybe useful for sorting.
Thanks
Edited
The sorting is based on the id that have been set on each value of array, so each element on array index [1] from $arrayOne is reset into new sequence adapted to same id from $newArray.
One way
<?php
$arr = array(
array
(
"0_age",
"1_academic id",
"2_name",
"3_sex",
),
array
(
"0_18",
"1_110291",
"2_Jason",
"3_Male",
),
array
(
"0_28",
"1_110291111",
"2_Jason Second",
"3_Female",
)
);
?>
<pre>
<?php
$new_array = array();
$count = 0;
foreach($arr as $a){
if($count==0){
}else{
$new_array[] = array(
$arr[0][0] => $a[0],
$arr[0][1] => $a[1],
$arr[0][2] => $a[2],
$arr[0][3] => $a[3],
);
}
$count=$count+1;
}
print_r($new_array);
?>
</pre>
Output
Array
(
[0] => Array
(
[0_age] => 0_18
[1_academic id] => 1_110291
[2_name] => 2_Jason
[3_sex] => 3_Male
)
[1] => Array
(
[0_age] => 0_28
[1_academic id] => 1_110291111
[2_name] => 2_Jason Second
[3_sex] => 3_Female
)
)
Second Way that exactly match with your output
$new_array = array();
$count = 0;
foreach($arr as $a){
if($count==0){
}else{
$new_array[] = array(
//$a[0],
$a[2],
$a[1],
$a[3],
);
}
$count=$count+1;
}
print_r($new_array);
Output:
Array
(
[0] => Array
(
[0] => 0_18
[1] => 1_110291
[2] => 2_Jason
[3] => 3_Male
)
[1] => Array
(
[0] => 0_28
[1] => 1_110291111
[2] => 2_Jason Second
[3] => 3_Female
)
)
You can search, Sort and remove any field. Update on the basis of your requirement.
I have array like this i need to count by array values
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ) [MSN] => Array ( [0] => Dozen ) [NeK] => Array ( [0] => Suhan [1] => Ebao ) [NetSE] => Array ( [0] => SuZhan [1] => Guhang ) )
For example
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ))
In the Cop key i have two different values for cop so i need cop should be 2
Cop - 2
MSn - 1
NeK - 2
NetSE - 2
I need the count like above how can i do this ?
Try simply using array_map,count,& array_unique like as
array_map(function($v) {
return count(array_unique($v));
}, $arr);
Use array_unique() and then count.
count(array_unique($array['Cop']));// output 2
If you want to print for every key do following:
$array = array('Cop'=>array('Dozen','Dozen','Akls','Akls'), 'MSN'=> array('Dozen'), 'NeK'=> array('Suhan','Ebao'));
foreach($array as $key => &$value) {
$value = count(array_unique($array[$key]));
}
print_r($array);
Output:
Cop = 2
MSN = 1
NeK = 2
You should use array_count_values() for that, here's an example:
$data = array('cop' => array(0 => 'test', 1 => 'test', 2 => 'test2'));
foreach($data as $item){
$result = array_count_values($item);
print_r($result);
}
Outputs:
Array
(
[test] => 2
[test2] => 1
)
I want to perform an intersection of two arrays that have different structures, but both have one key common (fid). I want a new (filtered second) array after intersection with first array. below is my code and two arrays :
first array:
Array
(
[0] => Array
(
[fid] => 1
)
[1] => Array
(
[fid] => 3
)
)
Second array:
Array
(
[0] => Array
(
[fid] => 9
[functionality] => testing
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[2] => Array
(
[fid] => 2
[functionality] => view functionality category
[funcat_id] => 1
[name] => functionality
)
[3] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
[4] => Array
(
[fid] => 4
[functionality] => edit functionality
[funcat_id] => 1
[name] => functionality
)
)
I want this Output :
Array
(
[0] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
)
I tried this code but I'm not getting the right answer:
$result=array_intersect($array1,$array2);
//Or this also
$result=recursive_array_intersect_key($array1,$array2);
Please let me know, if any one can do this ?
I do not know if a function does exists to do this outright, but alternatively, you can just loop them instead:
$result = array();
foreach($array2 as $val2) {
foreach ($array1 as $val1) {
if($val2['fid'] == $val1['fid']) {
$result[] = $val2;
}
}
}
echo '<pre>';
print_r($result);
Sample Output
Or if you're using PHP 5.5 or greater:
$val1 = array_column($array1, 'fid');
$result = array_filter($array2, function($val2) use($val1) {
return in_array($val2['fid'], $val1);
});
foreach($array2 as $val)
{
$i=0;
foreach($array1 as $val1)
{
if($val['fid']==$val1['fid'])
{
$i++;
}
}
if($i!=0)
{
$a[]=$val;
}
}
print_r($a);
I'm getting stack-removing duplicated keys and assigning them to a new array.
My array:
array (
[1] => Array
(
[name] => name1
[actions] => add
)
[2] => Array
(
[name] => name1
[actions] => remove
)
[3] => Array
(
[name] => name2
[actions] => dosomething1
)
[4] => Array
(
[name] => name2
[actions] => dosomething1
)
)
What I am trying to achieve:
array (
[1] => Array
(
[name] => name1
[actions] => add
[actions] => remove
)
[2] => Array
(
[name] => name2
[actions] => dosomething1
[actions] => dosomething1
)
)
What i have tried:
public function array_unique_multidimensional($input)
{
$serialized = array_map('serialize', $input);
$unique = array_unique($serialized);
return array_intersect_key($input, $unique);
}
It is incorrectly returning the same array. Any help would be appreciated.
You cannot have two array keys with the save values (so two actions elements for a given element would not be possible) What you can do is have a single action element with multiple values in it.
$results = array();
foreach ($array as $v){
if (!isset($results[$v["name"]]){
$results[$v["name"]] = array("name"=>$v["name"], "actions"=>array($v["actions"]));
} else {
$results[$v["name"]]["actions"][] = $v["actions"];
}
}
if you want to remove the string keys on the top level array you can then do.
$results = array_values($results);
I have an array contain this data
Array
(
[id] => Array
(
[0] => 1
[1] => 10
[2] => 4
)
[age] => Array
(
[0] => 1
[1] => 1
[2] => 2
)
)
Now I want to remove duplicates from the ['age'] and leave the first one in tact.
So this would return
Array
(
[id] => Array
(
[0] => 1
[2] => 4
)
[age] => Array
(
[0] => 1
[2] => 2
)
)
Any ideas? Or is there a function already in place to do this?
Like Gordon said, you'd need a custom function to make the relationship but you can use http://php.net/manual/en/function.array-unique.php ?
Wouldn't it be better to have the keys of the age array the corresponding values of the id array?
<?php
$array = array(
'id' => array(0 => 1, 1 => 10, 3 => 4),
'age' => array(0 => 1, 1 => 1, 2 => 2)
);
array_walk($array, 'dupe_killer');
print_r($array);
function dupe_killer(&$value, $key)
{
$value = array_unique($value);
}
?>
You could try this
$array = array('id' => array(1,10,4), 'age'=>array(1,1,2));
$age_array = array();
foreach ($array['age'] as $key => $val) {
if (in_array($val, $age_array))
unset($array['id'][$key], $array['age'][$key]);
$age_array[] = $val;
}
print_r($array);
this returns Array ( [id] => Array ( [0] => 1 [2] => 4 ) [age] => Array ( [0] => 1 [2] => 2 ) )
Regards
Luke