How to group image order by time from array - php

I have array of photo with filename and time. I want to find the photos which are taken in hour. For example, I have photos taken from 10.00, 10.15, 10.59. So when I search. I want these photo to be listed under 10.00.
I think it's something to do with in_array or array_search. But I still don't have an idea how.
Here is my array: $pixArr[]
Array
(
[0] => Array
(
[file] => IMG_7519.JPG
[time] => 13:02
)
[1] => Array
(
[file] => IMG_7518.JPG
[time] => 13:01
)
[2] => Array
(
[file] => IMG_7517.JPG
[time] => 13:00
)
[3] => Array
(
[file] => IMG_7516.JPG
[time] => 11:39
)
[4] => Array
(
[file] => IMG_7515.JPG
[time] => 11:39
)
[5] => Array
(
[file] => IMG_7514.JPG
[time] => 11:38
)
[6] => Array
(
[file] => IMG_7513.JPG
[time] => 11:29
)
[7] => Array
(
[file] => IMG_7512.JPG
[time] => 11:26
)
[8] => Array
(
[file] => IMG_7511.JPG
[time] => 11:26
)
)
I expect it coming out something like this:
10:00
listPhoto.php
<?
//now how to list files by hour???
?>

you may use array_filter As #u_mulder suggested:
<?php
$pixArr = array
(
0 => array
(
'file' => 'IMG_7519.JPG',
'time' => '13:02'
),
1 => array
(
'file' => 'IMG_7518.JPG',
'time' => '13:01'
),
2 => array
(
'file' => 'IMG_7517.JPG',
'time' => '13:00'
),
3 => array
(
'file' => 'IMG_7516.JPG',
'time' => '11:39'
),
4 => array
(
'file' => 'IMG_7515.JPG',
'time' => '11:39'
),
5 => array
(
'file' => 'IMG_7514.JPG',
'time' => '11:38'
),
6 => array
(
'file' => 'IMG_7513.JPG',
'time' => '11:29'
),
7 => array
(
'file' => 'IMG_7512.JPG',
'time' => '11:26'
),
8 => array
(
'file' => 'IMG_7511.JPG',
'time' => '11:26'
),
);
print_r(array_filter($pixArr, function($value){
if($value['time'] >= '11:00' && $value['time'] <= '12:00')
return $value;
}));
See Demo: https://eval.in/513466

you can using assort This function sorts an array such that array indices maintain their correlation with the array elements they are associated with.
I have been tried this problem.
$arr = [
[
"file" => "IMG_7519.JPG",
"time" => "13:02"
],
[
"file" => "IMG_7519.JPG",
"time" => "12:02"
],
[
"file" => "IMG_7519.JPG",
"time" => "15:02"
]
];
asort($arr); //sorting array
echo "<pre>".print_r($arr)."</pre>";
and this is the result.
Array ( [1] => Array ( [file] => IMG_7519.JPG [time] => 12:02 ) [0] => Array ( [file] => IMG_7519.JPG [time] => 13:02 ) [2] => Array ( [file] => IMG_7519.JPG [time] => 15:02 ) )

Related

Compare and Update PHP array

I have two arrays in PHP:
Array 1
Array
(
[0] => Array
(
[autoid] => t35wmkbg
[task] => Zaphod
[progress] => incomplete
)
[1] => Array
(
[autoid] => cwg2yc5q
[task] => Arthur
[progress] => incomplete
)
[2] => Array
(
[autoid] => 85q4bcmy
[task] => Ford
[progress] => incomplete
)
[3] => Array
(
[autoid] => yc5qcwg2
[task] => Trillian
[progress] => incomplete
)
)
Array 2
Array
(
[0] => Array
(
[autoid] => t35wmkbg
[task] => Zaphod
[progress] => complete
)
[1] => Array
(
[autoid] => 85q4bcmy
[task] => Ford
[progress] => incomplete
)
[2] => Array
(
[autoid] => cwg2yc5q
[task] => Arthur
[progress] => pending
)
)
I need to:
Step through Array 1, and check if the autoid exists in Array 2.
If it does exist, update the progress field to match.
So the ideal output when using the arrays above would be:
Array 3
Array
(
[0] => Array
(
[autoid] => t35wmkbg
[task] => Zaphod
[progress] => complete
)
[1] => Array
(
[autoid] => cwg2yc5q
[task] => Arthur
[progress] => pending
)
[2] => Array
(
[autoid] => 85q4bcmy
[task] => Ford
[progress] => incomplete
)
[3] => Array
(
[autoid] => yc5qcwg2
[task] => Trillian
[progress] => incomplete
)
)
I am not sure how best to go about this, if anyone has any thoughts or pointers that would be awesome.
First change the arr2 so its index is the autoid then you can use it easily to get the progress value.
Then just foreach over arr1 and apply changes to progress if they exist
$arr1 = [
['autoid' => 't35wmkbg','task' => 'Zaphod','progress' => 'incomplete'],
['autoid' => 'cwg2yc5q', 'task' => 'Arthur', 'progress' => 'incomplete'],
['autoid' => '85q4bcmy', 'task' => 'Ford', 'progress' => 'incomplete'],
['autoid' => 'yc5qcwg2', 'task' => 'Trillian', 'progress' => 'incomplete']
];
$arr2 = [
['autoid' => 't35wmkbg', 'task' => 'Zaphod', 'progress' => 'complete'],
['autoid' => '85q4bcmy', 'task' => 'Ford', 'progress' => 'incomplete'],
['autoid' => 'cwg2yc5q', 'task' => 'Arthur', 'progress' => 'pending']
];
#first make arr2 indexable on the autoid
foreach ($arr2 as $a) {
$arr2search[$a['autoid']] = $a;
}
foreach ($arr1 as $a){
// do we have an autoid in arr2
if ( isset($arr2search[$a['autoid']])){
$a['progress'] = $arr2search[$a['autoid']]['progress'];
}
$merged[] = $a;
}
print_r($merged);
RESULT
Array
(
[0] => Array
([autoid] => t35wmkbg, [task] => Zaphod, [progress] => complete)
[1] => Array
([autoid] => cwg2yc5q, [task] => Arthur, [progress] => pending )
[2] => Array
([autoid] => 85q4bcmy, [task] => Ford, [progress] => incomplete )
[3] => Array
([autoid] => yc5qcwg2, [task] => Trillian, [progress] => incomplete )
)

