Multidimensional array values with RecursiveArrayIterator for select option groups - php

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>

Related

Filter and re-format a given multi-dimensional array (php)

I have been given a dataset by my tutor and asked to carry out the following:
// make a function
// get skus of which the figure of the average monthly sales unit is equal or greater than 350
// array in the following format:
[
'id' => 11102,
'sku' => 'TEST_3',
'alternates' => [
'asin' => [
'code' => '50',
'value' => 'JL1235',
],
'barcode' => [
'code' => '10',
'value' => 'JS160694',
],
],
'commodityCode' => '9989898889',
'price' => [
'value' => 145.99,
],
'instructions' => [
'picking' => [
'code' => 'PICK',
'value' => 'I\'VE JUST HAD AN UNHAPPY LOVE AFFAIR, SO I DON\'T SEE WHY ANYBODY ELSE SHOULD HAVE A GOOD TIME.',
],
],
'quantity' => [
'inner' => 100,
'masterCarton' => 200,
'pallet' => 300,
],
'averageMonthlyUnitSales' => 100,
'created_at' => '2022-03-13 04:14:12'
],
Example dataset is here:
$data = [
[
'id' => '9947',
'sku' => 'test_1',
'asin_code' => '50',
'asin_value' => '',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '120.50',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => '',
'qty_inner' => '120',
'qty_masterCarton' => '200',
'qty_pallet' => '1200',
'averageMonthlyUnitSales' => '750',
'created_at' => '2019-02-23T01:54:14.957299+00:00'
],
[
'id' => '10921',
'sku' => 'test_2',
'asin_code' => '50',
'asin_value' => 'bx12345',
'barcode' => '10',
'barcode_value' => 'jb170931',
'commodityCode' => '9989898889',
'price' => '20.59',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'It\'s only half completed, I\'m afraid',
'qty_inner' => '70',
'qty_masterCarton' => '250',
'qty_pallet' => '270',
'averageMonthlyUnitSales' => '120',
'created_at' => '2021-12-23T11:41:31.193982+00:00'
],
[
'id' => '11102',
'sku' => 'test_3',
'asin_code' => '50',
'asin_value' => 'jl1235',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '145.99',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'I\'ve just had an unhappy love affair, so I don\'t see why anybody else should have a good time.',
'qty_inner' => '100',
'qty_masterCarton' => '200',
'qty_pallet' => '300',
'averageMonthlyUnitSales' => '100',
'created_at' => '2022-03-13T04:14:12.11.093745 +00:00'
],
];
I can perform the first part of the assignment (filter for averageMonthlyUnitSales>350) by carrying out something like:
$filtered_array= array_filter($data, function($item){
return ($item['averageMonthlyUnitSales']>350);
});
But I am not too sure how I can go about getting a new array in the required format?
The only way I can see to simply do both is as follows.
$data = [
[
'id' => '9947',
'sku' => 'test_1',
'asin_code' => '50',
'asin_value' => '',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '120.50',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => '',
'qty_inner' => '120',
'qty_masterCarton' => '200',
'qty_pallet' => '1200',
'averageMonthlyUnitSales' => '750',
'created_at' => '2019-02-23T01:54:14.957299+00:00'
],
[
'id' => '10921',
'sku' => 'test_2',
'asin_code' => '50',
'asin_value' => 'bx12345',
'barcode' => '10',
'barcode_value' => 'jb170931',
'commodityCode' => '9989898889',
'price' => '20.59',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'It\'s only half completed, I\'m afraid',
'qty_inner' => '70',
'qty_masterCarton' => '250',
'qty_pallet' => '270',
'averageMonthlyUnitSales' => '120',
'created_at' => '2021-12-23T11:41:31.193982+00:00'
],
[
'id' => '11102',
'sku' => 'test_3',
'asin_code' => '50',
'asin_value' => 'jl1235',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '145.99',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'I\'ve just had an unhappy love affair, so I don\'t see why anybody else should have a good time.',
'qty_inner' => '100',
'qty_masterCarton' => '200',
'qty_pallet' => '300',
'averageMonthlyUnitSales' => '100',
'created_at' => '2022-03-13T04:14:12.11.093745 +00:00'
],
];
$new = [];
foreach ($data as $line){
if ( $line['averageMonthlyUnitSales'] > 350 ){
// reformat the array
$new = [
'id' => $line['id'],
'sku' => $line['sku'],
'alternates' => ['asin' => ['code'=>$line['asin_code'], 'value'=>$line['asin_value'] ],
'barcode' => ['code'=> $line['barcode'], 'value' => $line['barcode_value']]
],
'commodityCode' => $line['commodityCode'],
'price' => [ 'value' => $line['price'] ],
'instructions' => ['picking' => ['code' => $line['picking_instruction_code'], 'value'=> $line['picking_instruction_value']]
],
'quantity' =>['inner' =>$line['qty_inner'], 'masterCarton' =>$line['qty_masterCarton'], 'pallet'=>$line['qty_pallet']],
'averageMonthlyUnitSales'=>$line['averageMonthlyUnitSales'],
'created_at'=>$line['averageMonthlyUnitSales']
];
}
}
print_r($new);
RESULT
PHP 8.1.3
Array
(
[id] => 9947
[sku] => test_1
[alternates] => Array
(
[asin] => Array
(
[code] => 50
[value] =>
)
[barcode] => Array
(
[code] => 10
[value] => js160694
)
)
[commodityCode] => 9989898889
[price] => Array
(
[value] => 120.50
)
[instructions] => Array
(
[picking] => Array
(
[code] => PICK
[value] =>
)
)
[quantity] => Array
(
[inner] => 120
[masterCarton] => 200
[pallet] => 1200
)
[averageMonthlyUnitSales] => 750
[created_at] => 750
)

