How to sort this array by date? - php

I'm trying to sort a array of this kind :
array (
0 =>
array (
'id_ouverture' => 5,
'debut' => '2011-04-25 08:00:00',
'fin' => '2011-04-25 20:00:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
),
1 =>
array (
'id_ouverture' => 6,
'debut' => '2011-04-18 08:00:00',
'fin' => '2011-04-18 10:45:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
),
2 =>
array (
'id_ouverture' => 7,
'debut' => '2011-05-02 08:00:00',
'fin' => '2011-05-02 10:45:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
),
3 =>
array (
'id_ouverture' => 8,
'debut' => '2011-05-09 08:00:00',
'fin' => '2011-05-09 10:45:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
));
I need to sort this array on this key : array[$i]['debut'] on an ascending order.
The results must be :
array (
1 =>
array (
'id_ouverture' => 6,
'debut' => '2011-04-18 08:00:00',
'fin' => '2011-04-18 10:45:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
),
0 =>
array (
'id_ouverture' => 5,
'debut' => '2011-04-25 08:00:00',
'fin' => '2011-04-25 20:00:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
),
2 =>
array (
'id_ouverture' => 7,
'debut' => '2011-05-02 08:00:00',
'fin' => '2011-05-02 10:45:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
),
3 =>
array (
'id_ouverture' => 8,
'debut' => '2011-05-09 08:00:00',
'fin' => '2011-05-09 10:45:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
));
Have you an idea to do it ?

You need to use uasort to write a function that will sort the array for you.
http://php.net/manual/en/function.uasort.php
Basically you write your own comparison function and pass it as a callback to the uasort method

Related

Multidimensional array values with RecursiveArrayIterator for select option groups

