php type cast and array reference - php

I am facing a problem in PHP OOPS code.
My code is:
class Settings
{
private $client_addr = array(
'ClientID' => array('maxlength'=>'10','IsNull'=>'n'),
'ClientAddressType' => array('maxlength'=>'12','IsNull'=>'y'),
'ClientAddressLine1' => array('maxlength'=>'30','IsNull'=>'y'),
'ClientAddressLine2' => array('maxlength'=>'30','IsNull'=>'y'),
'ClientCounty' => array('maxlength'=>'30','IsNull'=>'y'),
'ClientCity' => array('maxlength'=>'30','IsNull'=>'y'),
'ClientState' => array('maxlength'=>'2','IsNull'=>'y'),
'ClientZip' => array('maxlength'=>'9','IsNull'=>'y'),
);
private $client_general = array(
'PayerID' => array('maxlength'=>'64','IsNull'=>'n'),
'ProviderID' => array('maxlength'=>'50','IsNull'=>'n'),
'ClientID' => array('maxlength'=>'10','IsNull'=>'n'),
'ClientFirstName' => array('maxlength'=>'30','IsNull'=>'n'),
'ClientMiddleInitial' => array('maxlength'=>'1','IsNull'=>'y'),
'ClientLastName' => array('maxlength'=>'30','IsNull'=>'n'),
);
function getSelectedArrayData($setlected_arr)
{
$setlected_arr = '$this->'."$setlected_arr";
print_r($setlected_arr); //it prints a string '$this->client_general'
print_r($this->client_general);//it prints $client_general array data
}
}
$settings = new Settings();
$settings->getSelectedArrayData('client_general');
My problem is:
When I print print_r($this->client_general); it's printed $client_general array that is okay.
Array
(
[PayerID] => Array
(
[maxlength] => 64
[IsNull] => n
)
[ProviderID] => Array
(
[maxlength] => 50
[IsNull] => n
)
[ClientID] => Array
(
[maxlength] => 10
[IsNull] => n
)
[ClientFirstName] => Array
(
[maxlength] => 30
[IsNull] => n
)
[ClientMiddleInitial] => Array
(
[maxlength] => 1
[IsNull] => y
)
[ClientLastName] => Array
(
[maxlength] => 30
[IsNull] => n
)
)
When I print print_r($setlected_arr); It's printed
$this->client_general
I wnat that it should also point to the $client_general array.
How I can do it?

print_r($this->$setlected_arr);

Try this, Its for you.
class Settings {
private $client_addr = array(
'ClientID' => array('maxlength' => '10', 'IsNull' => 'n'),
'ClientAddressType' => array('maxlength' => '12', 'IsNull' => 'y'),
'ClientAddressLine1' => array('maxlength' => '30', 'IsNull' => 'y'),
'ClientAddressLine2' => array('maxlength' => '30', 'IsNull' => 'y'),
'ClientCounty' => array('maxlength' => '30', 'IsNull' => 'y'),
'ClientCity' => array('maxlength' => '30', 'IsNull' => 'y'),
'ClientState' => array('maxlength' => '2', 'IsNull' => 'y'),
'ClientZip' => array('maxlength' => '9', 'IsNull' => 'y'),
);
private $client_general = array(
'PayerID' => array('maxlength' => '64', 'IsNull' => 'n'),
'ProviderID' => array('maxlength' => '50', 'IsNull' => 'n'),
'ClientID' => array('maxlength' => '10', 'IsNull' => 'n'),
'ClientFirstName' => array('maxlength' => '30', 'IsNull' => 'n'),
'ClientMiddleInitial' => array('maxlength' => '1', 'IsNull' => 'y'),
'ClientLastName' => array('maxlength' => '30', 'IsNull' => 'n'),
);
function getSelectedArrayData($setlected_arr) {
$setlected_arr = ${'this'}->${'setlected_arr'};
print_r($setlected_arr); //it prints a string '$this->client_general'
echo '<br/>';
echo '<br/>';
print_r($this->client_general); //it prints $client_general array data
}
}
$settings = new Settings();
$settings->getSelectedArrayData('client_general');
Example for you is to create dynamic variable by string is here:
${'a' . 'b'} = 'hello there';
echo $ab; // hello there
My compiled output:

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>

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

