Get the unique array from an array with the lowest number - php

im having trouble figuring this one out, the scenario is, that there is an multidimensional array, in which there is name, id, and number in an array, what i want is to get the unique array in which array having same name should not appear, this can be done and i had done it, but i also want that the array that is returned should contain the lowest num, i hope this would help make you understand
i have
Array(
[0]=>array(
[id]=>1
[name]=>abc
[num]=>4)
[1]=>
array(
[id]=>2
[name]=>efg
[num]=>4)
[2]=>array(
[id]=>3
[name]=>abc
[num]=>2)
)
Now its a rough array representation, what i want from this is
Array(
[0]=>array(
[id]=>3
[name]=>abc
[num]=>2)
[1]=>
array(
[id]=>2
[name]=>efg
[num]=>4)
What im using:
code.php
<?php
$details = array(
0 => array("id"=>"1", "name"=>"Mike", "num"=>"9876543210"),
1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"),
2 => array("id"=>"1", "name"=>"Mathew", "num"=>"784581254"),
);
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
$details = unique_multidim_array($details,'name');
?>
This function returns me
Array(
[0]=>array(
[id]=>1
[name]=>abc
[num]=4)
[1]=>
array(
[id]=>2
[name]=>efg
[num]=>4)

You can use array_reduce:
$result = array_reduce
(
$array,
function( $carry, $item )
{
if( !isset( $carry[$item['name']] ) || $item['num'] < $carry[$item['name']]['num'] )
{
$carry[$item['name']] = $item;
}
return $carry;
},
[] // This parameter (empty array) is optional in this case
);
$result = array_values( $result );
We compare each array element with constructing returned array (initially empty): if in returned array doesn't exists an item with key = $item['name'], we add it; otherwise, if current item has num value lower than corresponding returned array item, we replace this item with current item. At the end, we use array_values to remove associative keys.
Result:
Array
(
[0] => Array
(
[id] => 3
[name] => abc
[num] => 2
)
[1] => Array
(
[id] => 2
[name] => efg
[num] => 4
)
)
Read more about array_reduce
Read more about array_values

I would do this with two loops
First, group the values by name, then number
foreach ($your_array as $value) {
$names[$value['name']][$value['num']] = $value;
}
Then iterate the result of that, sort name by key (num) and append the first element to the final result.
foreach ($names as $set_of_nums) {
ksort($set_of_nums);
$result[] = reset($set_of_nums);
}

I have considered the below example.
$details = array(
0 => array("id"=>"1", "name"=>"Mike", "num"=>"123"),
1 => array("id"=>"2", "name"=>"Carissa", "num"=>"235"),
2 => array("id"=>"3", "name"=>"Mike", "num"=>"5"),
3 => array("id"=>"4", "name"=>"Tom", "num"=>"256"),
4 => array("id"=>"5", "name"=>"Tom", "num"=>"500"),
5 => array("id"=>"6", "name"=>"Mike", "num"=>"7"),
);
function unique_multidim_array($array, $key) {
$temp_array = array();
$key_array = array();
foreach($array as $k=>$val) {
if (!in_array($val[$key], $key_array)) {
// storing the array with the key
$key_array[$k] = $val[$key];
$temp_array[$k] = $val;
} else{
foreach($key_array as $r=>$p){
//check for the array with the name
if($temp_array[$r]['name'] == $val['name']){
// compare the value
if($temp_array[$r]['num']>$val['num']){
// store the new array to the temp_array with same key of key_array
$temp_array[$r] = $val;
}
}
}
}
}
return $temp_array;
}
$details = unique_multidim_array($details,'name');
Output:
Array
(
[0] => Array
(
[id] => 3
[name] => Mike
[num] => 5
)
[1] => Array
(
[id] => 2
[name] => Carissa
[num] => 235
)
[3] => Array
(
[id] => 4
[name] => Tom
[num] => 256
)
)
You can sort the output array using any of the sort functions as you desire.

Related

Combining Arrays while merging the values with the same key

I have two arrays with same amount of values. I need to combine them ( array1 value to key, array2 value as value) without losing the values of the second array due to duplicate key. when I use combine_array() as expected it just gets the last value of the second array with the same key.
Array
(
[0] => 1
[1] => 2
[2] => 2
[3] => 3
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Desired result
Array
(
[1] => 1
[2] => Array(
[0]=>2
[1]=>3
)
[3] => 2
)
This code will meet your request
$array1 = array("0"=>1,"1"=>2,"2"=>2,"3"=>3);
$array2 = array("0"=>1,"1"=>2,"2"=>3,"3"=>4);
$array = array();
foreach($array1 as $key => $value){
if($value != $array2[$key]){
$array[$key][] = $value;
$array[$key][] = $array2[$key];
}else{
$array[$key] = $value;
}
}
print_r($array);
The desired result is
Array
(
[0] => 1
[1] => 2
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 3
[1] => 4
)
)
I'm sure there are way better solutions than this, but it does the job for now. I would appreciate if someone can send a better written solution.
$combined = array();
$tempAr = array();
$firstMatch = array();
$count = 0;
foreach ($array1 as $index => $key) {
if (array_key_exists($key, $combined)) {
$tempAr[] = $array2[$index];
$count++;
} else {
$totalCount = $count;
}
if (!array_key_exists($key, $firstMatch)) {
$firstMatch[$key] = $array2[$index];
}
$output = array_slice($tempAr, $totalCount);
$combined[$key] = $output;
}
$combined = array_merge_recursive($firstMatch, $combined);

Sort array values based on parent/child relationship

I am trying to sort an array to ensure that the parent of any item always exists before it in the array. For example:
Array
(
[0] => Array
(
[0] => 207306
[1] => Bob
[2] =>
)
[1] => Array
(
[0] => 199730
[1] => Sam
[2] => 199714
)
[2] => Array
(
[0] => 199728
[1] => Simon
[2] => 207306
)
[3] => Array
(
[0] => 199714
[1] => John
[2] => 207306
)
[4] => Array
(
[0] => 199716
[1] => Tom
[2] => 199718
)
[5] => Array
(
[0] => 199718
[1] => Phillip
[2] => 207306
)
[6] => Array
(
[0] => 199720
[1] => James
[2] => 207306
)
)
In the above array this "fails" as [1][2] (Sam) does not yet exist and nor does [4][2] (Tom).
The correct output would be as, in this case, as both Sam and Tom's parents already exist before they appear in the array:
Array
(
[0] => Array
(
[0] => 207306
[1] => Bob
[2] =>
)
[1] => Array
(
[0] => 199714
[1] => John
[2] => 207306
)
[2] => Array
(
[0] => 199730
[1] => Sam
[2] => 199714
)
[3] => Array
(
[0] => 199728
[1] => Simon
[2] => 207306
)
[4] => Array
(
[0] => 199718
[1] => Phillip
[2] => 207306
)
[5] => Array
(
[0] => 199716
[1] => Tom
[2] => 199718
)
[6] => Array
(
[0] => 199720
[1] => James
[2] => 207306
)
)
I found an answer https://stackoverflow.com/a/12961400/1278201 which was very close but it only seems to go one level deep (i.e. there is only ever one parent) whereas in my case there could be 1 or 10 levels deep in the hierarchy.
How do I sort the array so no value can appear unless its parent already exists before it?
This will trivially order the array (in O(n)) putting first all those with no parent, then these whose parent is already in the array, iteratively, until there's no children having the current element as parent.
# map the children by parent
$parents = ['' => []];
foreach ($array as $val) {
$parents[$val[2]][] = $val;
}
# start with those with no parent
$sorted = $parents[''];
# add the children the current nodes are parent of until the array is empty
foreach ($sorted as &$val) {
if (isset($parents[$val[0]])) {
foreach ($parents[$val[0]] as $next) {
$sorted[] = $next;
}
}
}
This code requires PHP 7, it may not work in some cases under PHP 5. - for PHP 5 compatibility you will have to swap the foreach ($sorted as &$val) with for ($val = reset($sorted); $val; $val = next($sorted)):
# a bit slower loop which works in all versions
for ($val = reset($sorted); $val; $val = next($sorted)) {
if (isset($parents[$val[0]])) {
foreach ($parents[$val[0]] as $next) {
$sorted[] = $next;
}
}
}
Live demo: https://3v4l.org/Uk6Gs
I have two different version for you.
a) Using a "walk the tree" approach with recursion and references to minimize memory consumption
$data = [
[207306,'Bob',''], [199730,'Sam',199714],
[199728,'Simon',207306], [199714,'John',207306],
[199716, 'Tom',199718], [199718,'Phillip',207306],
[199720,'James',207306]
];
$list = [];
generateList($data, '', $list);
var_dump($list);
function generateList($data, $id, &$list) {
foreach($data as $d) {
if($d[2] == $id) {
$list[] = $d; // Child found, add it to list
generateList($data, $d[0], $list); // Now search for childs of this child
}
}
}
b) Using phps built in uusort()function (seems only to work up to php 5.x and not with php7+)
$data = [
[207306,'Bob',''], [199730,'Sam',199714],
[199728,'Simon',207306], [199714,'John',207306],
[199716, 'Tom',199718], [199718,'Phillip',207306],
[199720,'James',207306]
];
usort($data, 'cmp');
var_dump($data);
function cmp($a, $b) {
if($a[2] == '' || $a[0] == $b[2]) return -1; //$a is root element or $b is child of $a
if($b[2] == '' || $b[0] == $a[2]) return 1; //$b is root element or $a is child of $b
return 0; // both elements have no direct relation
}
I checked this works in PHP 5.6 and PHP 7
Sample array:
$array = Array(0 => Array(
0 => 207306,
1 => 'Bob',
2 => '',
),
1 => Array
(
0 => 199730,
1 => 'Sam',
2 => 199714,
),
2 => Array
(
0 => 199728,
1 => 'Simon',
2 => 207306,
),
3 => Array
(
0 => 199714,
1 => 'John',
2 => 207306,
),
4 => Array
(
0 => 199716,
1 => 'Tom',
2 => 199718,
),
5 => Array
(
0 => 199718,
1 => 'Phillip',
2 => 207306,
),
6 => Array
(
0 => 199720,
1 => 'James',
2 => 207306,
),
);
echo "<pre>";
$emp = array();
//form the array with parent and child
foreach ($array as $val) {
$manager = ($val[2] == '') ? 0 : $val[2];
$exist = array_search_key($val[2], $emp);
if ($exist)
$emp[$exist[0]][$val[0]] = $val;
else
//print_R(array_search_key(199714,$emp));
$emp[$manager][$val[0]] = $val;
}
$u_emp = $emp[0];
unset($emp[0]);
//associate the correct child/emp after the manager
foreach ($emp as $k => $val) {
$exist = array_search_key($k, $u_emp);
$pos = array_search($k, array_keys($u_emp));
$u_emp = array_slice($u_emp, 0, $pos+1, true) +
$val +
array_slice($u_emp, $pos-1, count($u_emp) - 1, true);
}
print_R($u_emp); //print the final result
// key search function from the array
function array_search_key($needle_key, $array, $parent = array())
{
foreach ($array AS $key => $value) {
$parent = array();
if ($key == $needle_key)
return $parent;
if (is_array($value)) {
array_push($parent, $key);
if (($result = array_search_key($needle_key, $value, $parent)) !== false)
return $parent;
}
}
return false;
}
Find the below code that might be helpful.So, your output is stored in $sortedarray.
$a=array(array(207306,'Bob',''),
array (199730,'Sam',199714),
array(199728,'Simon',207306),
array(199714,'John',207306),
array(199716,'Tom',199718),
array(199718,'Phillip',207306),
array(199720,'James',207306));
$sortedarray=$a;
foreach($a as $key=>$value){
$checkvalue=$value[2];
$checkkey=$key;
foreach($a as $key2=>$value2){
if($key<$key2){
if ($value2[0]===$checkvalue){
$sortedarray[$key]=$value2;
$sortedarray[$key2]=$value;
}else{
}
}
}
}
print_r($sortedarray);
What about this approach:
Create an empty array result.
Loop over your array and only take the items out of it where [2] is empty and insert them into result.
When this Loop is done you use a foreach-Loop inside a while-loop. With the foreach-Loop you take every item out of your array where [2] is already part of result. And you do this as long as your array contains anything.
$result = array();
$result[''] = 'root';
while(!empty($yourArray)){
foreach($yourArray as $i=>$value){
if(isset($result[$value[2]])){
// use the next line only to show old order
$value['oldIndex'] = $i;
$result[$value[0]] = $value;
unset($yourArray[$i]);
}
}
}
unset($result['']);
PS: You may run into trouble by removing parts of an array while walking over it. If you do so ... try to solve this :)
PPS: Think about a break condition if your array have an unsolved loop or a child without an parent.
you can use your array in variable $arr and use this code it will give you required output.
function check($a, $b) {
return ($a[0] == $b[2]) ? -1 : 1;
}
uasort($arr, 'check');
echo '<pre>';
print_r(array_values($arr));
echo '</pre>';