I have this new multidimensional array and I'm trying to make multiple drop-down selects with option groups. So that each select contains options with "$value[ 'proj_id' ] $value[ 'projName' ]. Selects are grouped by ID (the first select: 127, second select: 131, third select: 130 etc.) Options are grouped by clients names, first opt.group Another client, with one option. Second opt. group Nice with two options etc. Before I was using RecursiveArrayIterator() now with third dimension it no longer works:
$iterator = new RecursiveArrayIterator( $project_data_group );
$dropdown = '';
// get all children for dropdown
while ( $iterator->valid() ) {
if ( $iterator->hasChildren() ) {
foreach ( $iterator->getChildren() as $key1 => $value ) {
$dropdown .= "<option data-proj_id=" . $value[ 'proj_id' ] . " value=" . $value[ 'projName' ] . ">" . $value[ 'projName' ] . "</option>";
}
}
break;
}
array (
127 =>
array (
'Another client' =>
array (
0 =>
array (
'dateTime' => '2020-06-04 09:33:00',
'id' => '127',
'cID' => '50',
'client' => 'Another client',
'proj_id' => '103',
'projName' => 'Logo new',
'is_from_main_table' => '1',
),
),
'Nice' =>
array (
0 =>
array (
'dateTime' => '2020-06-04 09:33:00',
'id' => '127',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '106',
'projName' => 'Advert',
'is_from_main_table' => '0',
),
1 =>
array (
'dateTime' => '2020-06-04 09:33:00',
'id' => '127',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '102',
'projName' => 'Webpage',
'is_from_main_table' => '0',
),
),
'Mikrosoft' =>
array (
0 =>
array (
'dateTime' => '2020-06-04 09:33:00',
'id' => '127',
'cID' => '51',
'client' => 'Mikrosoft',
'proj_id' => '104',
'projName' => 'Poster',
'is_from_main_table' => '0',
),
),
'Gooble' =>
array (
0 =>
array (
'dateTime' => '2020-06-04 09:33:00',
'id' => '127',
'cID' => '48',
'client' => 'Gooble',
'proj_id' => '105',
'projName' => 'Poster another',
'is_from_main_table' => '0',
),
),
),
131 =>
array (
'Nice' =>
array (
0 =>
array (
'dateTime' => '2020-06-08 17:23:00',
'id' => '131',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '102',
'projName' => 'Webpage',
'is_from_main_table' => '1',
),
1 =>
array (
'dateTime' => '2020-06-08 17:23:00',
'id' => '131',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '106',
'projName' => 'Advert',
'is_from_main_table' => '0',
),
),
'Another client' =>
array (
0 =>
array (
'dateTime' => '2020-06-08 17:23:00',
'id' => '131',
'cID' => '50',
'client' => 'Another client',
'proj_id' => '103',
'projName' => 'Logo new',
'is_from_main_table' => '0',
),
),
'Mikrosoft' =>
array (
0 =>
array (
'dateTime' => '2020-06-08 17:23:00',
'id' => '131',
'cID' => '51',
'client' => 'Mikrosoft',
'proj_id' => '104',
'projName' => 'Poster',
'is_from_main_table' => '0',
),
),
'Gooble' =>
array (
0 =>
array (
'dateTime' => '2020-06-08 17:23:00',
'id' => '131',
'cID' => '48',
'client' => 'Gooble',
'proj_id' => '105',
'projName' => 'Poster another',
'is_from_main_table' => '0',
),
),
),
130 =>
array (
'Gooble' =>
array (
0 =>
array (
'dateTime' => '2020-06-09 17:30:00',
'id' => '130',
'cID' => '48',
'client' => 'Gooble',
'proj_id' => '105',
'projName' => 'Poster another',
'is_from_main_table' => '1',
),
),
'Nice' =>
array (
0 =>
array (
'dateTime' => '2020-06-09 17:30:00',
'id' => '130',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '106',
'projName' => 'Advert',
'is_from_main_table' => '0',
),
1 =>
array (
'dateTime' => '2020-06-09 17:30:00',
'id' => '130',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '102',
'projName' => 'Webpage',
'is_from_main_table' => '0',
),
),
'Another client' =>
array (
0 =>
array (
'dateTime' => '2020-06-09 17:30:00',
'id' => '130',
'cID' => '50',
'client' => 'Another client',
'proj_id' => '103',
'projName' => 'Logo new',
'is_from_main_table' => '0',
),
),
'Mikrosoft' =>
array (
0 =>
array (
'dateTime' => '2020-06-09 17:30:00',
'id' => '130',
'cID' => '51',
'client' => 'Mikrosoft',
'proj_id' => '104',
'projName' => 'Poster',
'is_from_main_table' => '0',
),
),
),
133 =>
array (
'Nice' =>
array (
0 =>
array (
'dateTime' => '2020-06-11 19:58:00',
'id' => '133',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '106',
'projName' => 'Advert',
'is_from_main_table' => '1',
),
1 =>
array (
'dateTime' => '2020-06-11 19:58:00',
'id' => '133',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '102',
'projName' => 'Webpage',
'is_from_main_table' => '0',
),
),
'Another client' =>
array (
0 =>
array (
'dateTime' => '2020-06-11 19:58:00',
'id' => '133',
'cID' => '50',
'client' => 'Another client',
'proj_id' => '103',
'projName' => 'Logo new',
'is_from_main_table' => '0',
),
),
'Mikrosoft' =>
array (
0 =>
array (
'dateTime' => '2020-06-11 19:58:00',
'id' => '133',
'cID' => '51',
'client' => 'Mikrosoft',
'proj_id' => '104',
'projName' => 'Poster',
'is_from_main_table' => '0',
),
),
'Gooble' =>
array (
0 =>
array (
'dateTime' => '2020-06-11 19:58:00',
'id' => '133',
'cID' => '48',
'client' => 'Gooble',
'proj_id' => '105',
'projName' => 'Poster another',
'is_from_main_table' => '0',
),
),
),
134 =>
array (
'Mikrosoft' =>
array (
0 =>
array (
'dateTime' => '2020-06-22 13:36:00',
'id' => '134',
'cID' => '51',
'client' => 'Mikrosoft',
'proj_id' => '104',
'projName' => 'Poster',
'is_from_main_table' => '1',
),
),
'Nice' =>
array (
0 =>
array (
'dateTime' => '2020-06-22 13:36:00',
'id' => '134',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '106',
'projName' => 'Advert',
'is_from_main_table' => '0',
),
1 =>
array (
'dateTime' => '2020-06-22 13:36:00',
'id' => '134',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '102',
'projName' => 'Webpage',
'is_from_main_table' => '0',
),
),
'Another client' =>
array (
0 =>
array (
'dateTime' => '2020-06-22 13:36:00',
'id' => '134',
'cID' => '50',
'client' => 'Another client',
'proj_id' => '103',
'projName' => 'Logo new',
'is_from_main_table' => '0',
),
),
'Gooble' =>
array (
0 =>
array (
'dateTime' => '2020-06-22 13:36:00',
'id' => '134',
'cID' => '48',
'client' => 'Gooble',
'proj_id' => '105',
'projName' => 'Poster another',
'is_from_main_table' => '0',
),
),
),
)
I haven't tested this, but based on the grouping you want, this approach might work.
function getGroupedDropdownOptions($array) {
$options = [];
foreach ( $array as $groups ) {
foreach ( $groups as $clientName => $group ) {
// You should be able to use $clientName or $group['client'] here
$options[$group['client']][] = ["proj_name" => $group['projName'], "proj_id" => $group['proj_id'];
}
break;
}
return $options;
}
Build the HTML like this after setting $dropdownOptions with getGroupedDropdownOptions().
<select>
<?php foreach($dropdownOptions as $groupName => $group): ?>
<optgroup label="<?php echo $groupName; ?>">
<?php foreach($group as $option): ?>
<option data-proj_id="<?php echo $option['proj_id']; ?>" value="<?php echo $option['proj_name']; ?>"> <?php echo $option['proj_name']; ?></option>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>

Add values from multidimensional array to another array

Hello good morning wherever you are :D, i have a lil problem, i have this code of arrays
$arrayToView is the info of every single user that i want.
$tagsArray are only tags that use every user but i need to merge all the info something like the last array...
$arrayToView = array(
'IVOFACUNDO' = array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23
),
'ESRAYCU' = array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44
)
)
And i have another one like this
$tagsArray= array(
'IVOFACUNDO' = array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
),
'ESRAYCU' = array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
)
so the question is how i can merge both arrays obviously respectively with the same admin something like this
$arrayToView = array(
'IVOFACUNDO' = array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23,
'tags' => array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
),
'ESRAYCU' = array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44,
'tags' => array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
)
)
The key 'tags' need to be created in the merge of every iteration to add and get one array with all the values, how i can do this?
You can try this snippet.
foreach($arrayToView as $key => $arr){
if(array_key_exists($key, $tagsArray)){
$arrayToView[$key]['tags'] = $tagsArray[$key];
}
}
echo '<pre>';print_r($arrayToView);echo '</pre>';
Use php inbuilt function
$result_Arr = array_merge_recursive($arrayToView,$tagsArray);
<?php
$arrayToView = array(
'IVOFACUNDO' => array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23
),
'ESRAYCU' => array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44
)
);
$tagsArray= array(
'IVOFACUNDO' => array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
),
'ESRAYCU' => array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
);
foreach($arrayToView as $key => $value){
if(isset($tagsArray[$key])){
$arrayToView[$key]['tags'] = array();
foreach($tagsArray[$key] as $key2 => $value2){
$arrayToView[$key]['tags'][$key2] = $tagsArray[$key][$key2];
}
}
}
echo'<pre>';
print_r($arrayToView);
echo'</pre>';
?>

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 )