combining two arrays merge the same data add the new data

retrieve data from database :
$db = array (
0 =>
array (
'stay' => '10',
'end' => '2015-12-29',
'start' => '2015-12-20',
'pid' => '231096236',
'normal_price' => '385',
'promo_price' => '385',
'resell_price' => '385',
),
1 =>
array (
'stay' => '7',
'end' => '2016-01-05',
'start' => '2015-12-30',
'pid' => '231096235',
'normal_price' => '385',
'promo_price' => '385',
'resell_price' => '385',
),
2 =>
array (
'stay' => '3',
'end' => '2053-01-01',
'start' => '2016-01-06',
'pid' => '231096234',
'normal_price' => '385',
'promo_price' => '385',
'resell_price' => '385',
),
),
new array to merge with:
$new = array (
0 =>
array (
'stay' => '2',
'end' => '2015-07-01',
'start' => '2015-06-03',
'normal_price' => '145',
'promo_price' => '145',
'resell_price' => '135',
),
1 =>
array (
'stay' => '4',
'end' => '2015-07-05',
'start' => '2015-07-02',
'pid' => '231096235',
'normal_price' => '100',
'promo_price' => '100',
'resell_price' => '100',
),
2 =>
array (
'stay' => '4',
'end' => '2015-09-03',
'start' => '2015-07-06',
'pid' => '231096236',
'normal_price' => '100',
'promo_price' => '100',
'resell_price' => '100',
),
),
expected result:
$expected = array (
0 =>
array (
'stay' => '2',
'end' => '2015-07-01',
'start' => '2015-06-03',
'normal_price' => '145',
'promo_price' => '145',
'resell_price' => '135',
),
1 =>
array (
'stay' => '4',
'end' => '2015-07-05',
'start' => '2015-07-02',
'pid' => '231096235',
'normal_price' => '100',
'promo_price' => '100',
'resell_price' => '100',
),
2 =>
array (
'stay' => '4',
'end' => '2015-09-03',
'start' => '2015-07-06',
'pid' => '231096236',
'normal_price' => '100',
'promo_price' => '100',
'resell_price' => '100',
),
3 =>
array (
'stay' => '3',
'end' => '2053-01-01',
'start' => '2016-01-06',
'pid' => '231096234',
'normal_price' => '385',
'promo_price' => '385',
'resell_price' => '385',
),
),
my result so far:
$merge = array (
0 =>
array (
'stay' => '2',
'end' => '2015-07-01',
'start' => '2015-06-03',
'normal_price' => '145',
'promo_price' => '145',
'resell_price' => '135',
),
1 =>
array (
'stay' => '4',
'end' => '2015-07-05',
'start' => '2015-07-02',
'pid' => '231096235',
'normal_price' => '100',
'promo_price' => '100',
'resell_price' => '100',
),
2 =>
array (
'stay' => '4',
'end' => '2015-09-03',
'start' => '2015-07-06',
'pid' => '231096236',
'normal_price' => '100',
'promo_price' => '100',
'resell_price' => '100',
),
),
these are the requirements:
if record in new array does not has pid (price id) then it's
considered as new data. (record with start : 2015-06-03 and end :
2015-06-03)
if record in new array has pid and match with one of record from table, then new record's values will override the old one (record with pid : 231096235 and pid : 231096236)
if record from database has pid and does not match with any pid from new array then it should be added to new array (it shouldb record with pid : 231096234)
my functions :
$merge = array();
foreach ($new as $ikey => $ival) {
$ispid = (isset($ival['pid'])) ? (int) $ival['pid'] : 0;
if ($ispid === 0) {
$merge[] = $ival;
}
if ($ispid > 0) {
$exist = false;
foreach ($db as $rkey => $rval) {
$rspid = (int) $rval['pid'];
if ($ispid === $rspid) {
$spid_exist = true;
overridePriceValue($ival, $rval);
$merge[] = $rval;
}
}
// if ($exist === false) {
// echo "price id " . $ispid . " is not match with any special prices.";
// }
}
}
function overridePriceValue($input_spc_price, &$spc_price) {
if (isset($input_spc_price['stay'])) {
$spc_price['stay'] = $input_spc_price['stay'];
}
if (isset($input_spc_price['end'])) {
$spc_price['end'] = $input_spc_price['end'];
}
if (isset($input_spc_price['start'])) {
$spc_price['start'] = $input_spc_price['start'];
}
if (isset($input_spc_price['normal_price'])) {
$spc_price['normal_price'] = $input_spc_price['normal_price'];
}
if (isset($input_spc_price['promo_price'])) {
$spc_price['promo_price'] = $input_spc_price['promo_price'];
}
if (isset($input_spc_price['resell_price'])) {
$spc_price['resell_price'] = $input_spc_price['resell_price'];
}
}
I need your advise to achieve the goal. thanks :)
You can use the UDF in_array_r from another stackoverflow question (thanks to jwueller) and do it like this:
$merge = $new;
foreach($db as $item) {
if (!in_array_r($item["pid"], $new)) {
$merge[] = $item;
}
}
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}

