PHP - Array unique only using specific values - php

I have a PHP array that looks like this...
Array
(
[0] => Array
(
[id] => 1
[value] => 111
[date] => 'today'
)
[1] => Array
(
[id] => 2
[value] => 222
[date] => 'today'
)
[2] => Array
(
[id] => 3
[value] => 333
[date] => 'today'
)
[3] => Array
(
[id] => 1
[value] => 111
[date] => 'today'
)
[4] => Array
(
[id] => 5
[value] => 111
[date] => 'today'
)
)
If I use array_unique like this...
print_r(array_unique($array, SORT_REGULAR));
It removes the duplicate [3] which is correct, but I am looking for a way to ignore [id] and only match by [date] and [value] so that my output looks like this...
Array
(
[0] => Array
(
[id] => 1
[value] => 111
[date] => 'today'
)
[1] => Array
(
[id] => 2
[value] => 222
[date] => 'today'
)
[2] => Array
(
[id] => 3
[value] => 333
[date] => 'today'
)
)

array_reduce + array_values() solution:
$arr = [
['id' => 1, 'value' => 111, 'date'=> 'today'],
['id' => 2, 'value' => 222, 'date'=> 'today'],
['id' => 3, 'value' => 333, 'date'=> 'today'],
['id' => 1, 'value' => 111, 'date'=> 'today'],
['id' => 5, 'value' => 111, 'date'=> 'today']
];
$result = array_values(
array_reduce($arr, function($r, $a){
if (!isset($r[$a['value'] . $a['date']])) $r[$a['value'] . $a['date']] = $a;
return $r;
}, [])
);
print_r($result);
The output:
Array
(
[0] => Array
(
[id] => 1
[value] => 111
[date] => today
)
[1] => Array
(
[id] => 2
[value] => 222
[date] => today
)
[2] => Array
(
[id] => 3
[value] => 333
[date] => today
)
)

Iterate over your array and get a key as concatenation of 'date' and 'value' fields. If this key has already been found - skip array value:
$pairs = [];
$new_values = [];
foreach ($array as $item) {
$key = $item['date'] . $item['value'];
if (empty($pairs[$key])) {
$pairs[$key] = 1;
$new_values[] = $item;
}
}

Related

How to change indexes in array with sub arrays with subarray property value in PHP