merge two array with loop and key name

i have two separate arrays $first_one:
Array
(
[0] => Array
(
[_date] => 2019-10-16
[_number] => 1
[_order] => 1
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[_date] => 2020-10-11
[_number] => 2
[_order] => 2
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)
and the $second_array:
Array
(
[0] => Array
(
[date] => 2019-10-16
[number] => 1
[order] => 1
[name] => jack
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[date] => 2020-10-11
[number] => 2
[order] => 2
[name] => joey
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1433
)
)
[2] => Array
(
[date] => 2020-10-28
[number] => 3
[order] => 3
[name] => tom
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1593
)
)
)
they are very similar but they came from different api's and they are different in numbers and also some key names.
so what i'm trying to do is to count the $first_one arrays and if for that many loop through the $second_one (in this example is 2) and if the [_number] [_order] from $first_one was equal (==) to [number] [number] $second_one then take some info (for example the last name) from it and put in a new array.
so is this possible to do?
$arr1 = [ [ '_date' => '2019-10-16','_number' => 1,'_order' => 1,
'name' => 'jack','other_ids' => ['b_id' => 1253]
],
['_date' => 2020-10-11,'_number' => 2,'_order' => 2,
'name' => 'joey','other_ids' => ['b_id' => 1433]
]
];
$arr2 = [ [ 'date' => '2019-10-16','number' => '1','order' => '1',
'name' => 'jack','last_name' => 'foobar','other_ids' => ['b_id' => 1253]
],
[ 'date' => '2019-10-11','number' => '2','order' => '2',
'name' => 'joey','last_name' => 'foobar','other_ids' => ['b_id' => 1433]
],
[ 'date' => '2019-10-28', 'number' => '3', 'order' => '3',
'name' => 'tom', 'last_name' => 'foobar', 'other_ids' => ['b_id' => 1593]
],
];
// first make second array more directly searchable
// make new array with the `number` as the key
foreach( $arr2 as $a){
$arr2new[$a['number']] = $a;
}
foreach ($arr1 as $a) {
if ( array_key_exists($a['_number'], $arr2new) &&
$a['_order'] == $arr2new[$a['_order']]['order'] )
{
$merged[] = ['name'=>$a['name'], 'other_ids' => $a['other_ids']];
}
}
print_r($merged);
RESULT
Array
(
[0] => Array (
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array (
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)

Unexpected php usort result

I have an array with files. And I want to sort them by their min value (I'll explain below).
This is an example of array:
$files = array(
array(
'name' => "DevTools/tools/files/design/js/custom/http_build_query.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "DevTools/tools/files/design/js/custom/Form.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "DevTools/tools/files/design/js/custom/VanillaJS.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/plugins/jquery.js"
),
array(
'name' => "resources/js/plugins/jquery-ui.js"
),
array(
'name' => "resources/js/plugins/popper.js"
),
array(
'name' => "resources/js/plugins/bootstrap-v4/index.js",
'range' => array(
'min' => 800
)
),
array(
'name' => "resources/js/plugins/bootstrap-v4/util.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/plugins/bootstrap-v4/dropdown.js",
'range' => array(
'min' => 200
)
),
array(
'name' => "resources/js/plugins/bootstrap-v4/collapse.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/custom/global.js"
),
array(
'name' => "layouts/cms/.js/+0.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/plugins/module/delete.js"
),
array(
'name' => "resources/js/plugins/module/form.submit.js"
),
array(
'name' => "resources/js/plugins/module/piece.image.js"
),
array(
'name' => "resources/js/plugins/select2.js"
)
);
It's easy to understand. We have names for all files. And some of them have [range][min].
What I need to do is keeping their order, but sorting only files from same folder which have the [range][min], so we reorder these ones by [min].
My code
So I use this function:
usort($files, function (array $a, array $b) {
if (!isset($a['range']) || !isset($b['range'])
|| dirname($a['name']) != dirname($b['name'])
|| $a['range']['min'] == $b['range']['min']) {
return 0;
}
return $a['range']['min'] <=> $b['range']['min'];
});
Which does exactly what I said above. So only those ones are correcly reordered, as you can see here:
[6] => Array
(
[name] => resources/js/plugins/bootstrap-v4/util.js
[range] => Array
(
[min] => 0
)
)
[7] => Array
(
[name] => resources/js/plugins/bootstrap-v4/collapse.js
[range] => Array
(
[min] => 0
)
)
[8] => Array
(
[name] => resources/js/plugins/bootstrap-v4/dropdown.js
[range] => Array
(
[min] => 200
)
)
[9] => Array
(
[name] => resources/js/plugins/bootstrap-v4/index.js
[range] => Array
(
[min] => 800
)
)
Which is great.
The problem
Now I add in my $files array this one:
array(
'name' => "resources/js/plugins/module/piece.images.js"
)
Append it anywhere you want in $files, the usort will reorder entire array with no logic:
Array
(
[0] => Array
(
[name] => DevTools/tools/files/design/js/custom/http_build_query.js
[range] => Array
(
[min] => 0
)
)
[1] => Array
(
[name] => resources/js/plugins/bootstrap-v4/collapse.js
[range] => Array
(
[min] => 0
)
)
[2] => Array
(
[name] => resources/js/plugins/select2.js
)
[3] => Array
(
[name] => resources/js/plugins/module/piece.image.js
)
[4] => Array
(
[name] => resources/js/plugins/module/form.submit.js
)
[5] => Array
(
[name] => resources/js/plugins/module/delete.js
)
[6] => Array
(
[name] => layouts/cms/.js/+0.js
[range] => Array
(
[min] => 0
)
)
[7] => Array
(
[name] => resources/js/plugins/bootstrap-v4/util.js
[range] => Array
(
[min] => 0
)
)
[8] => Array
(
[name] => resources/js/custom/global.js
)
[9] => Array
(
[name] => resources/js/plugins/bootstrap-v4/dropdown.js
[range] => Array
(
[min] => 200
)
)
[10] => Array
(
[name] => DevTools/tools/files/design/js/custom/Form.js
[range] => Array
(
[min] => 0
)
)
[11] => Array
(
[name] => resources/js/plugins/bootstrap-v4/index.js
[range] => Array
(
[min] => 800
)
)
[12] => Array
(
[name] => resources/js/plugins/popper.js
)
[13] => Array
(
[name] => resources/js/plugins/jquery-ui.js
)
[14] => Array
(
[name] => resources/js/plugins/jquery.js
)
[15] => Array
(
[name] => DevTools/tools/files/design/js/custom/VanillaJS.js
[range] => Array
(
[min] => 0
)
)
[16] => Array
(
[name] => resources/js/plugins/module/piece.images.js
)
)
I have no idea why is that happening. If you can find out why, can you please see if there is a solution please?
If I didn't explain well, please ask in comments for clarification.
So, what I need to do is keeping their order, but sorting only files from same folder which have the [range][min], so we reorder these ones by [min].

how to add new key and value to multidimensional in PHP without using the loops....?

Array ( [0] => Array (
[date] => 01-06-2018
[nav] => 30.65100 )
[1] => Array (
[date] => 31-05-2018
[nav] => 30.84900 )
[2] => Array (
[date] => 30-05-2018
[nav] => 30.73200 )
[3] => Array (
[date] => 29-05-2018
[nav] => 30.81500 )
The above code is the Multi-array, we have added a common id like id_code = 0089 to every array in it without using any loops in PHP. Can anyone helps me and it is possible or not .....?
If you mean not manually loop through the array then yes, it is possible using array_walk:
$array = [
0 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
1 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
2 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
3 => [
"date" =>"01-06-2018",
"nav" => "30.65100"]
];
array_walk($array, function(&$item1) {
$item1['id_code'] = "0089";
});
print_r($array);
Output:
Array
(
[0] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[1] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[2] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[3] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
)
Demo https://3v4l.org/lCGIO
An effective solution will be to use array_map function as:
$keyValue = 'some value';
$data = array_map(function($d) use ($keyValue){
return $d + ['keyName' => $keyValue];
}, $data);
I think this is the closer you can get, but it is probably not your expected result. The exact result you need cannot be done without using a loop as far as I know.
$t = array( 0 => array( 'date' => '01-06-2018', 'nav' => '30.65100' ), 1 => array( 'date' => '31-05-2018', 'nav' => '30.84900' ), 2 => array( 'date' => '30-05-2018', 'nav' => '30.73200' ), 3 => array( 'date' => '29-05-2018', 'nav' => '30.81500' ));
$tt = array( 0 => array( 'id' => '648'), 1 => array( 'id' => '332'), 2 => array( 'id' => '889'), 3 => array( 'id' => '285') );
$final = array_map(null, $t, $tt);
print_r($final);
The output will look like
Array
(
[0] => Array
(
[0] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
)
[1] => Array
(
[id] => 648
)
)
[1] => Array
(
[0] => Array
(
[date] => 31-05-2018
[nav] => 30.84900
)
[1] => Array
(
[id] => 332
)
)
[2] => Array
(
[0] => Array
(
[date] => 30-05-2018
[nav] => 30.73200
)
[1] => Array
(
[id] => 889
)
)
[3] => Array
(
[0] => Array
(
[date] => 29-05-2018
[nav] => 30.81500
)
[1] => Array
(
[id] => 285
)
)
)

PHP:Merge array values when key has same value [duplicate]

This question already has answers here:
PHP - Count duplicate values within two dimensional array, then display only unique values with the count
(2 answers)
Closed 4 years ago.
I have a multidimensional Array like this. Which I have to sort in some way where the same user's data will be stored in one array.
Array
(
[0] => Array
(
[user_email] => example#gmail.com
[available_date] => 2018/06/30
[available_time] => 06.00,06.30,07.00,07.30
)
[1] => Array
(
[user_email] => example#gmail.com
[available_date] => 2018/06/31
[available_time] => 06.30,07.00,07.30,08.00
)
[3] => Array
(
[user_email] => newuser#gmail.com
[available_date] => 2018/06/31
[available_time] => 08.00,08.30,09.00,09.30
)
[4] => Array
(
[user_email] => newuser#gmail.com
[available_date] => 2018/06/30
[available_time] => 08.30,09.00,09.30,10.00
)
)
)
I want the final array to be like this,Where available_date and available time will be merged as an array with a key name for a same user.Maybe this is easy but I am stuck.Any help would be appreciated.
Array
(
[0] => Array
(
[user_email] => example#gmail.com
[date_time] => Array
(
[0]=>Array
(
[date] => 2018/06/30
[time] => 06.00,06.30,07.00,07.30,
)
[1]=>Array
(
[date] => 2018/06/31
[time] => 06.30,07.00,07.30,08.00,
)
)
)
[1] => Array
(
[user_email] => newuser#gmail.com
[date_time] => Array
(
[0]=>Array
(
[date] => 2018/06/31,
[time] => 08.00,08.30,09.00,09.30,
)
[1]=>Array
(
[date] => 2018/06/30,
[time] => 08.30,09.00,09.30,10.00,
)
)
)
)
You could do something like this :
$x =
[
"0" => [
"user_email" => "example#gmail.com",
"available_date" => "2018/06/30",
"available_time" => "06.00,06.30,07.00,07.30",
],
"1" => [
"user_email" => "example#gmail.com",
"available_date" => "2018/06/31",
"available_time" => "06.30,07.00,07.30,08.00,"
],
"3" => [
"user_email" => "newuser#gmail.com",
"available_date" => "2018/06/31",
"available_time" => "08.00,08.30,09.00,09.30",
],
"4" => [
"user_email" => "newuser#gmail.com",
"available_date" => "2018/06/30",
"available_time" => "08.30,09.00,09.30,10.00",
]
];
$result = [];
foreach ($x as $item) {
$temp['date'] = $item['available_date'];
$temp['time'] = $item['available_time'];
$result[$item['user_email']]['date_time'][] = $temp;
}
print_r($result);
And the result is this :
Array ( [example#gmail.com] => Array ( [date_time] => Array ( [0] => Array ( [date] => 2018/06/30 [time] => 06.00,06.30,07.00,07.30 ) [1] => Array ( [date] => 2018/06/31 [time] => 06.30,07.00,07.30,08.00, ) ) ) [newuser#gmail.com] => Array ( [date_time] => Array ( [0] => Array ( [date] => 2018/06/31 [time] => 08.00,08.30,09.00,09.30 ) [1] => Array ( [date] => 2018/06/30 [time] => 08.30,09.00,09.30,10.00 ) ) ) )

Categories