Finding duplicate values in a multidimensional array for Search Method - php

My Code was :
$data = array();
foreach ($table as $key => $var) {
$data[] = ['id' => $var->id, 'value' => $var->designation];
}
My Data array should be like this
array (
0 => array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 22,
'value' => 'laravel developer',
),
3 => array (
'id' => 23,
'value' => 'laravel casts',
),
4 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
I need only one value i tried all the php core library function output:
array (
0 =>
array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 23,
'value' => 'laravel casts',
),
3 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
Based on the name only i need to remove duplicate bacause in my search bar it shows repeated mode.

You can use array_unique, wich saves indexes, and get the result by array_intersect_key
$temp = array_unique(array_column($arr, 'value'));
$res = array_intersect_key($arr, $temp);
print_r($res);

Related

Overwrite rows in one array using another array when two columns are shared between the arrays

I got 2 arrays. In first one field number is empty:
array (
3 => array ( 0 => array ( 'id' => 1, 'number' => 0, 'time' => 40,), ),
4 => array ( 0 => array ( 'id' => 2, 'number' => 0, 'time' => 40, ), ),
5 => array ( 0 => array ( 'id' => 3, 'number' => 0, 'time' => 40, ), ),
6 => array ( 0 => array ( 'id' => 1, 'number' => 0, 'time' => 41, ), ),
7 => array ( 0 => array ( 'id' => 2, 'number' => 0, 'time' => 41, ), ),
8 => array ( 0 => array ( 'id' => 3, 'number' => 0, 'time' => 41, ), ),
)
In the second one fields number are not empty, however the array is bit different, because it doesn't have a 2 arrays in the middle (id = 3, time = 40 and id = 3, time = 41).
array ( 3 => array ( 'id' => '1', 'number' => '3785', 'time' => '40', ),
4 => array ( 'id' => '2', 'number' => '1574', 'time' => '40', ),
5 => array ( 'id' => '1', 'number' => '2954', 'time' => '41', ),
6 => array ( 'id' => '2', 'number' => '2463', 'time' => '41', ),
)
What I want to do is to some sort of merge of this arrays into one looking like this:
array (
3 => array ( 'id' => '1', 'number' => '3785', 'time' => '40', ),
4 => array ( 'id' => '2', 'number' => '1574', 'time' => '40', ),
5 => array ( 0 => array ( 'id' => 3, 'number' => 0, 'time' => 40, ), ),
6 => array ( 'id' => '1', 'number' => '2954', 'time' => '41', ),
7 => array ( 'id' => '2', 'number' => '2463', 'time' => '41', ),
8 => array ( 0 => array ( 'id' => 3, 'number' => 0, 'time' => 41,), ),
)
I tried some variations of array_merge() and array_combine(), but neither of them seems to work, or perhaps I've been using them badly.
Again, notice how row 5 and 8 are not affected because (id = 3 and time = 40) and (id = 3 and time = 41), respectively, are not represented in the second array.
What I tried and it works, but it doesn't look well in my opinion:
foreach($arr2 as $wpis2){
foreach($arr as $key=>$wpis){
if($wpis2['id'] == $wpis[0]['id'] && $wpis2['time'] == $wpis[0]['time']){
$arr[$key] = $wpis2;
}
}
}
You will need to iterate over the first array in order to populate a key-map based on the combination of id and time values from all rows.
Use a second loop to iterate over the second array and access the correct key to overwrite by leveraging the mapping array.
Code: (Demo)
$map = [];
foreach ($first as $k => [['id' => $id, 'time' => $time]]) {
$map["{$id}_{$time}"] = $k;
}
foreach ($second as $row) {
$key = $map["{$row['id']}_{$row['time']}"];
$first[$key] = $row;
}
var_export($first);

Nested array, replace alphabetical keys with numeric and keep structure