I need help. I have an array of items like this one:
[7646] => Array
(
[0] => Array
(
[id] => 156153
[tmplvarid] => 5
[value] => 2
)
[1] => Array
(
[id] => 56795
[tmplvarid] => 7
[value] => 430
)
[2] => Array
(
[id] => 56798
[tmplvarid] => 19
[value] => rate_08
)
),
[7647] => Array ()
And I need to change array indexes to value of property tmplvarid in sub array to transform array like this:
`[7646] => Array
(
[5] => Array
(
[id] => 156153
[tmplvarid] => 5
[value] => 2
)
[7] => Array
(
[id] => 56795
[tmplvarid] => 7
[value] => 430
)
[19] => Array
(
[id] => 56798
[tmplvarid] => 19
[value] => rate_08
)
)
How can I transform it in assosiative array ?
set index value from array value using foreach loop
Code
<?PHP
$arr = [
"7646" => array
(
[
"id"=> 156153,
"tmplvarid" => 5,
"value" => 2
],
[
"id"=> 56795,
"tmplvarid" => 7,
"value" => 430
],
[
"id"=> 56798,
"tmplvarid" => 19,
"value" => "rate_08"
]
)
];
echo "<pre>";
print_r($arr);
$newarr= [];
foreach($arr as $key => $value)
{
foreach($value as $key1 => $value1)
{
$newarr[$key][$value1['tmplvarid']] = $value1;
}
}
print_r($newarr);
?>
Output
Array
(
[7646] => Array(
[5] => Array
(
[id] => 156153
[tmplvarid] => 5
[value] => 2
)
[7] => Array
(
[id] => 56795
[tmplvarid] => 7
[value] => 430
)
[19] => Array
(
[id] => 56798
[tmplvarid] => 19
[value] => rate_08
)
)
)

How to build tree structure from array in PHP

I have following table:
$arr = array(
array('id'=>100, 'year'=>2019, 'month'=>9, 'name'=>'a'),
array('id'=>101, 'year'=>2019, 'month'=>12, 'name'=>'b'),
array('id'=>102, 'year'=>2020, 'month'=>1, 'name'=>'c'),
array('id'=>103, 'year'=>2020, 'month'=>2, 'name'=>'d'),
);
With the code below
$tree = array();
foreach ($arr as $row) {
$tree[$row['year']] = array(
$row['month'] => array (
'id' => $row['id'],
'name' => $row['name'],
),
);
}
I would like to get following result - this structure as a tree:
Array
(
[2019] => Array
(
[9] => Array
(
[id] => 100
[name] => a
)
[12] => Array
(
[id] => 101
[name] => b
)
)
[2020] => Array
(
[1] => Array
(
[id] => 102
[name] => c
)
[2] => Array
(
[id] => 103
[name] => d
)
)
)
Unfortunately I get only following one with single "branches":
Array
(
[2019] => Array
(
[12] => Array
(
[id] => 101
[name] => b
)
)
[2020] => Array
(
[2] => Array
(
[id] => 103
[name] => d
)
)
)
What is missing here? why previous rows disappear from the structure?
As you want the year and month as the main indexes, you need to use both of these when adding the data into the $tree...
$tree = array();
foreach ($arr as $row) {
$tree[$row['year']][$row['month']] = [ 'id' => $row['id'],
'name' => $row['name']];
}
with your test data, this gives...
Array
(
[2019] => Array
(
[9] => Array
(
[id] => 100
[name] => a
)
[12] => Array
(
[id] => 101
[name] => b
)
)
[2020] => Array
(
[1] => Array
(
[id] => 102
[name] => c
)
[2] => Array
(
[id] => 103
[name] => d
)
)
)
You have mistake in key's names - bl_year, bl_month not exist:
foreach ($arr as $row) {
$tree[$row['year']] = array(
$row['month'] => array (
'id' => $row['id'],
'name' => $row['name'],
),
);
}

add sum of values to first array for each matching ID in the second array in PHP

I have two arrays like this:
Array1
$array1 = Array
(
0 => Array
(
'ID' => 101,
'Code' => 1075,
'Date' => '2012-03-03 17:13:12.433'
),
1 => Array
(
'ID' => 103,
'Code' => 175,
'Date' => '2012-09-05 20:30:02.217'
),
2 => Array
(
'ID' => 109,
'Code' => 178,
'Date' => '2012-07-05 20:30:02.217'
)
);
Array2
$array2 = Array
(
0 => Array
(
'Amount' => 1234,
'ID' => 101
),
1 => Array
(
'Amount' => 5656,
'ID' => 101
),
2 => Array
(
'Amount' => 1342,
'ID' => 103
),
3 => Array
(
'Amount' => 0,
'ID' => 0
)
);
I'm using the code below to perform a join on the two arrays :
$arr2 = array_column($array2, "ID");
$finalArray = array();
foreach($array1 as $arr){
$key = array_search($arr['ID'], $arr2);
if($key ===false){
$key = array_search(0, $arr2);
$array2[$key]['Found'] = "No";
}
else {
$array2[$key]['Found'] = "Yes";
}
unset($array2[$key]['ID']);
$finalArray[] = array_merge($arr,$array2[$key]);
}
print_r($finalArray);
The current output using the code above is :
finalArray
Array
(
[0] => Array
(
[ID] => 101
[Code] => 1075
[Date] => 2012-03-03 17:13:12.433
[Amount] => 1234 //considers only the first entry of ID 101 in array2
[Found] => Yes
)
[1] => Array
(
[ID] => 103
[Code] => 175
[Date] => 2012-09-05 20:30:02.217
[Amount] => 1342
[Found] => Yes
)
[2] => Array
(
[ID] => 109
[Code] => 178
[Date] => 2012-07-05 20:30:02.217
[Amount] => 0
[Found] => No
)
)
But since in array2 there are two entries for ID 101 but the code above only takes the first match for a matching ID.
The expected output is :
Desired Output
Array
(
[0] => Array
(
[ID] => 101
[Code] => 1075
[Date] => 2012-03-03 17:13:12.433
[Amount] => 6890 //sum of all the amounts(1234+5656)for matching ID 101
[Found] => Yes
)
[1] => Array
(
[ID] => 103
[Code] => 175
[Date] => 2012-09-05 20:30:02.217
[Amount] => 1342
[Found] => Yes
)
[2] => Array
(
[ID] => 109
[Code] => 178
[Date] => 2012-07-05 20:30:02.217
[Amount] => 0
[Found] => No
)
)
I'm not able to figure out how to do the addition here.
The code should do addition of the Amount feild for each matching ID of array2 and merge that amount to array1 Amount feild as shown in the expected output above.
How do I modify my current code such that it gives me the desired output?
What about:
$finalArray = array();
foreach ($array1 as $arr1)
{
$amount = 0;
foreach ($array2 as $key2 => $arr2)
{
if ($arr1['ID'] === $arr2['ID'])
{
$amount += $arr2['Amount'];
unset($array2[$key2]);
}
}
$finalArray[] = array_merge($arr1, array(
'Amount' => $amount,
'Found' => $amount ? "Yes" : "No"
));
}
print_r($finalArray);
Output:
Array
(
[0] => Array
(
[ID] => 101
[Code] => 1075
[Date] => 2012-03-03 17:13:12.433
[amount] => 6890
[found] => Yes
)
[1] => Array
(
[ID] => 103
[Code] => 175
[Date] => 2012-09-05 20:30:02.217
[amount] => 1342
[found] => Yes
)
[2] => Array
(
[ID] => 109
[Code] => 178
[Date] => 2012-07-05 20:30:02.217
[amount] => 0
[found] => No
)
)
Let me know if you need any further explanation.

Group array by field and give total value

I'm trying to group the following array by 'label' to give a total value for each person.
Array
(
[0] => Array
(
[label] => John
[value] => 84
)
[1] => Array
(
[label] => Darren
[value] => 28
)
[2] => Array
(
[label] => John
[value] => 20
)
[3] => Array
(
[label] => Morgan
[value] => 20
)
[4] => Array
(
[label] => Hannah
[value] => 14
)
[5] => Array
(
[label] => Morgan
[value] => 14
)
[6] => Array
(
[label] => Darren
[value] => 10
)
)
This would be the end result:
Array
(
[0] => Array
(
[label] => John
[value] => 104
)
[1] => Array
(
[label] => Darren
[value] => 38
)
[2] => Array
(
[label] => Morgan
[value] => 34
)
[3] => Array
(
[label] => Hannah
[value] => 14
)
)
I'm assuming I need to use foreach to group the labels together but I'm stuck for the best way to do this.
I would do something like this (expecting your array in $array):
// output array
$newArray = array();
// loop over the input array
foreach($array as $entry) {
// loop over the output array
foreach($newArray as &$currentEntry) {
// check if there is a the current label
if($currentEntry['label'] == $entry['label']) {
// if so, add the value and continue (also parent)
$currentEntry['value'] += $entry['value'];
continue 2;
}
}
// if the current label wan't found, it will get here and add an entry
array_push($newArray,$entry);
}
print_r($newArray);
Try this:
<?php
$array = [
['label' => 'John',
'value' => '84'],
['label' => 'Darren',
'value' => '28'],
['label' => 'John',
'value' => '20'],
['label' => 'Morgan',
'value' => '20'],
['label' => 'Hannah',
'value' => '14'],
['label' => 'Morgan',
'value' => '14'],
['label' => 'Darren',
'value' => '10']
];
$final = [];
foreach ($array as $arr)
$final[$arr['label']] = isset($final[$arr['label']]) ? $final[$arr['label']] + $arr['value'] : $arr['value'];
$result = [];
foreach ($final as $label => $value)
$result[] = ['label' => $label, 'value' => $value];
print_r($result);
Output:
Array
(
[0] => Array
(
[label] => John
[value] => 104
)
[1] => Array
(
[label] => Darren
[value] => 38
)
[2] => Array
(
[label] => Morgan
[value] => 34
)
[3] => Array
(
[label] => Hannah
[value] => 14
)
)

How to construct sub arrays from a main array

I would like to construct sub n number of arrays from a multi dimensional array depends on the data. For example: I have a main array as
Array
(
[0] => Array
(
[id] => 1
[status] => -1
)
[1] => Array
(
[id] => 2
[status] => 1
)
[2] => Array
(
[id] => 3
[status] => 2
)
[3] => Array
(
[id] => 4
[status] => 2
)
[4] => Array
(
[id] => 5
[status] => 2
)
)
I would like to get 3 arrays from this array depends on the no and type of status value
like
array{
[0]=array(
[status]=-1
[count]=1
)
[1]=array(
[status]=1
[count]=1
)
[2]=array(
[status]=2
[count]=3
)
}
Thanks in advance,
Sunil Kumar P
You mean like this?:
$array = array(
array('id' => 1, 'status' => -1),
array('id' => 2, 'status' => 1),
array('id' => 3, 'status' => 2),
array('id' => 4, 'status' => 2),
array('id' => 5, 'status' => 2)
);
$statuses = array();
foreach($array as $one){
if(!isset($statuses[$one['status']])){ $statuses[$one['status']] = 0; }
$statuses[$one['status']]++;
}
$newArray = array();
foreach($statuses as $key => $val){
$newArray[] = array('status' => $key, 'count' => $val);
}
print_r($newArray);
/*Array
(
[0] => Array
(
[status] => -1
[count] => 1
)
[1] => Array
(
[status] => 1
[count] => 1
)
[2] => Array
(
[status] => 2
[count] => 3
)
)*/

Categories