Loop into an array of array with PHP

I have this array,
$bookings[] = array(
'booking_id' => '1',
'client_name' => 'John',
'client_firstname' => 'Peter',
'days' => array(
array(
'day_id' => '2016-11-23',
'room_id' => '2'
),
array(
'day_id' => '2016-11-24',
'room_id' => '2'
),
array(
'day_id' => '2016-11-25',
'room_id' => '4'
)
)
)
I'm looking for looping into the days array with PHP.
How is it possible please ?
Thanks for your help.
demo
<?php
$bookings[] = array(
'booking_id' => '1',
'client_name' => 'John',
'client_firstname' => 'Peter',
'days' => array(
array(
'day_id' => '2016-11-23',
'room_id' => '2'
),
array(
'day_id' => '2016-11-24',
'room_id' => '2'
),
array(
'day_id' => '2016-11-25',
'room_id' => '4'
)
)
);
foreach($bookings as $booking)
{
foreach($booking['days'] as $day)
{
echo $day['day_id'];
echo "\n";
}
}

cakephp manually building arrays

I have a relationship where a quote habtm applicants. I am trying to get a quote to save with multiple applicants at once. I already have an array of the applicants I need but I don't know how to format that array to get it to save when I insert it into the quote array.
The applicant array looks like this:
array(
(int) 0 => array(
'Applicant' => array(
'id' => '436',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'george',
'middle_name' => 'a',
'surname' => 'summerlane',
'email' => 'email#q.com',
'landline_number' => '88465120.',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:43:10',
'modified' => '2013-09-21 01:43:10'
)
),
(int) 1 => array(
'Applicant' => array(
'id' => '435',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'mary',
'middle_name' => 's',
'surname' => 'amnn',
'email' => 'some#this.cin',
'landline_number' => '465132',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:41:48',
'modified' => '2013-09-21 01:41:48'
)
),
(int) 2 => array(
'Applicant' => array(
'id' => '66',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => null,
'title' => null,
'first_name' => 'Tania',
'middle_name' => '',
'surname' => 'Humphreys',
'email' => 'purple67#me.com',
'landline_number' => null,
'mobile_number' => '0438854355',
'applicant_type' => 'Main applicant',
'created' => '2012-10-29 00:00:00',
'modified' => '2012-10-21 00:00:00'
)
)
)
I need one that looks like this:
array(
'Applicants' => array(
'id' => 435,
'id' => 436,
'id' => 66
)
)
How might I go about doing this?
Or is there a better way?
When I save a quote the array looks like this:
array(
'QuoteButton' => 'Submit',
'Quote' => array(
'date' => array(
'day' => '13',
'month' => '10',
'year' => '2013'
),
'description' => '',
'quote_accepted' => '0',
'research_accepted' => '0',
'cc_accepted' => '0',
'pesel_accepted' => '0',
'setfees_accepted' => '0',
'total' => '0'
),
'Applicant' => array(
'id' => '66'
),
How do I insert more than one applicant into the array?
An array can't have the same id, but can crate another array like this:
$datas = array(
(int) 0 => array(
'Applicant' => array(
'id' => '436',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'george',
'middle_name' => 'a',
'surname' => 'summerlane',
'email' => 'email#q.com',
'landline_number' => '88465120.',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:43:10',
'modified' => '2013-09-21 01:43:10'
)
),
(int) 1 => array(
'Applicant' => array(
'id' => '435',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'mary',
'middle_name' => 's',
'surname' => 'amnn',
'email' => 'some#this.cin',
'landline_number' => '465132',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:41:48',
'modified' => '2013-09-21 01:41:48'
)
),
(int) 2 => array(
'Applicant' => array(
'id' => '66',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => null,
'title' => null,
'first_name' => 'Tania',
'middle_name' => '',
'surname' => 'Humphreys',
'email' => 'purple67#me.com',
'landline_number' => null,
'mobile_number' => '0438854355',
'applicant_type' => 'Main applicant',
'created' => '2012-10-29 00:00:00',
'modified' => '2012-10-21 00:00:00'
)
)
);
$ids = array();
foreach($datas as $data => $applicants) {
$ids[] = $applicants['Applicant']['id'];
}
print_r($ids);
Output:
Array ( [0] => 436 [1] => 435 [2] => 66 )
How to use the ids? Like this:
foreach($ids as $key => $id) {
// do whatever you want with the applicant id
}
like tttony pointed out, array indexes need to be unique so i would:
$ids = array();
foreach($array as $applicant)
$ids[$applicant['Applicant']['id']] = null;
which will output:
Array
(
[436] =>
[435] =>
[66] =>
)
now id is key to $ids array...you could add something else as value rather than null

Categories