Only show Unique ID greater than x seconds in PHP - php

I am trying to figure out how to only output unique IDs that are above 30 seconds in this array. to make it simple it just needs to grab the first value over 30 seconds for a unique ID.
$array = array(
0 => array(
'id' => '1',
'seconds' => '36'
),
1 => array(
'id' => '1',
'seconds' => '60'
),
2 => array(
'id' => '1',
'seconds' => '36'
),
3 => array(
'id' => '2',
'seconds' => '22'
),
4 => array(
'id' => '1',
'seconds' => '36'
),
5 => array(
'id' => '2',
'seconds' => '36'
),
6 => array(
'id' => '3',
'seconds' => '44'
),
7 => array(
'id' => '3',
'seconds' => '2'
),
8 => array(
'id' => '4',
'seconds' => '58'
),
9 => array(
'id' => '6',
'seconds' => '9'
),
10 => array(
'id' => '6',
'seconds' => '8'
));
Ideal result would look like this
$arrayResult = array(
0 => array(
'id' => '1',
'seconds' => '36'
),
1 => array(
'id' => '2',
'seconds' => '36'
),
2 => array(
'id' => '3',
'seconds' => '44'
),
3 => array(
'id' => '4',
'seconds' => '58'
));
Currently I can only get unique values for the ID's without having the seconds correlated with the 'id' field.

You can use array_reduce to iterate over the $array and extract only values that you want.
$array = array_reduce($array, function ($unique, $entry) {
extract($entry);
if (! isset($unique[$id]) && $seconds > 30) {
$unique[$id] = $entry;
}
return $unique;
}, []);
$array = array_values($array);

$final_array = array();
foreach($array as $val)
{
if($val['seconds'] > 30 && (!array_key_exists($val['id'], $final_array))){
$final_array[$val['id']] = $val;
}
}
echo "<pre>";
print_r($final_array);
echo "</pre>";
if you want to reset array keys then you can use
$final_array = array_values($final_array);
echo "<pre>";
print_r($final_array);
echo "</pre>";

Related

OUTPUT Alone 1 key from the array