how to build an array in parent and child manner by fetching mysql data using php

this is my code where i am fetching my mysql record.
$parentChildArr=array();
//mysql query for fetching parent and child record
$selectparentMenu=mysql_query("SELECT `id`,`item_name`,`menu_type`,`parent` FROM `epic_master_menu`");
if(mysql_num_rows($selectparentMenu)>1) {
while($fetchparentMenu=mysql_fetch_array($selectparentMenu)) {
$parentChildArr[]=$fetchparentMenu['id'];
$parentChildArr[]=$fetchparentMenu['item_name'];
$parentChildArr[]=$fetchparentMenu['menu_type'];
$parentChildArr[]=$fetchparentMenu['parent'];
}
var_export($parentChildArr); // exporting or printing arrays
// when i export the array i get this output.
array ( 0 => '1', 1 => 'Dashboard', 2 => 'item', 3 => '0',
4 => '2', 5 => 'Admission', 6 => 'item', 7 => '0', 8 => '3',
9 => 'Examination', 10 => 'item', 11 => '0', 12 => '4',
13 => 'CET', 14 => 'item', 15 => '0');
but the problem is that i want to build the array like this.
$newarr=array ( 'dataSource' => array ( 0 => array ( 'id' => '1',
'text' => 'Dashboard', 'expanded' => 'true', 'spriteCssClass' => 'rootfolder',
'items' => array ( 0 => array ( 'id' => '89', 'text' => 'Users',
'expanded' => true, 'spriteCssClass' => 'folder',
'items' => array ( 0 => array ( 'id' => '94', 'text' => 'Users',
'spriteCssClass' => 'html', ), 1 => array ( 'id' => '94',
'text' => 'Users', 'spriteCssClass' => 'html', ), 2 => array (
'id' => '94', 'text' => 'Users', 'spriteCssClass' => 'image' ) ) ) ) ) ));
database table view is...
i am not getting the logic that how to build the array like $newarr. thank you
not tested, but you need to defined a structure and then you need to go recursive... so you can check where is the parent entry to my child ...
function structure($data){
return array(
'id' => $data['id'],
'item_name' => $data['item_name'],
'menu_type' => $data['menu_type'],
'parent' => $data['parent'],
'expanded' => false,
'childs' => array()
);
}
function recursiveImport(&$parent, $data){
foreach($parent AS $key => $value){
if($value['id'] == $data['parent']){
$parent[$key]['childs'][] = structure($data);
$parent[$key]['expanded'] = true;
return true;
}
else{
if(count($value['childs']) > 0){
foreach($value['childs'] AS $key2 => $child){
$result = recursiveImport($parent[$key]['childs'][$key2], $data);
if($result === true) return true;
}
}
}
}
return false;
}
$newarr = array();
while($fetchparentMenu=mysql_fetch_array($selectparentMenu)) {
$result = recursiveImport($newarr , $fetchparentMenu);
if($result === false){
$newarr[] = structure($fetchparentMenu);
}
}
//output array
array ( 0 => array ( 'id' => '1', 'item_name' => 'Dashboard', 'menu_type' => 'item', 'parent' => '0', 'expanded' => false, 'childs' => array ( ), ), 1 => array ( 'id' => '2', 'item_name' => 'Admission', 'menu_type' => 'item', 'parent' => '0', 'expanded' => false, 'childs' => array ( ), ), 2 => array ( 'id' => '3', 'item_name' => 'Examination', 'menu_type' => 'item', 'parent' => '0', 'expanded' => false, 'childs' => array ( ), ), 3 => array ( 'id' => '4', 'item_name' => 'CET', 'menu_type' => 'item', 'parent' => '0', 'expanded' => false, 'childs' => array ( ), )
Use following code
$m=0;
if(mysql_num_rows($selectparentMenu)>1) {
while($fetchparentMenu=mysql_fetch_array($selectparentMenu)) {
$parentChildArr[$m][]=$fetchparentMenu['id'];
$parentChildArr[$m][]=$fetchparentMenu['item_name'];
$parentChildArr[$m][]=$fetchparentMenu['menu_type'];
$parentChildArr[$m][]=$fetchparentMenu['parent'];
$m++;
}

Symfony 2 + SoapClient

When i try to send this request then i get error "id: 0 500 Internal Server Error - SoapFault".
My request:
$client = new SoapClient( 'xxx', array('trace' => 1));
$client -> soap_defencoding = 'utf-8';
$client -> decode_utf8 = FALSE;
$a = array( 'user_name' => 'xx', 'user_password' => 'xx' );
$response = $client->__soapCall( 'adeLogin', $a );
$session = get_object_vars($response)['session'];
$aCPrep = array(
'rname1' => 'Pan',
'rname2' => 'Jan',
'rname3' => 'test',
'rcountry' => 'PL',
'rzipcode' => '00-950',
'rcity' => 'Warszawa',
'rstreet' => 'ul. Kwieciska 15',
'quantity' => '1',
'rphone' => '1500-100-900',
'rcontact' => 'Antoni',
'weight' => '100',
'date' => '2012-01-01',
'references' => 'by WebAPI ',
'notes' => 'by Thomi (thest 4)',
'srv_ade' => 'asd',
'pfc' => 'Magazyn',
'sendaddr' => array( 'name1' => 'Firma z Francji', 'name2' => 'firma', 'name3' => 'firma2','country' => 'FR', 'zipcode' => '14117', 'city' => 'Arromanches', 'street' => 'Pl. du Six Juin 1944' ),
'srv_daw' => array( 'name' => '','building' => '','floor' => '','room' => '','phone' => '','altrec' => ''),
'srv_ident' => array( 'name' => '','country' => '','zipcode' => '','city' => '','street' => '','nation' => '','date_birth' => '','identity' => '','ident_doctype' => '','spages' => '','ssign' => '','sdealsend' => '','sdealrec' => ''),
'srv_ppe' => array( 'sname1' => '','sname2' => '','sname3' => '','scountry' => '','szipcode' => '','scity' => '','sstreet' => '','sphone' => '','scontact' => '','rname1' => '','rname2' => '','rname3' => '','rcountry' => '','rzipcode' => '','rcity' => '','rstreet' => '','rphone' => '','rcontact' => '','references' => '','weight' => ''),
'srv_bool' => array( 'cod' => '1', 'cod_amount' => '200', 'exw' => '0', 'rod' => '0', 'pod' => '0', 'exc' => '0','ident' => '0','daw' => '0','ps' => '0','pr' => '0','s10' => '0','s12' => '0','sat' => '0','ow' => '0','srs' => '0',),
'parcels' => array( '0' => array( 'weight' => '15', 'reference' => 'paczka duza','number' => '','srv_ade' => '', 'srv_bool' => array( 'cod' => '1', 'cod_amount' => '200', 'exw' => '0', 'rod' => '0', 'pod' => '0', 'exc' => '0','ident' => '0','daw' => '0','ps' => '0','pr' => '0','s10' => '0','s12' => '0','sat' => '0','ow' => '0','srs' => '0')))
);
$aCMsg = array( 'session' => $session, 'consign_prep_data' => $aCPrep );
$aClient = $client->__soapCall( 'adePreparingBox_Insert', $aCMsg );
It is error from my soap server or from symfony ? Anybody know what is wrong ?
How can i show the XML ?
it's response from SOAP server
Try
echo "REQUEST:\n".$client->__getLastRequest()."\n";
echo "RESPONSE:\n".$client->__getLastResponse()."\n";

Categories