select data from array that matches condition in another array in php

i have 2 arrays i want to display the final array as what are the array element in $displayArray only be displayed from the $firstArray
$firstArray = Array
(
[0] => Array
(
[Dis_id] => Dl-Dis1
[Dis_Desc] => Discount
[Dis_Per] => 7.500
[Dis_val] => 26.25
)
[1] => Array
(
[Dis_id] => Dl-Dis2
[Dis_Desc] => Discount
[Dis_Per] => 2.500
[Dis_val] => 8.13
)
)
$displayArray = Array
(
[0] => Array
(
[0] => Dis_id
[1] => Dis_val
)
)
i want the final output will be
$resultArray = Array
(
[0] => Array
(
[Dis_id] => Dl-Dis1
[Dis_val] => 26.25
)
[1] => Array
(
[Dis_id] => Dl-Dis2
[Dis_val] => 8.13
)
)
Both the $firstArray and the $DisplayArray are dynamic but the $displayArray should be one.
i dont know how to do give me any suggestion
First up, if $displayArray will never have more than one array, the answer is pretty simple. Start by popping the inner array, to get to the actual keys you will need:
$displayArray = array_pop($displayArray);//get keys
$resultArray = array();//this is the output array
foreach ($firstArray as $data)
{
$item = array();
foreach ($displayArray as $key)
$item[$key] = isset($data[$key]) ? $data[$key] : null;//make sure the key exists!
$resultArray[] = $item;
}
var_dump($resultArray);
This gives you what you need.
However, if $displayArray contains more than 1 sub-array, you'll need an additional loop
$resultArray = array();
foreach ($displayArray as $k => $keys)
{
$resultArray[$k] = array();//array for this particular sub-array
foreach ($firstArray as $data)
{
$item = array();
foreach ($keys as $key)
$item[$key] = isset($data[$key]) ? $data[$key] : null;
$resultArray[$k][] = $item;//add data-item
}
}
var_dump($resultArray);
the latter version can handle a display array like:
$displayArray = array(
array(
'Dis_id',
'Dis_val'
),
array(
'Dis_id',
'Dis_desc'
)
);
And it'll churn out a $resultArray that looks like this:
array(
array(
array(
'Dis_id' => 'foo',
'Dis_val' => 123
)
),
array(
array(
'Dis_id' => 'foo',
'Dis_desc' => 'foobar'
)
)
)
Job done