How to make if _from is Alone to _to --- to Select _Alone with the higher ID
Drop else from array
array (
0 =>
array (
'id' => '8',
'_from' => '2',
'_to' => '1',
'date' => '2018-10-15 15:51:07',
'message' => 'ccccccxxxxx',
'read' => '0',
'feedback' => '0',
'cnt' => '3',
),
1 =>
array (
'id' => '6',
'_from' => '1',
'_to' => '2',
'date' => '2018-10-15 15:47:01',
'message' => 'zzzzzzz1',
'read' => '1',
'feedback' => '0',
'cnt' => '1',
),
If you have small arrays, It is not a problem to run the algorithm with complexity O(n2). But for me better is less clear, but faster algorithm with complexity equals to O(2n)
$array = array(
array(
'id' => 12,
'_from' => 1,
'_to' => 2
),
array(
'id' => 13,
'_from' => 4,
'_to' => 2
),
array(
'id' => 14,
'_from' => 2,
'_to' => 1
),
);
$newArray = [];
foreach ($array as $item) {
$uniqueRecordKey = $item['_from'].'-'.$item['_to'];
$oppositeRecordKey = $item['_to'].'-'.$item['_from'];
//If exists record from the opposite and new ID is greater than previous put
if (isset($newArray[$oppositeRecordKey])) {
$newArray[$oppositeRecordKey] = $item;
continue; //Do not append to the end
}
$newArray[$uniqueRecordKey] = $item;
}
var_dump($newArray);
https://3v4l.org/0oL2d

Check if key exist in array if so, count it and create a new array php

From my SQL query, I will get the following array as a result. This is the last result I can get from SQL.I cant change any SQL because of some constraint.
I need to check if the same id exists or not and if it does need to count them and remove one of the duplicate array having the same id.
Sample array is
$array = array(
0 => array(
'id' => '17',
'status' => 1,
),
1 => array(
'id' => '18',
'status' => 1,
),
2 => array(
'id' => '21',
'status' => 1,
),
3 => array(
'id' => '5',
'status' => 2,
),
4 => array(
'id' => '18',
'status' => 1,
),
5 => array(
'id' => '22',
'status' => 5,
),
6 => array(
'id' => '6',
'status' => 1,
),
);
I need to check if they have a duplicate id or not, if yes need to count them and remove one of the duplicates.We need to preserve the array structure.
End Results should be
array(
0 => array(
'id' => '17',
'status' => 1,
'count'=1,
),
1 => array(
'id' => '18',
'status' => 1,
'count'=2,
),
2 => array(
'id' => '21',
'status' => 1,
'count'=1,
),
3 => array(
'id' => '5',
'status' => 2,
'count'=1,
),
4 => array(
'id' => '22',
'status' => 5,
'count'=1,
),
5 => array(
'id' => '6',
'status' => 1,
'count'==>1,
),
)
You can Check it by this way but duplicate entry still in the array.
$ids = array();
foreach ($array as $key => $value)
{
$ids[] = $value['id'];
$count = array_count_values($ids);
}
for($i = 0; $i < count($array);$i++)
{
$array[$i]['count'] = $count[$array[$i]['id']];
}
You can do this with the array_unique function.
Use it like so:
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
Which outputs:
Array ( [a] => red [b] => green )

Multidimensional array difference

I need find difference between multidimensional arrays
I have arrays like this :
$arr1 = array(
'test1' => array(
'XXX' => array(
'w1' => array('id' => '5'),
'w2' => array('id' => '2'),
'w3' => array('id' => 'g'),
),
'YYY' => array(
'w1' => array('id' => '4'),
'w2' => array('id' => '1')
),
'ZZZ' => array(
'w1' => array('id' => '3'),
'w2' => array('id' => '9')
),
'QQQ' => array(
'w1' => array('id' => '3'),
'w2' => array('id' => '9')
),
),
'test2' => array(
'XXX' => array(
'w1' => array('id' => '8'),
'w2' => array('id' => '3')
),
'YYY' => array(
'w1' => array('id' => '15'),
'w2' => array('id' => '1')
),
'ZZZ' => array(
'w1' => array('id' => '5'),
'w2' => array('id' => '2')
),
),
);
$arr2 = array(
'test1' => array(
'XXX' => array(
'w1' => array('id' => '5'),
'w2' => array('id' => '2'),
'w3' => array('id' => 'g'),
'w4' => array('id' => 'x'),
),
'YYY' => array(
'w1' => array('id' => '4'),
'w2' => array('id' => '1')
),
'ZZZ' => array(
'w1' => array('id' => '3'),
'w2' => array('id' => '9')
),
),
);
And I need to remove duplicates but I need compare first level key, second level key and last level key,value pairs so my results should be like that
array(
'test1' => array(
'XXX' => array(
'w4' => array('id' => 'x'),
),
)
I try use
function check_key($a,$b) {
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
function check_value($a,$b) {
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$merged_arr = array_udiff_uassoc($arr2,$arr1,"check_key","check_value");
But this function don't compare last level key-value pair in last level .
Try with this function.I think It will be helpful to you.
$result[] = array_diff_key($arr1, $arr2);
print_r($result);

Getting largest values from different arrays

I want to get the largest values from different arrays. Basically, the arrays are populated from 3 different websites, and I need to know what are the largest tags and photos values of each product.
I have the following array:
$data = array(
'domain1.com' => array(
'id1' => array(
'tags' => '5',
'photos' => '4',
),
'id2' => array(
'tags' => '8',
'photos' => '2',
),
'id3' => array(
'tags' => '6',
'photos' => '1',
),
),
'domain2.com' => array(
'id1' => array(
'tags' => '3',
'photos' => '1',
),
'id2' => array(
'tags' => '4',
'photos' => '9',
),
'id3' => array(
'tags' => '2',
'photos' => '0',
),
),
'domain3.com' => array(
'id1' => array(
'tags' => '7',
'photos' => '3',
),
'id2' => array(
'tags' => '9',
'photos' => '5',
),
'id3' => array(
'tags' => '2',
'photos' => '4',
),
),
);
I need to get the following result:
$data = array(
'id1' => array(
'tags' => '7',
'photos' => '4',
),
'id2' => array(
'tags' => '9',
'photos' => '9',
),
'id3' => array(
'tags' => '6',
'photos' => '4',
),
);
This is solveable with a simple loop:
$new = array();
foreach($data as $domain){
foreach($domain as $id => $_data){
foreach($_data as $elem => $value) {
if(!isset($new[$id][$elem]) || $value > $new[$id][$elem]){
$new[$id][$elem] = $value;
}
}
}
}
Which returns:
Array
(
[id1] => Array
(
[tags] => 7
[photos] => 4
)
[id2] => Array
(
[tags] => 9
[photos] => 9
)
[id3] => Array
(
[tags] => 6
[photos] => 4
)
)
Example
The easiest way is to use nested foreach loops to iterate and capture the data. The results are updated if there is a higher value found in the results array.
$results = [];
foreach ($data as $domain => $items) {
foreach ($items as $id => $elements) {
if (!isset($results[ $id ])) {
$results[ $id ] = [];
}
foreach ($elements as $tag => $value) {
if (!isset($results[ $id ][ $tag ]) || $value>$results[ $id ][ $tag ]) {
$results[ $id ][ $tag ] = $value;
}
}
}
}
print_r($results);

Extracting data from complicated associative array in php and put into new array

I have an complicated array that looks like this:
$input=array(
(int) 0 => array(
'XXX' => array(
'id' => '7',
'p_id' => '1',
'address' => '9463',
'arrival_time' => '2014-05-01 03:30:00'
),
'YYY' => array(
'id' => '1',
'iden' => '1111',
'name' => 'Tom'
)
),
(int) 1 => array(
'XXX' => array(
'id' => '9',
'p_id' => '2',
'address' => '9469',
'arrival_time' => '2014-05-27 16:43:58'
),
'YYY' => array(
'id' => '2',
'iden' => '2222',
'name' => 'Sam'
)
),
(int) 2 => array(
'XXX' => array(
'id' => '3',
'p_id' => '3',
'address' => '9462',
'arrival_time' => '2014-04-21 14:05:00'
),
'YYY' => array(
'id' => '3',
'iden' => '3333',
'name' => 'James'
)
)
)
I would like to convert it such that it looks like this;
$output=array(
(int) 0 => array(
'name' => 'Tom',
'iden' => '1111',
'address' => '9463'
),
(int) 1 => array(
'name' => 'Sam',
'iden' => '2222',
'address' => '9469'
),
(int) 2 => array(
'name' => 'James',
'iden' => '3333',
'address' => '9462'
)
I wrote some code to solve this problem:
foreach ( $input as $key => $value)
{
$output['name']=$input[$key]['YYY']['name'];
$output['iden']=$input[$key]['YYY']['iden'];
$output['address']=$input[$key]['XXX']['address'];
}
Unfortunately, it retrieves only the last element of the input array.
Can someone more experienced help?
Thank you very much.
You are overwriting the values in each iteration, as you always write to $output['name'] etc.
foreach ( $input as $key => $value)
{
$output[$key] = array(
'name' => $value['YYY']['name'],
'iden' => $value['YYY']['iden'],
'address' => $value['XXX']['address']
);
}
The key here is using $output[$key] instead of $output - this way you will add a new element in each iteration.
Also $input[$key] and $value are equivalent, so I used the shorter variant ;)
Try this in your foreach loop :-
foreach ( $input as $key=>$value)
{
$output[$key]['name']=$value['YYY']['name'];
$output[$key]['iden']=$value['YYY']['iden'];
$output[$key]['address']=$value['XXX']['address'];
}
You have to add an index to the array in the foreach: $output[$key]["name"] = ...;

Categories