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 !
Related
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
}
}
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/>";
}
i have an array
Array
(
[0] => Array
(
[folding_knives__no_assist__possession19] => G
[folding_knives__no_assist__possession___length20] =>
[folding_knives__no_assist___open_cary21] => G
[folding_knives__no_assist__open_carry_length22] =>
[folding_knives__no_assist__concealed23] => G
[folding_knives__no_assisted__concealed_length24] =>
[folding_knives__no_assist__concealed_w__ccw_required25] => R
[folding_knives__no_assist__concealed_w__ccw_required_l26] =>
[folding_knives__no_assist___notes27] =>
)
[1] => Array
(
[folding_knives__assisted_opening__possession28] => G
[folding_knives__assisted_opening__possession_length29] =>
[folding_knives__assisted_opening__open_carry30] => G
[folding_knives__assisted_opening__open_carry_length31] =>
[folding_knives__assisted_opening___concealed32] => G
[folding_knives__assisted_opening__concealed_length33] =>
[folding_knives__assisted_opening___concealed_w__ccw_re34] => R
[folding_knives__assisted_opening__concealed_w__ccw_req35] =>
[folding_knives___assisted_opening___notes36] =>
)
)
I already trying to show my array data into another array but cant
foreach ($chunks as $key => $val)
{
$allknife[] = array(
'name'=>$key[0],
'possession'=>$val[0],
'possession_length'=>$val[1]
);
}
Here name'=>$key[0] here will be arry first item key like folding_knives__no_assist__possession19
It should be link this, and you want field count link 1/2/3/4... then you have to use flag logic like, i=0; and i++
$allknife[] = array();
foreach ($chunks as $key => $val)
{
$allknife['name']=>$val['yourvaluesoeshere'],
$allknife['possession']=>$val['yourvaluesoeshere'],
$allknife['possession_length']=>$val['yourvaluesoeshere']
}
Since your arrays have inconsistent key names you can't rely on this an need consistent names first!
Map the array more like this:
array(
0 => array(
'assist' => true,
'open_carry_nr' => 21,
'open_carry_length' => 22
// and so on...
),
2 => array(
'assist' => false,
'open_carry_nr' => 18,
'open_carry_length' => 1337
// and so on...
)
);
Way better practice.
I need to merge a new array of alternative information into the loop if they have the alternative information in their profile.
Here's my loop:
foreach ($doctor->getVars() as $k => $v)
{
$data['doctor_'. $k] = $v;
}
foreach ($patient->get_data() as $k=>$v)
{
if (is_string($v) || is_numeric($v))
$data["patient_" . $k] = strtoupper($v);
}
Here's the $data var_dump:
Array
(
[employee] => person
[date] => 05/08/2013
[datetime] => 05/08/2013 9:41:15 AM
[department] => stuff
[employee_ext] => 7457
[employee_email] =>
[barcode] => *NZS01*
[doctor_df_code] => 09HQ
[doctor_npi] => 1111111111
[doctor_dea] => B4574
[doctor_upin] =>
[doctor_license] =>
[doctor_phone] => (111)111-1111
[doctor_fax] => (000)000-0000
[doctor_fname] => UNDEFINED
[doctor_lname] => UNDEFINED
[doctor_title] =>
[doctor_intake_rx_caller_id] =>
[doctor_costco_rx_caller_id] =>
[doctor_reorder_rx_caller_id] =>
[doctor_address1] => 24 CABELL st
[doctor_address2] => SUITE 10
[doctor_city] => places
[doctor_state] => CA
[doctor_zip] => 91111
[doctor_active_events] =>
[doctor_dont_call] => 0
[doctor_dont_fax] => 1
)
I need to merge the below array into the above array.
Here's the print var for the function addr($dfcode):
Array
(
[0] => Array
(
[CODE_] => 09HQ
[doctor_address1] => alternate addy
[doctor_address2] => 45854
[doctor_city] => different city
[doctor_state] => CA
[doctor_zip] => 963545
[doctor_phone] => (619)111-2548
[doctor_fax] => (157)123-4569
)
)
I'm new to array merge and I'm assuming right after the $data['doctor_'. $k] = $v i could list out the new function and the fields i want to merge in particular?
syntax is what i'm not sure on:
$data['doctor_'. $k] . array_merge(addr($dfcode))['doctor_address1'] = $v;
Any help would be greatly appreciated, thank you.
The general formula for merging two arrays is as follows (merging $array_m into $array_o):
foreach($array_m as $key=>$value){
$array_o[$key] = $value;
}
$array_o would now contain all of the elements of $array_m
EDIT: I just noticed in your post that you seem to want to use the array_merge function. You could also do the following:
$array_o = array_merge($array_o, array_m);
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')));