I have a nested array who is 4 levels deep.
I would like to replace the keys by numerical ones but not the meta level, this array should keep the alphabetical keys.
The array will have a lot of values and there will be more accounts like Marco Mueller, this is just a short example. It is important that it will keep the same nested structure, after its replaced by numerical keys.
$array = array (
'Marco Mueller' =>
array (
'meta' =>
array (
'accountName' => 'Marco Mueller',
'accountId' => '1',
'sum' => '3,659.06',
),
'Conrad, Ute' =>
array (
'meta' =>
array (
'customerName' => 'Conrad, Ute',
'customerId' => '8391',
'sum' => '457.59',
),
'Fidor Bank' =>
array (
'meta' =>
array (
'bankName' => 'Fidor Bank',
'bankKey' => 'FID',
'sum' => '457.59',
),
'H1-2019' =>
array (
'meta' =>
array (
'periodName' => 'H1-2019',
'periodId' => '5',
'sum' => '457.59',
),
),
),
),
),
);
The result should be
$array = array (
array (
'meta' =>
array (
'accountName' => 'Marco Mueller',
'accountId' => '1',
'sum' => '3,659.06',
),
'items' => array (
'meta' =>
array (
'customerName' => 'Conrad, Ute',
'customerId' => '8391',
'sum' => '457.59',
),
'items' =>
array (
'meta' =>
array (
'bankName' => 'Fidor Bank',
'bankKey' => 'FID',
'sum' => '457.59',
),
'items' => array (
'meta' =>
array (
'periodName' => 'H1-2019',
'periodId' => '5',
'sum' => '457.59',
),
),
),
array (
'meta' =>
array (
'periodName' => 'H2-2019',
'periodId' => '6',
'sum' => '600',
),
),
),
),
),
);
I solved my problem with this function, if anyone has an idea how to make this more elegant and efficient, your welcome!
private function getValues(array $array)
{
$newArray = [];
foreach ($array as $item) {
$meta = $item['meta'];
unset($item['meta']);
$arrayValues = array_values($item);
if (count($arrayValues) > 1 || count($arrayValues[0]) > 1) {
$arrayValues = $this->getValues($arrayValues);
}
if (!$arrayValues) {
$values = [
'meta' => $meta,
];
} else {
$values = [
'meta' => $meta,
'items' => $arrayValues,
];
}
$newArray[] = $values;
}
return $newArray;
}

convert indexed multidimensional array to associative multidimensional array