Nested array to flat with parent_id creating

I want to create flat array from nested array, like this one:
[0]=>Array(
"id"=>1,
"positions">Array(
[0]=>Array(
"id"=>2
),
[1]=>Array(
"id"=>3
"positions"=>Array(
[0]=>Array(
"id"=>4
)
)
)
to something like this:
[0]=>Array(
"id"=>1,
"parent_id"=>0
),
[1]=>Array(
"id"=>2,
"parent_id"=>1
),
[2]=>Array(
"id"=>3,
"parent_id"=>1
),
[3]=>Array(
"id"=>4,
"parent_id"=>3
)
I don't have parent_id in nested structure, so all the trick is to "ride" trough the nested array, and add 'parent_id', base on id from parent node. I know how to flat the array, but I need parent_id information.
Try below code : i hope useful it...
<?php
$array = array(array(
"id"=>1,
"positions" =>
array(
array(
"id"=>2
),
array(
"id"=>3,
"positions"=>
array(
array(
"id"=>4
)
)
)
)
));
echo "<pre>";
print_r(getArray($array));
echo "</pre>";
exit;
function getArray($array,$parent_id = 0)
{
$result = array();
foreach ($array as $value)
{
$tmp = array();
$tmp['id'] = $value['id'];
$tmp['parent_id'] = $parent_id;
$result[] = $tmp;
if(!empty($value['positions']))
{
$result= array_merge($result,getArray($value['positions'],$value['id']));
}
}
return $result;
}
?>
OUTPUT :
Array
(
[0] => Array
(
[id] => 1
[parent_id] => 0
)
[1] => Array
(
[id] => 2
[parent_id] => 1
)
[2] => Array
(
[id] => 3
[parent_id] => 1
)
[3] => Array
(
[id] => 4
[parent_id] => 3
)
)
Use this code
$result = array();
function generateArray($array,$parent=0){
foreach ($array as $key=>$val){
$tmp = array();
if(!empty($val['id'])){
$tmp['id'] = $val['id'];
$tmp['parent_id'] = $parent;
$result[] = $tmp;
}
if(!empty($val['positions'])){
$result=array_merge($result,generateArray($val['positions'],$val['id']));
}
}
return $result;
}
Your array must be of this structure
$data = array(0=>array("id"=>1,"positions"=>array(0=>array("id"=>2),1=>array("id"=>3,"positions"=>array(0=>array("id"=>4))))));
Then call the function generateArray(),
var_dump(generateArray($data));

Group rows by one column, only create deeper subarrays when multiple rows in group

I have a multi-dimensional array like this:
Array
(
[0] => Array
(
[id] => 1
[email_id] => ok#gmail.com
[password] => test
)
[1] => Array
(
[id] => 2
[email_id] => check#gmail.com
[password] => test
)
[2] => Array
(
[id] => 3
[email_id] => an#gmail.com
[password] => pass
)
)
In the above array, password value is the same in two different rows. I need to merge the arrays which have duplicate values to get the following output:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[email_id] => ok#gmail.com
[password] => test
)
[1] => Array
(
[id] => 2
[email_id] => check#gmail.com
[password] => test
)
)
[1] => Array
(
[id] => 3
[email_id] => an#gmail.com
[password] => pass
)
)
How to do this? I've tried array_merge() & foreach() loops, but I can't get this output.
Try,
$arr = array( array('id'=>1, 'email_id'=>'ok#gmail.com', 'password'=>'test'),
array('id'=>2, 'email_id'=>'check#gmail.com', 'password'=>'test'),
array('id'=>3, 'email_id'=>'an#gmail.com', 'password'=>'pass'));
$new_arr = array();
foreach($arr as $k => $v) {
if( is_array($arr[$k+1]) && $arr[$k]['password'] === $arr[$k + 1]['password'] )
$new_arr[] = array($arr[$k], $arr[$k+1]);
else if( in_array_recursive($arr[$k]['password'], $new_arr) === FALSE )
$new_arr[] = $v;
}
function in_array_recursive( $val, $arr) {
foreach( $arr as $v ) {
foreach($v as $m) {
if( in_array($val, $m ) )
return TRUE;
}
}
return FALSE;
}
print_r($new_arr);
Demo
You only want to create more depth in your output array if there is more than one entry in the group. I personally wouldn't want that kind of variability in a data structure because the code that will print the data will need to go to extra trouble to handle associative rows of data that might be on different levels.
Anyhow, here's how I would do it with one loop...
Foreach Loop Code: (Demo)
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['password']])) {
$result[$row['password']] = $row; // save shallow
} else {
if (isset($result[$row['password']]['id'])) { // if not deep
$result[$row['password']] = [$result[$row['password']]]; // make original deeper
}
$result[$row['password']][] = $row; // save deep
}
}
var_export(array_values($result)); // print without temporary keys
Functional Code: (Demo)
var_export(
array_values(
array_reduce(
$array,
function($result, $row) {
if (!isset($result[$row['password']])) {
$result[$row['password']] = $row;
} else {
if (isset($result[$row['password']]['id'])) {
$result[$row['password']] = [$result[$row['password']]];
}
$result[$row['password']][] = $row;
}
return $result;
},
[]
)
)
);

Categories