this is my reply
Array ([code] => 202 [message] => Accepted [data] => Array ( [resultMap] => Array ( [D3856~H158] => Array ( [AppDay] => * [HosTown] => Colombo 06 [SpecName] => Physiotherapist/Sports Physiotherapist [HosName] => Revival Healthcare Services (Pvt)-Colombo [SpecializationId] => 333 [HosCode] => H158 [AppDate] => Any [DocName] => MR CHAMARA MATHANGAWEERA [DoctorNo] => D3856 ) ) ) [detailMessage] => Success )
now i want to assign variable for this vale and echo in proper way i try this
but it is giving a error msg
Undefined index: DocName
this is my code
////////////////////////////////////
if( $response ){
if ( isset($result->error) )die( $result->error_message );
/* Convert json data to array */
$arr=json_decode( $response, true );
//print_r($arr);
foreach($arr['data'] as $data)
{
$output="Doctor".$data['DocName']."<br/>";
$output="Doctor".$data['SpecName']."<br/>";
$output="Doctor".$data['HosName']."<br/>";
$output="Doctor".$data['Day']."<br/>";
$output="Doctor".$data['Date']."<br/>";
}
Have a look at the structure of your array. The values you are looking for are nested withing several arrays:
$data = array (
'code' => 202,
'message' => 'Accepted',
'data' => array (
'resultMap' => array (
'D3856~H158' => array (
'AppDay' => '*',
'HosTown' => 'Colombo 06',
'SpecName' => 'Physiotherapist/Sports Physiotherapist',
'HosName' => 'Revival Healthcare Services (Pvt)-Colombo',
'SpecializationId' => 333,
'HosCode' => 'H158',
'AppDate' => 'Any',
'DocName' => 'MR CHAMARA MATHANGAWEERA',
'DoctorNo' => 'D3856',
)
)
),
'detailMessage' => 'Success'
);
To print all values for the first record in resultMap:
foreach(current($data['data']['resultMap']) as $key => $value) {
echo $key . " => " . $value . "<br />";
}
You need an additional loop in order to print all records in resultMap.
You can try this working fine
for this fixed array values means below given
foreach($arr['data']['resultMap']['D3856~H158'] as $data)
{
$output="Doctor".$data['DocName']."<br/>";
$output="Doctor".$data['SpecName']."<br/>";
$output="Doctor".$data['HosName']."<br/>";
$output="Doctor".$data['Day']."<br/>";
$output="Doctor".$data['Date']."<br/>";
}
or
The changing array values are given the answer
foreach($arr['data']['resultMap'] as $data)
{
$output="Doctor".$data['DocName']."<br/>";
$output="Doctor".$data['SpecName']."<br/>";
$output="Doctor".$data['HosName']."<br/>";
$output="Doctor".$data['Day']."<br/>";
$output="Doctor".$data['Date']."<br/>";
}
Related
I have 2 arrays, I'm trying to find any matches and return 'url from $array_full.
I tried array_intersect($array_full, $array_ids), but it doesn't work.
$array_full = array
(
Array
(
'#attributes' => Array
(
'topicid' => 102000,
'url' => 'Velkommen.htm',
'alias' => 'Velkommen'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130313,
'url' => 'WStation/WAS_Indstillinger.htm',
'alias' => 'WAS_Indstillinger'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130315,
'url' => 'SPedestal/Applikationer/LoadSharing/Indstillinger.htm',
'alias' => 'LOS_Indstillinger'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130312,
'url' => 'WStation/WAS_Indstillinger.htm',
'alias' => 'WAS_Indstillinger'
)
)
);
$array_ids = array('130312', '130315');
I expect to get an array of matched url's, like:
array('WStation/WAS_Indstillinger.htm','SPedestal/Applikationer/LoadSharing/Indstillinger.htm')
A simple couple of foreach loops seems the easiest approach
$results = [];
foreach ( $array_full as $a ) {
foreach ( $a as $item ) {
if ( in_array($item['topicid'], $array_ids) ) {
$results[] = $item['url'];
}
}
}
print_r($results);
RESULT
Array
(
[0] => SPedestal/Applikationer/LoadSharing/Indstillinger.htm
[1] => WStation/WAS_Indstillinger.htm
)
You will have to make foreach inside foreach to find item that is matching to ID.
Something like this (not tested, may contain some typos).
foreach($array_ids as $id) {
foreach($array_full as $key => $fullItem) {
if($fillItem['#attributes']['topicid'] != $id) {
continue;
}
//do what you need with $fullItem array
$key; // this is the key you want
}
}
you can use array_map, in_array to get the URL's
$result = [];
array_map(function($v) use ($array_ids,&$result){
$result[] = in_array($v['#attributes']['topicid'], $array_ids) ? $v['#attributes']['url'] : '';
}, $array_full);
Result:-
echo '<pre>';
print_r(array_filter($result));
Array
(
[2] => SPedestal/Applikationer/LoadSharing/Indstillinger.htm
[3] => WStation/WAS_Indstillinger.htm
)
foreach this array result and echo the results
Array
(
[0] => Array
(
[0] => Array
(
[blog_title] => sooraj bloging
[blog_id] => 2
)
[1] => Array
(
[blog_title] => What are Mobile App Testing Challenges?
[blog_id] => 4
)
[2] => Array
(
[blog_title] => sooraj blog
[blog_id] => 8
)
)
[1] => Array
(
[0] => Array
(
[title] => sooraj casestudy
)
)
[2] => Array
(
[0] => Array
(
[career_id] => 14
[title] => Software Engineer .NET sooraj
[location] => Kochi, India.
[description] => Developing .NET applications.
[qualification] => B.Tech. in CSE, MCA
[status] => 0
[created_at] => 2017-11-20 13:14:29
[updated_at] => 0000-00-00 00:00:00
)
)
[3] => Array
(
)
[4] => Array
(
[0] => Array
(
[tst_id] => 146
[tst_name] => John Kasha
[tst_quote] => Gadgeon was extremely professional and was easy to commun sooraj icate and work with on a day-to-day basis. I also liked the fact that they were willing to do the research for specific tasks and present a viable solution or workaround to keep the project on schedule. I would recommend them for any task for any industry software or hardware. Bottom line, they get it done and you get results, not excuses. VP of Engineering.
[tst_desig] => Vice President,Product Development and Engineering
[tst_image] => 91c09ac9ee6234fdfcc523a393800bd5.jpg
[url] =>
[crop_name] => 668959f965ab28815dc97bbc1f8718d8.jpg
[sysDate] => 2017-11-20 15:42:34
)
)
)
Just Run this code
<?php
$array = array(
array(
array(
'blog_title' => 'sooraj bloging',
'blog_id' => 2
),
array(
'blog_title' => 'What are Mobile App Testing Challenges?',
'blog_id' => 4
),
array(
'blog_title' => 'sooraj blog',
'blog_id' => 8
)
),
array(
array(
'title' => 'sooraj casestudy',
)
),
array(
array(
'career_id' => 14,
'title' => 'Software Engineer .NET sooraj',
'location' => 'Kochi, India.',
'description' => 'Developing .NET applications.',
'qualification' => 'B.Tech. in CSE, MCA',
'status' => 0,
'created_at' => '2017-11-20 13:14:29',
'updated_at' => '0000-00-00 00:00:00'
)
),
array(),
array(
array(
'tst_id' => 146,
'tst_name' => 'John Kasha',
'tst_quote' => 'Gadgeon was extremely professional and was easy to commun sooraj icate and work with on a day-to-day basis. I also liked the fact that they were willing to do the research for specific tasks and present a viable solution or workaround to keep the project on schedule. I would recommend them for any task for any industry software or hardware. Bottom line, they get it done and you get results, not excuses. VP of Engineering.',
'tst_desig' => 'Vice President,Product Development and Engineering',
'tst_image' => '91c09ac9ee6234fdfcc523a393800bd5.jpg',
'url' => '',
'crop_name' => '668959f965ab28815dc97bbc1f8718d8.jpg',
'sysDate' => '2017-11-20 15:42:34'
)
)
);
foreach ($array as $value){
foreach ($value as $row){
foreach ($row as $key=> $row1){
echo $key.' - '. $row1;
}
echo '<br>';
}
}
?>
Depending what you're trying to do (debugging vs tabular display), you can "pretty print" the array with var_export like so:
// Assuming your array is $data
echo '<pre>'.var_export($data, TRUE).'</pre>';
Otherwise, to loop through the array as is with a foreach:
// Assuming your array is $data
foreach ($data as $subdata) {
// You probably want to check that this is an array for case #3
if(is_array($subdata)) {
foreach ($subdata as $valueset) {
// Check for array validity (not required for example data, but good to be safe)
if (is_array($valueset)) {
foreach ($subdata as $key => $value) {
// Print each key, value pair as a row
echo $key .' => '.$value . '<br />';
}
}
}
} else {
// Optional handling of empty set
echo 'No data to display...';
}
}
foreach ($array as $value){
foreach ($value as $row){
if (is_array($row)){
foreach ($row as $key => $val){
echo $key."=>". $val."<br>";
}///endForeach
}///endIf
else {
echo $row;
}////endElse
}
}
regarding in query from the model, i got already the values and store it from a variable, now i want it to be stored from a data array for further use, how can i do this, i am in controller from codeigniter.
Here is my controller full code:
public function move_data($queue_id) {
$data = array();
$user = array('user_id' => $this->session->userdata['logged_in']['user_id']);
$getqueuedata = $this->Clinic_model->queue_data($queue_id,$user);
//echo json_encode($getqueuedata);
$data = array (
'queue_id' => $getqueuedata['queue_id'],
'user_id' => $getqueuedata['user_id'],
'clinic_id' => $getqueuedata['clinic_id'],
'order_num' => $getqueuedata['order_num'],
'patient_id' => $getqueuedata['patient_id'],
);
}
when i //echo json_encode($getqueuedata); uncomment this line, and comment the array storing, i will have this:
[{"user_id":"102","clinic_id":"2","order_num":"1","patient_id":"7","status":"2","time_sched":null,"queue_id":"1"}]
in my full code, i got error, i dont know how to store the values of my query from array.
function 'queue_data` return result like this :
//$getqueuedata = json_decode($json,true);
Array ( [0] => Array ( [user_id] => 102 [clinic_id] => 2 [order_num] => 1 [patient_id] => 7 [status] => 2 [time_sched] => [queue_id] => 1 ) )
So you can store data as this way:
$data = array (
'queue_id' => $getqueuedata[0]['queue_id'],
'user_id' => $getqueuedata[0]['user_id'],
'clinic_id' => $getqueuedata[0]['clinic_id'],
'order_num' => $getqueuedata[0]['order_num'],
'patient_id' => $getqueuedata[0]['patient_id'],
);
print_r($getqueuedata);
If your function return array object:
$data = array (
'queue_id' => $getqueuedata[0]->queue_id,
'user_id' => $getqueuedata[0]->user_id,
'clinic_id' => $getqueuedata[0]->clinic_id,
'order_num' => $getqueuedata[0]->order_num,
'patient_id' => $getqueuedata[0]->patient_id,
);
I have data like this :
[1] => Array
(
[COMPANY_SERVICE_ID] => CS01
[COMPANY_NAME] => HANOMAN SAKTI PRATAMA, PT - JAKARTA
[TARIFF_CURRENCY] => IDR
[SELLING_SERVICE_ID] => SS01
[CONTAINER_TYPE_ID] => DC
[SERVICE_NAME] => CONTAINER TRUCKING SERVICE
[FROM_QTY] => 1
[TO_QTY] => 100
[FROM_NAME] => TANJUNG PRIOK
[FROM_LOCATION_ID] => L096
[TO_NAME] => BALARAJA
[TO_LOCATION_ID] => L002
[RESULT_LOCATION] => Array
(
[L001] => Array
(
[TARIF_20] => 1.500.000,00
[TARIF_40] => 1.750.000,00
[TARIF_45] => 5.500.000,00
[TARIF_4H] => 3.500.000,00
)
[L002] => Array
(
[TARIF_20] => 500.000,00
)
)
)
i wanna unset value of RESULT_LOCATION if value in RESULT_LOCATION different with value of TO_LOCATION_ID.
What should i do?
i try to remove with code like this :
foreach ($hasil_jakarta as $key => $value) {
foreach ($value['RESULT_LOCATION'] as $key1 => $value1) {
if ($value['TO_LOCATION_ID'] != $value['RESULT_LOCATION'][$key1]) {
unset($hasil_jakarta[$key]['RESULT_LOCATION'][$key1]);
}
}
}
but all value of RESULT_LOCATION deleted. Whats wrong with my code?
You don't have to test the value but the key. Then, unset() arrays with different keys.
Here is an example :
<?php
$a = array(
'COMPANY_SERVICE_ID' => 'CS01',
'COMPANY_NAME' => 'HANOMAN SAKTI PRATAMA, PT - JAKARTA',
'TARIFF_CURRENCY' => 'IDR',
'SELLING_SERVICE_ID' => 'SS01',
'CONTAINER_TYPE_ID' => 'DC',
'SERVICE_NAME' => 'CONTAINER TRUCKING SERVICE',
'FROM_QTY' => 1,
'TO_QTY' => 100,
'FROM_NAME' => 'TANJUNG PRIOK',
'FROM_LOCATION_ID' => 'L096',
'TO_NAME' => 'BALARAJA',
'TO_LOCATION_ID' => 'L002',
'RESULT_LOCATION' => array(
'L001' => array(
'TARIF_20' => '1.500.000,00',
'TARIF_40' => '1.750.000,00',
'TARIF_45' => '5.500.000,00',
'TARIF_4H' => '3.500.000,00'
),
'L002' => array(
'TARIF_20' => '500.000,00'
)
)
);
foreach ($a['RESULT_LOCATION'] as $key => $value) {
if ($key != $a['TO_LOCATION_ID']) {
unset($a['RESULT_LOCATION'][$key]);
}
}
The result will be :
php > print_r($a);
Array
(
[COMPANY_SERVICE_ID] => CS01
[COMPANY_NAME] => HANOMAN SAKTI PRATAMA, PT - JAKARTA
[TARIFF_CURRENCY] => IDR
[SELLING_SERVICE_ID] => SS01
[CONTAINER_TYPE_ID] => DC
[SERVICE_NAME] => CONTAINER TRUCKING SERVICE
[FROM_QTY] => 1
[TO_QTY] => 100
[FROM_NAME] => TANJUNG PRIOK
[FROM_LOCATION_ID] => L096
[TO_NAME] => BALARAJA
[TO_LOCATION_ID] => L002
[RESULT_LOCATION] => Array
(
[L002] => Array
(
[TARIF_20] => 500.000,00
)
)
)
Hope it helps.
EDIT
I saw your snippet and adapt the loop for your needs :
foreach ($hasil_jakarta as $h_j_key => $h_j_value) {
foreach ($h_j_value['RESULT_LOCATION'] as $key => $value) {
if ($key != $h_j_value['TO_LOCATION_ID']) {
unset($hasil_jakarta[$h_j_key]['RESULT_LOCATION'][$key]);
}
}
}
Your code was almost good but the unset line was not. In that case, you need to unset a variable from the $hasil_jakarta array.
Good luck !
I have this array in php returned from db
Array
(
[inv_templates] => Array
(
[0] => Array
(
[inven_subgroup_template_id] => 1
[inven_group] => Wires
[inven_subgroup] => CopperWires
[inven_template_id] => 1
[inven_template_name] => CopperWires6G
[constrained] => 0
[value_constraints] =>
[accept_range] => 2 - 16
[information] => Measured Manual
)
[1] => Array
(
[inven_subgroup_template_id] => 1
[inven_group] => Wires
[inven_subgroup] => CopperWires
[inven_template_id] => 2
[inven_template_name] => CopperWires2G
[constrained] => 0
[value_constraints] =>
[accept_range] => 1 - 7
[information] => Measured by Automated Calipers
)
)
)
I need to output this kind of multidimensional stuff
Array
(
[Wires] => Array
(
[inv_group_name] => Wires
[inv_subgroups] => Array
(
[CopperWires] => Array
(
[inv_subgroup_id] => 1
[inv_subgroup_name] => CopperWires
[inv_templates] => Array
(
[CopperWires6G] => Array
(
[inv_name] => CopperWires6G
[inv_id] => 1
)
[CopperWires2G] => Array
(
[inv_name] => CopperWires2G
[inv_id] => 2
)
)
)
)
)
)
I currently do this stuff
foreach ($data['inv_templates'] as $key => $value) {
$processeddata[$value['inven_group']]['inv_group_name'] = $value['inven_group'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_subgroup_id'] = $value['inven_subgroup_template_id'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_subgroup_name'] = $value['inven_subgroup'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_templates'][$value['inven_template_name']]['inv_name'] = $value['inven_template_name'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_templates'][$value['inven_template_name']]['inv_id'] = $value['inven_template_id'];
}
return $processeddata;
EDIT : A var_export
array (
'inv_templates' =>
array (
0 =>
array (
'inven_subgroup_template_id' => '1',
'inven_group' => 'Wires',
'inven_subgroup' => 'CopperWires',
'inven_template_id' => '1',
'inven_template_name' => 'CopperWires6G',
'constrained' => '0',
'value_constraints' => '',
'accept_range' => '2 - 16',
'information' => 'Measured Manual',
),
1 =>
array (
'inven_subgroup_template_id' => '1',
'inven_group' => 'Wires',
'inven_subgroup' => 'CopperWires',
'inven_template_id' => '2',
'inven_template_name' => 'CopperWires6G',
'constrained' => '0',
'value_constraints' => '',
'accept_range' => '1 - 7',
'information' => 'Measured by Automated Calipers',
),
),
)
The foreach is almost unreadable. There must be a simpler way
$processeddata = array();
foreach($data['inv_templates'] as $key => $value) {
$group = $value['inven_group'];
$processeddata[$group]['inv_group_name'] = $group;
$subgroup = &$processeddata[$group]['inv_subgroups'][$value['inven_subgroup']];
$subgroup['inv_subgroup_id'] = $value['inven_subgroup_template_id'];
$subgroup['inv_subgroup_name'] = $value['inven_subgroup'];
$template = $value['inven_template_name'];
$subgroup['inv_templates'][$template]['inv_name'] = $template;
$subgroup['inv_templates'][$template]['inv_id'] = $value['inven_template_id'];
}
return $processeddata;
Untested code. This structures the array in a multidimensional way, and then uses array_merge_recursive to merge them with the already processed data.
if (!isset($processeddata[$value['inven_group']]))
$processeddata[$value['inven_group']] = array();
$processeddata[$value['inven_group']] = array_merge_recursive(
$processeddata[$value['inven_group']],
array(
'inv_group_name' => $value['inven_group'],
'inv_subgroups' => array(
$value['inven_subgroup'] => array(
'inv_subgroup_id' => $value['inven_subgroup_template_id'],
'inv_subgroup_name' => $value['inven_subgroup'],
'inv_templates' => array(
$value['inven_template_name'] => array(
'inv_name' => $value['inven_template_name'],
'inv_id' => $value['inven_template_id'],
),
),
),
),
)
);
I find this format usually works for me. You could do it more efficient, I've just never cared :D
I started traversing at $yourArray['inv_templates'] though.
function groupToStructure(array $rows, array $keys) {
$tree = array();
$keys = array_reverse($keys);
foreach ($rows as $row) {
$subTree = array($row);
foreach ($keys as $key) {
$subTree = array($row[$key] => $subTree);
}
$tree = array_merge_recursive($tree, $subTree);
}
return $tree;
}
print_r(groupToStructure($rows, array('inven_group', 'inven_subgroup', 'inven_template_name')));