I have an indexed array with nested categories like this:
array (
0 =>
array (
'name' => 'Furniture',
'id' => 'b3cdd1k',
'content' =>
array (
0 =>
array (
'name' => 'Tables',
'id' => 'nw24ga3',
'content' =>
array (
0 =>
array (
'name' => 'Wooden tables',
'id' => 'ba5lgaz',
),
1 =>
array (
'name' => 'Glass tables',
'id' => 'rqt91gz',
),
),
),
),
),
1 =>
array (
'name' => 'Lamps',
'id' => 'vb1a4nf',
),
2 =>
array (
'name' => 'Doors',
'id' => 'a5l4gal',
'content' =>
array (
0 =>
array (
'name' => 'Entrance doors',
'id' => 'qwg30fb',
),
),
),
)
Is there elegant way to convert it to associative array (where keys are id's) and keep nesting structure?
After conversion I excepting something like this:
array (
'b3cdd1k' =>
array (
'name' => 'Furniture',
'content' =>
array (
'nw24ga3' =>
array (
'name' => 'Tables',
'content' =>
array (
'ba5lgaz' =>
array (
'name' => 'Wooden tables',
),
'rqt91gz' =>
array (
'name' => 'Glass tables',
),
),
),
),
),
'vb1a4nf' =>
array (
'name' => 'Lamps',
),
'a5l4gal' =>
array (
'name' => 'Doors',
'content' =>
array (
'qwg30fb' =>
array (
'name' => 'Entrance doors',
),
),
),
)
You could try this - not the most elegant, but seems to work:
function convert(&$a)
{
$result = Array();
foreach($a as $k=>&$v)
{
$result[$v['id']]['name'] = $v['name'];
if(is_array($v['content'])) $result[$v['id']]['content'] = convert($v['content']);
}
return $result;
}
if(count($array) != 0) $result = convert($array);

PHP - generate multi-dimentional array

This is my array:
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
I've to convert this array into multi-dimentional array as below:
$arr = array(
'jan-2015' => array(
0 => array(
'title' => 'test1',
'count' => 4,
),
1 => array(
'title' => 'test2',
'count' => 10,
),
),
'jun-2015' => array(
0 => array(
'title' => 'test3',
'count' => 14,
),
),
'july-2015' => array(
0 => array(
'title' => 'test4',
'count' => 45,
),
),
);
I've tried to make it as expected but unfortunately i can't make this.
Is any other solutions for this?
According to your data structure :
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
try this:
$newArray = array();
foreach($arr as $key => $val) {
$newArray[$val['month']][] = $val;
}
echo '<pre>'.print_r($newArray,1).'</pre>';
Output:
Array
(
[jan-2015] => Array
(
[0] => Array
(
[title] => test1
[count] => 4
[month] => jan-2015
)
[1] => Array
(
[title] => test2
[count] => 10
[month] => jan-2015
)
)
[jun-2015] => Array
(
[0] => Array
(
[title] => test3
[count] => 14
[month] => jun-2015
)
)
[july-2015] => Array
(
[0] => Array
(
[title] => test4
[count] => 45
[month] => july-2015
)
)
)
You could use this function:
function transform($input) {
// Extract months, and use them as keys, with value set to empty array
// The array_fill_keys also removes duilicates
$output = array_fill_keys(array_column($input, 'month'), array());
foreach ($input as $element) {
$copy = $element;
// remove the month key
unset($copy["month"]);
// assign this to the month key in the output
$output[$element["month"]][] = $copy;
}
return $output;
}
Call it like this:
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
print_r (transform($arr));
Output:
Array
(
[jan-2015] => Array
(
[0] => Array
(
[title] => test1
[count] => 4
)
[1] => Array
(
[title] => test2
[count] => 10
)
)
[jun-2015] => Array
(
[0] => Array
(
[title] => test3
[count] => 14
)
)
[july-2015] => Array
(
[0] => Array
(
[title] => test4
[count] => 45
)
)
)
By using answer of #Girish Patidar, You can achieve this by:
$outputArr = array();
$to_skip = array();
foreach($arr as $row){
$to_skip = $row;
unset($to_skip['month']);
$outputArr[$row['month']][] = $to_skip;
}
echo "<pre>";
print_r($outputArr);
die;
There could many way to do this. Please try this one if it works for you
<?php
$newArr=NULL;
foreach($arr as $array)
{
$temp=NULL;
$temp['title']=$array['title'];
$temp['count']=$array['count'];
$newArr[$array['month']][]=$temp;
}
var_dump($newArr);
?>

Using COUNT_RECURSIVE with a specified key name?

count($arr['children'], COUNT_RECURSIVE); to get the total number of keys inside the original children key, however I'm wanting to filter out all but the keys named 'children.' I'm unsure the count_recursive works like this.
$arr = array (
'id' => '81',
'parent' => NULL,
'children' =>
array (
0 =>
array (
'id' => '173',
'parent' => '81',
),
1 =>
array (
'id' => '84',
'parent' => '81',
'children' =>
array (
0 =>
array (
'id' => '85',
'parent' => '84',
'children' =>
array (
0 =>
array (
'id' => '131',
'parent' => '85',
'children' =>
array (
0 =>
array (
'id' => '176',
'parent' => '131',
),
),
),
),
),
1 =>
array (
'id' => '174',
'parent' => '84',
),
2 =>
array (
'id' => '175',
'parent' => '84',
),
),
),
),
);
echo count($arr['children'], COUNT_RECURSIVE);
I'm trying to have it return 7, but because it's counting all keys, it returns 24. How could I do this?

Categories