How to create an ARRAY in an ARRAY

I have this code (created from the admin side) in my wordpress Database postmeta table.
a:28:{s:12:"featuredItem";s:1:"0";s:10:"headerType";s:5:"image";s:11:"headerImage";s:60:"https://rocks---.de/wp-content/uploads/2018/08/IMG_2051.jpg";s:16:"headerImageAlign";s:10:"image-left";s:3:"map";a:7:{s:7:"address";s:6:"Goslar";s:8:"latitude";s:10:"51.9059531";s:9:"longitude";s:18:"10.428996299999994";s:10:"streetview";s:1:"0";s:9:"swheading";s:2:"90";s:7:"swpitch";s:1:"5";s:6:"swzoom";s:1:"1";}s:9:"telephone";s:4:"0221";s:19:"telephoneAdditional";a:3:{i:0;a:1:{s:6:"number";s:12:"111111111111";}i:1;a:1:{s:6:"number";s:13:"2222222222222";}i:2;a:1:{s:6:"number";s:13:"3333333333333";}}s:5:"email";s:20:"goslarEmail#email.de";s:9:"showEmail";s:1:"1";s:15:"contactOwnerBtn";s:1:"1";s:3:"web";s:27:"https://musicheadquarter.de";s:12:"webLinkLabel";s:17:"Label of the Link";s:19:"displayOpeningHours";s:1:"1";s:18:"openingHoursMonday";s:2:"12";s:19:"openingHoursTuesday";s:2:"12";s:21:"openingHoursWednesday";s:2:"12";s:20:"openingHoursThursday";s:2:"12";s:18:"openingHoursFriday";s:2:"12";s:20:"openingHoursSaturday";s:2:"12";s:18:"openingHoursSunday";s:2:"12";s:16:"openingHoursNote";s:26:"Text zu den Öffnungsziten";s:18:"displaySocialIcons";s:1:"1";s:26:"socialIconsOpenInNewWindow";s:1:"1";s:11:"socialIcons";a:2:{i:0;a:4:{s:5:"image";s:0:"";s:4:"icon";s:18:"fa-facebook-square";s:9:"iconColor";s:7:"#00FF00";s:4:"link";s:22:"http://rocks---.de";}i:1;a:4:{s:5:"image";s:0:"";s:4:"icon";s:12:"fa-instagram";s:9:"iconColor";s:7:"#00FF00";s:4:"link";s:20:"https://rocks---.de";}}s:14:"displayGallery";s:1:"1";s:7:"gallery";a:3:{i:0;a:2:{s:5:"title";s:6:"Bild 1";s:5:"image";s:60:"https://rocks---.de/wp-content/uploads/2019/01/IMG_8797.jpg";}i:1;a:2:{s:5:"title";s:6:"Bild 2";s:5:"image";s:60:"https://rocks---.de/wp-content/uploads/2019/01/IMG_8443.jpg";}i:2;a:2:{s:5:"title";s:6:"Bild 3";s:5:"image";s:60:"https://rocks---.de/wp-content/uploads/2018/08/IMG_8545.jpg";}}s:15:"displayFeatures";s:1:"1";s:8:"features";a:2:{i:0;a:3:{s:4:"icon";s:0:"";s:4:"text";s:15:"Feature Titel 1";s:4:"desc";s:28:"Feature Titel Beschreibung 1";}i:1;a:3:{s:4:"icon";s:0:"";s:4:"text";s:15:"Feature Titel 2";s:4:"desc";s:28:"Feature Titel Beschreibung 2";}}}
Now I tried to create the code for this ARRAY or what it is called. Everything works fine, exept the ARRAY in an ARRAY thing. Like, the list of social icons, image galelry and telephone numbers.
This is what I have so far:
$data = array(
'subtitle' => 'Utertitel',
'featuredItem' => '0',
'headerType' => 'image',
'headerImage' => 'https://rocks---.de/wp-content/uploads/slide3___beach-2179183_1920.jpg',
'headerHeight' => '375',
'map' => array(
'address' => $adresse,
'latitude' => $lat,
'longitude' => $lng,
'streetview' => '0',
'swheading' => '90',
'swpitch' => '5',
'swzoom' => '14'
),
'telephone' => '0221',
'telephoneAdditional' => '0228',
'email' => 'meister#rocks---.de',
'showEmail' => '1',
'contactOwnerBtn' => '1',
'web' => 'https://musicheadquarter.de',
'webLinkLabel' => '',
'displayOpeningHours' => '1',
'openingHoursMonday' => '12-17',
'openingHoursTuesday' => '12-17',
'openingHoursWednesday' => '12-17',
'openingHoursThursday' => '12-17',
'openingHoursFriday' => '12-17',
'openingHoursSaturday' => '12-17',
'openingHoursSunday' => '12-17',
'openingHoursNote' => 'Zusatzinfos zu den Öffnungszeiten',
'displaySocialIcons' => '0',
'socialIconsOpenInNewWindow' => '1',
'socialIcons' => array(
'icon' => 'fa-facebook-square',
'link' => 'https://rocks---.de'
),
'displayGallery' => '0',
'gallery' => array(
'title' => 'Titel des Bildes',
'image' => 'https://rocks---.de/wp-content/uploads/slide3___beach-2179183_1920.jpg'
),
'displayFeatures' => '0',
'features' => '0'
);
/* $data = maybe_serialize( $data ); */
update_post_meta( $post_id, '_ait-item_item-data', $data );
HOw do I create the ARRAY for the Images, social icons and telephone numbers.
I unserialized the Field Data and it shows the ARRAY, great!
array (
'featuredItem' => '0',
'headerType' => 'image',
'headerImage' => 'https://rockspots.de/wp-content/uploads/2018/08/IMG_2051.jpg',
'headerImageAlign' => 'image-left',
'map' =>
array (
'address' => 'Goslar',
'latitude' => '51.9059531',
'longitude' => '10.428996299999994',
'streetview' => '0',
'swheading' => '90',
'swpitch' => '5',
'swzoom' => '1',
),
'telephone' => '0221',
'telephoneAdditional' =>
array (
0 =>
array (
'number' => '111111111111',
),
1 =>
array (
'number' => '2222222222222',
),
2 =>
array (
'number' => '3333333333333',
),
),
'email' => 'goslarEmail#email.de',
'showEmail' => '1',
'contactOwnerBtn' => '1',
'web' => 'https://musicheadquarter.de',
'webLinkLabel' => 'Label of the Link',
'displayOpeningHours' => '1',
'openingHoursMonday' => '12',
'openingHoursTuesday' => '12',
'openingHoursWednesday' => '12',
'openingHoursThursday' => '12',
'openingHoursFriday' => '12',
'openingHoursSaturday' => '12',
'openingHoursSunday' => '12',
'openingHoursNote' => 'Text zu den Öffnungsziten',
'displaySocialIcons' => '1',
'socialIconsOpenInNewWindow' => '1',
'socialIcons' =>
array (
0 =>
array (
'image' => '',
'icon' => 'fa-facebook-square',
'iconColor' => '#00FF00',
'link' => 'http://festivaldate.de',
),
1 =>
array (
'image' => '',
'icon' => 'fa-instagram',
'iconColor' => '#00FF00',
'link' => 'https://rockspots.de',
),
),
'displayGallery' => '1',
'gallery' =>
array (
0 =>
array (
'title' => 'Bild 1',
'image' => 'https://rockspots.de/wp-content/uploads/2019/01/IMG_8797.jpg',
),
1 =>
array (
'title' => 'Bild 2',
'image' => 'https://rockspots.de/wp-content/uploads/2019/01/IMG_8443.jpg',
),
2 =>
array (
'title' => 'Bild 3',
'image' => 'https://rockspots.de/wp-content/uploads/2018/08/IMG_8545.jpg',
),
),
'displayFeatures' => '1',
'features' =>
array (
0 =>
array (
'icon' => '',
'text' => 'Feature Titel 1',
'desc' => 'Feature Titel Beschreibung 1',
),
1 =>
array (
'icon' => '',
'text' => 'Feature Titel 2',
'desc' => 'Feature Titel Beschreibung 2',
),
),
)
Now I need to know, how to create this part of the array. The number of images for the gallery is not fixed.
'gallery' =>
array (
0 =>
array (
'title' => 'Bild 1',
'image' => 'https://rockspots.de/wp-content/uploads/2019/01/IMG_8797.jpg',
),
1 =>
array (
'title' => 'Bild 2',
'image' => 'https://rockspots.de/wp-content/uploads/2019/01/IMG_8443.jpg',
),
2 =>
array (
'title' => 'Bild 3',
'image' => 'https://rockspots.de/wp-content/uploads/2018/08/IMG_8545.jpg',
),
),
Thanks for some tips,
Denis

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

How to sort this array by date?

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

Categories