PHP Array to XML - php

I have created a multi-dimensional array using fgetcsv from a CSV file.
Using both DOMDocument and SimpleXML I am trying to create a XML file of the CSV document.
The array and XML variables are being passed to a function within the same class file. The XML document is being created without any issues, but no value is passing from the array into the XML. It does work it I use a static value opposed to passing a value from the array, also if I print_r the array the structure and values are all correct.
I have tried 'htmlspecialcharacters' and 'encode_UTF8' before passing the value into the XML.
An example of the code is below, product is the multi-dimensional array.
public function array_to_xml($product, &$xml)
{
foreach($product as $row)
{
$element = $xml->createElement("Product");
$xml->appendChild($element);
$element = $xml->createElement("ID", ($row[38]));
$xml->appendChild($element);
}
}
The problem is obviously with the array but I can't find the answer. Any help would be gratefully appreciated.
The output currently looks like (with not value in the ID element). Once it is working Product will have about 20 child elements.
<?xml version="1.0"?>
<ProductList/>
<Product>
<ID/>
</Product>
</ProductList>
Example of $row when printed to screen:
Array ( [0] => [1] => [2] => 6/10/2016 [3] => [4] => [5] => 7.35 [6] => N [7] => N [8] => N [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 80 [15] => 0 [16] => 80 [17] => 0 [18] => 80 [19] => N [20] => N [21] => N [22] => N [23] => 236.50 [24] => 0.00 [25] => 4.86 [26] => AFG Home Loans - Alpha [27] => 100% Offset Lo Doc Fixed [28] => 100% Offset Lo Doc 4 Year Fixed Owner Occupied [29] => 250.00 [30] => [31] => 7.35 [32] => 0.00 [33] => 4.9 [34] => N [35] => 325.00 [36] => 48 [37] => 4.52 [38] => 1-1MX78TF [39] => N [40] => [41] => [42] => N [43] => N [44] => [45] => Y [46] => 0.00 [47] => 10,000.00 [48] => 2,000,000.00 [49] => Y [50] => 30 [51] => [52] => [53] => Y [54] => 0.00 )

A couple things stand out. First, you have a syntax error on this line:
$element = $xml->createElement("ID", ($row[38])); (note the errant parentheses around $row[38]. The createElement method takes a String for its second parameter.
Second, you're not adding the ID to the product, but to the root XML. Fixing that, your code should look closer to this.
public function array_to_xml($product, &$xml)
{
foreach ($product as $row)
{
$product= $xml->createElement("Product");
$id = $xml->createElement("ID", $row[38]);
$product->appendChild($id);
$xml->appendChild($product);
}
}
If you need it as an attribute as #Barmar commented, you'd use the DOMElement->setAttribute() method, and it would look like:
public function array_to_xml($product, &$xml)
{
foreach ($product as $row)
{
$product= $xml->createElement("Product");
$product->setAttribute('ID', $row[38]);
$xml->appendChild($product);
}
}

Related

Passing multiple variables (some in an array) to a function and getting desired output (PHP)

EDIT: After reading the comments and looking at this again - I realise I was going about this the wrong way.
I was able to achieve my aim by using the first foreach loop on it's own.
Apologies for the long post.
I am parsing an array where the returned data is not consistent with the keys.
From this array, I need to determine what is contained within: Title:, Album:, Artist:
I can't simply filter or search for those strings, as they appear multiple times but followed by different text (see array below).
Below is a small sample of the array ($playlistid).
Array
(
[0] => MPD 0.23.5
[1] => file: Blondie/Parallel_Lines/12_Just_Go_Away.flac
[2] => Last-Modified: 2016-06-05T17:15:07Z
[3] => Format: 44100:16:2
[4] => Album: Parallel Lines
[5] => AlbumArtist: Blondie
[6] => Artist: Blondie
[7] => Composer: Debbie Harry
[8] => Date: 1978
[9] => Genre: Rock
[10] => Title: Just Go Away
[11] => Track: 12
[12] => Time: 214
[13] => duration: 213.800
[14] => Pos: 0
[15] => Id: 745
[16] => file: Blondie/Parallel_Lines/13_Once_I_Had_a_Love-aka_The_Disco_Song-1978_version-.flac
[17] => Last-Modified: 2016-06-05T17:15:01Z
[18] => Format: 44100:16:2
[19] => Album: Parallel Lines
[20] => AlbumArtist: Blondie
[21] => Artist: Blondie
[22] => Composer: Chris Stein; Debbie Harry
[23] => Date: 1978
[24] => Genre: Rock
[25] => Title: Once I Had a Love (aka The Disco Song) (1978 version)
[26] => Track: 13
[27] => Time: 198
[28] => duration: 198.000
[29] => Pos: 1
[30] => Id: 746
[31] => file: Blondie/Parallel_Lines/14_Bang_a_Gong-Get_It_On-live-.flac
[32] => Last-Modified: 2016-06-05T17:15:03Z
[33] => Format: 44100:16:2
[34] => Album: Parallel Lines
[35] => AlbumArtist: Blondie
[36] => Artist: Blondie
[37] => Composer: Marc Bolan
[38] => Date: 1978
[39] => Genre: Rock
[40] => Title: Bang a Gong (Get It On) (live)
[41] => Track: 14
[42] => Time: 330
[43] => duration: 330.293
[44] => Pos: 2
[45] => Id: 747
[46] =>
)
I have a foreach loop that searches the above array ($playlistid) for the desired field, and generates three arrays with the desired keys.
foreach($playlistid as $key => $value) {
if (preg_match('/\bTitle:\s/', $value)) {
$song_title = $playlistid[$key];
}
if (preg_match('/\bArtist:\s/', $value)) {
$song_artist = $playlistid[$key];
}
if (preg_match('/\bAlbum:\s/', $value)) {
$album_title = $playlistid[$key]
}
}
The result I'm looking for, is the output to look like this:
Artist, Album, Title
Blondie Parallel Lines Just Go Away
Blondie Parallel Lines Once I Had a Love (aka The Disco Song) (1978 version)
Blondie Parallel Lines Bang a Gong (Get It On) (live)
It's worth bearing in mind that with a various album for example, the Artist and Album tags will also change with each new track.
I hope all of this makes sense!

php multidimensional array path segment combination loop

I am trying to figure a way to get this to work. But I have a hard time thinking out the logics.
I have this array:
Array
(
[0] => Array
(
[0] => news
[1] => {section}
[2] => {slug}
[3] => {*}
)
[1] => Array
(
[0] => {id}
[1] => {*}
)
[2] => Array
(
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
)
That I need to convert to this result:
0 news/{id}/{date}
1 news/{id}/25-07-1982
2 news/{id}/{section}
3 news/{id}/{slug}
4 news/{id}/{*}
5 news/{*}/{date}
6 news/{*}/25-07-1982
7 news/{*}/{section}
8 news/{*}/{slug}
9 news/{*}/{*}
10 {section}/{id}/{date}
11 {section}/{id}/25-07-1982
12 {section}/{id}/{section}
13 {section}/{id}/{slug}
14 {section}/{id}/{*}
15 {section}/{*}/{date}
16 {section}/{*}/25-07-1982
17 {section}/{*}/{section}
18 {section}/{*}/{slug}
19 {section}/{*}/{*}
20 {slug}/{id}/{date}
21 {slug}/{id}/25-07-1982
22 {slug}/{id}/{section}
23 {slug}/{id}/{slug}
24 {slug}/{id}/{*}
25 {slug}/{*}/{date}
26 {slug}/{*}/25-07-1982
27 {slug}/{*}/{section}
28 {slug}/{*}/{slug}
29 {slug}/{*}/{*}
30 {*}/{id}/{date}
31 {*}/{id}/25-07-1982
32 {*}/{id}/{section}
33 {*}/{id}/{slug}
34 {*}/{id}/{*}
35 {*}/{*}/{date}
36 {*}/{*}/25-07-1982
37 {*}/{*}/{section}
38 {*}/{*}/{slug}
39 {*}/{*}/{*}
The input array could contain more than three keys, so the solution I'm looking for should be dynamic. And the result should have the same order as the result shown above.
Does someone know how to do this in a efficient way? Can someone give me a push in the right direction? Thanks a lot! :)
Sth like this
foreach ($array[0] as $val0 )
foreach ($array[1] as $val1 )
foreach ($array[2] as $val2 )
$newArray[] = "$val0/$val1/$val2";
EDIT: for variable array length
function recursive($array , $length = 0){
$retval =array();
if($length < count($array) -1){
foreach ($array[$length] as $val0 )
foreach (recursive($array, $length+1) as $val1)
$retval[] = "$val0/$val1";
}
else
{
foreach ($array[$length] as $val0 )
$retval[] = "$val0";
}
return $retval;
}
print_r(recursive($array));
Just because I like writing functions that mis/manage PHP arrays, I put this together, mainly because I was pretty sure you could avoid recursion — because the structure itself isn't recursive. (My head seems to think that is a rule, I'm sure someone somewhere can prove it wrong).
foreach ( array_reverse($array) as $sub ) {
if ( isset($rem) ) {
$ret = array();
foreach ( $sub as $itm ) {
foreach ( $rem as $val ) { $ret[] = "$itm/$val"; }
}
$rem = $ret;
}
else {
$rem = $sub;
}
}
The output found in $rem is as follows:
Array (
[0] => news/{id}/{date}
[1] => news/{id}/25-07-1982
[2] => news/{id}/{section}
[3] => news/{id}/{slug}
[4] => news/{id}/{*}
[5] => news/{*}/{date}
[6] => news/{*}/25-07-1982
[7] => news/{*}/{section}
[8] => news/{*}/{slug}
[9] => news/{*}/{*}
[10] => {section}/{id}/{date}
[11] => {section}/{id}/25-07-1982
[12] => {section}/{id}/{section}
[13] => {section}/{id}/{slug}
[14] => {section}/{id}/{*}
[15] => {section}/{*}/{date}
[16] => {section}/{*}/25-07-1982
[17] => {section}/{*}/{section}
[18] => {section}/{*}/{slug}
[19] => {section}/{*}/{*}
[20] => {slug}/{id}/{date}
[21] => {slug}/{id}/25-07-1982
[22] => {slug}/{id}/{section}
[23] => {slug}/{id}/{slug}
[24] => {slug}/{id}/{*}
[25] => {slug}/{*}/{date}
[26] => {slug}/{*}/25-07-1982
[27] => {slug}/{*}/{section}
[28] => {slug}/{*}/{slug}
[29] => {slug}/{*}/{*}
[30] => {*}/{id}/{date}
[31] => {*}/{id}/25-07-1982
[32] => {*}/{id}/{section}
[33] => {*}/{id}/{slug}
[34] => {*}/{id}/{*}
[35] => {*}/{*}/{date}
[36] => {*}/{*}/25-07-1982
[37] => {*}/{*}/{section}
[38] => {*}/{*}/{slug}
[39] => {*}/{*}/{*}
)
Also, for those that like their arrays multidimensional, this might come in handy (although I'd hate to think what the overheads are for such a code golfed version). Just to be clear, this second example doesn't create the string list as requested by the OP, but a hierarchical array structure instead.
foreach ( array_reverse($array) as $sub ) {
$rem = isset($rem)
? array_combine($sub, array_fill(0, count($sub), $rem))
: $sub
;
}
This generates (again in $rem):
Array (
[news] => Array (
[{id}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
[{*}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
)
[{section}] => Array (
[{id}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
... and so on
Now if only PHP had a join_recursive that included keys.
(it would be almost pointless, save for helping with the above).

ERROR: using '$this' when not in object context [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have been coding and improving the code jszobody provided me that can found on this LINK which It makes me to be my refference but it results me to an error Fatal error: Using $this when not in object context
PHP:
<?php
function getAllPossiblePermutations($mdArray, $firstTime=true, $tempArray=array())
{
// initialize results array
if ($firstTime)
{
$this->permutationsResultsArray = array();
}
// find first sub array and iterate through it
$thisArray = array_shift($mdArray);
foreach ($thisArray as $key => $elem)
{
// if this number has already been used skip this possible permutation
if (in_array($elem, $tempArray))
{
continue;
}
$tempArray[] = $elem;
if (count($mdArray) == 0)
{
$this->permutationsResultsArray[] = $tempArray;
}
else
{
$this->getAllPossiblePermutations($mdArray, false, $tempArray);
}
array_pop($tempArray);
}
}
$traits = array
(
array('Happy', 'Sad', 'Angry', 'Hopeful'),
array('Outgoing', 'Introverted'),
array('Tall', 'Short', 'Medium'),
array('Handsome', 'Plain', 'Ugly')
);
print_r(getAllPossiblePermutations($traits));
?>
EXPECTED OUTPUT:
Array ( [0] => HappyOutgoingTallHandsome 1 => HappyOutgoingTallPlain [2] => HappyOutgoingTallUgly [3] => HappyOutgoingShortHandsome [4] => HappyOutgoingShortPlain [5] => HappyOutgoingShortUgly [6] => HappyOutgoingMediumHandsome [7] => HappyOutgoingMediumPlain [8] => HappyOutgoingMediumUgly [9] => HappyIntrovertedTallHandsome [10] => HappyIntrovertedTallPlain [11] => HappyIntrovertedTallUgly [12] => HappyIntrovertedShortHandsome [13] => HappyIntrovertedShortPlain [14] => HappyIntrovertedShortUgly [15] => HappyIntrovertedMediumHandsome [16] => HappyIntrovertedMediumPlain [17] => HappyIntrovertedMediumUgly [18] => SadOutgoingTallHandsome [19] => SadOutgoingTallPlain [20] => SadOutgoingTallUgly [21] => SadOutgoingShortHandsome [22] => SadOutgoingShortPlain [23] => SadOutgoingShortUgly [24] => SadOutgoingMediumHandsome [25] => SadOutgoingMediumPlain [26] => SadOutgoingMediumUgly [27] => SadIntrovertedTallHandsome [28] => SadIntrovertedTallPlain [29] => SadIntrovertedTallUgly [30] => SadIntrovertedShortHandsome [31] => SadIntrovertedShortPlain [32] => SadIntrovertedShortUgly [33] => SadIntrovertedMediumHandsome [34] => SadIntrovertedMediumPlain [35] => SadIntrovertedMediumUgly [36] => AngryOutgoingTallHandsome [37] => AngryOutgoingTallPlain [38] => AngryOutgoingTallUgly [39] => AngryOutgoingShortHandsome [40] => AngryOutgoingShortPlain [41] => AngryOutgoingShortUgly [42] => AngryOutgoingMediumHandsome [43] => AngryOutgoingMediumPlain [44] => AngryOutgoingMediumUgly [45] => AngryIntrovertedTallHandsome [46] => AngryIntrovertedTallPlain [47] => AngryIntrovertedTallUgly [48] => AngryIntrovertedShortHandsome [49] => AngryIntrovertedShortPlain [50] => AngryIntrovertedShortUgly [51] => AngryIntrovertedMediumHandsome [52] => AngryIntrovertedMediumPlain [53] => AngryIntrovertedMediumUgly [54] => HopefulOutgoingTallHandsome [55] => HopefulOutgoingTallPlain [56] => HopefulOutgoingTallUgly [57] => HopefulOutgoingShortHandsome [58] => HopefulOutgoingShortPlain [59] => HopefulOutgoingShortUgly [60] => HopefulOutgoingMediumHandsome [61] => HopefulOutgoingMediumPlain [62] => HopefulOutgoingMediumUgly [63] => HopefulIntrovertedTallHandsome [64] => HopefulIntrovertedTallPlain [65] => HopefulIntrovertedTallUgly [66] => HopefulIntrovertedShortHandsome [67] => HopefulIntrovertedShortPlain [68] => HopefulIntrovertedShortUgly [69] => HopefulIntrovertedMediumHandsome [70] => HopefulIntrovertedMediumPlain [71] => HopefulIntrovertedMediumUgly [72] => )
where did I go wrong?
You have a lot of referemces to class variables, like this:
$this->permutationsResultsArray = array();
And PHP complains since this function is not a method in a class. It will work if you just remove this-> so you get:
$permutationsResultsArray = array();
In addition when you are all done you never really return the result.. Like this:
return $permutationsResultsArray;
There is a problem with it though. You are recusing and you don't create that array except in the first round but your code uses it as if it was defined.
BTW: Your function could be much easier with 3 foreach loops:
function getCombinations($traits)
{
$combinations = array('');
foreach( $traits as $trait_level ) {
$new_combinations = array();
foreach ( $combinations as $comb ) {
foreach ( $trait_level as $trait ){
$new_combinations[] = "$comb $trait";
}
}
$combinations = $new_combinations;
}
return $combinations;
}
A function doesn't have a self referential $this. Actually, removing all your $this-> references and make $tempArray a pass by reference instead of by value, your code should work...
function getAllPossiblePermutations($mdArray, $firstTime=true, &$tempArray=array())

PHP Array last element being lost from using another array as dictionary

I have three arrays. One that contains the given title information from the user stored in $attributenames. The second has the data from the user stored in $anesdata. (So the first two are related) And the third is a dictionary so that I can get out the information I want using the index which is stored in $medicalInstanceDictionary.
This is the first one:
Array
(
[0] => PATIENT MRN
[1] => PATIENT LAST NAME
[2] => PATIENT FIRST NAME
[3] => PATIENT MIDDLE NAME
[4] => PATIENT SSN
[5] => PATIENT SEX
[6] => INVOICE
[7] => TRANSACTION
[8] => DATE OF PROCEDURE
[9] => POSTING DATE
[10] => LOCATION
[11] => CHARGE AMT
[12] => PROVIDER
[13] => FSC1
[14] => FSC2
[15] => PATIENT DOB
[16] => ATTENDING SURGEON
[17] => ATTENDING SURGEON
[18] => CPT CODES
[19] => ASA CLASSFICATION
[20] => ANESTHESIA CRNA
[21] => ANESTHESIA RESIDENT
[22] => SURGERY START TIME
[23] => SURGERY END TIME
[24] => SVC UNIT
[25] => FACILITY NAME
[26] => BILLING NUMBER
[27] => BILLING AREA NAME
[28] => PROCEDURE MODIFIER1
[29] => PROCEDURE MODIFIER2
[30] => PROCEDURE MODIFIER3
[31] => PRIMARY DX
[32] => SECONDARY DX
)
The second array is a two dimensional array, but each line is equivalent to one patient. So that first patient looks like this(put in x's instead of actual patient data):
[0] => Array
(
[0] => xxxx
[1] => xxxx
[2] => xxxx
[3] => xxxx
[4] => xxxxx
[5] => xxxx
[6] => xxxx
[7] => xxx
[8] => xxxxx
[9] => xxxx
[10] => xxxx
[11] => xxxxx
[12] => xxxx
[13] => xxxxx
[14] => xxxx
[15] => xxxx
[16] => xxxxxxx
[17] => xxxxx
[18] => xxxxx
[19] => xxxx
[20] =>
[21] => xxxxx
[22] => xxxxx
[23] => xxxxx
[24] => xxxxx
[25] => xxxx
[26] => xxxxx
[27] => xxxx
[28] => xxxxxxxx
[29] => xxxx
[30] =>
[31] => xxxxxxx
[32] => xxxxxxx
)
Then the dictionary looks like this:
$medicalInstanceDictionary = array(
'CPT CODES' => "CPT_Code",
'ASA CLASSFICATION' => "MG_ASA_Class",
'FACILITY NAME' => "Facility_Name",
'BILLING NUMBER' => "Billing_Number",
'BILLING AREA NAME' => "Billing_Area_Name",
'PROCEDURE MODIFIER1' => "Procedure_Modifier1",
'PROCEDURE MODIFIER2' => "Procedure_Modifier2",
'PRIMARY DX' => "Primary_Dx",
'SECONDARY DX' => "Secondary_Dx",
'INVOICE' => "FIN"
);
I am doing a nested foreach loop to get each row.
foreach ($dataarray as $dataindex => $datavalue)
{
$out = "";
foreach ($dictionary as $index => $value)
{
//find PATIENT MRN in $attributearray and get it's index
$attributeindex = array_search($index, $attributearray);
if ($attributeindex===FALSE) {
echo "Error : ".$index." not found <br />";
} else {
echo "<br>The attribute is: ".$value." The index is: ".$attributeindex."<br>";
}
(more code....)
}
(more code....)
}
That echo statement looks like this:
The attribute is: CPT_Code The index is: 18
The attribute is: MG_ASA_Class The index is: 19
The attribute is: Facility_Name The index is: 25
The attribute is: Billing_Number The index is: 26
The attribute is: Billing_Area_Name The index is: 27
The attribute is: Procedure_Modifier1 The index is: 28
The attribute is: Procedure_Modifier2 The index is: 29
The attribute is: Primary_Dx The index is: 31
Error : SECONDARY DX not found
The attribute is: FIN The index is: 6
I have no idea why it is skipping over Secondary_Dx. I have checked for spelling errors. I don't think it is my method of doing it because it only does not work for Secondary_Dx. The only thing I can think of is that it does something funky since it is the last element of the array. Has anyone seen this before?
Edit:
Added element(tried both methods, and both resulted in the same looking array using print_r:
//array_push($attributenames, "THE END");
$attributenames[] ="THE END";
echo "<pre>";
print_r($attributenames);
echo "</pre>";
output from that along with the error handling statement from above:
Array
(
[0] => PATIENT MRN
[1] => PATIENT LAST NAME
[2] => PATIENT FIRST NAME
[3] => PATIENT MIDDLE NAME
[4] => PATIENT SSN
[5] => PATIENT SEX
[6] => INVOICE
[7] => TRANSACTION
[8] => DATE OF PROCEDURE
[9] => POSTING DATE
[10] => LOCATION
[11] => CHARGE AMT
[12] => PROVIDER
[13] => FSC1
[14] => FSC2
[15] => PATIENT DOB
[16] => ATTENDING SURGEON
[17] => ATTENDING SURGEON
[18] => CPT CODES
[19] => ASA CLASSFICATION
[20] => ANESTHESIA CRNA
[21] => ANESTHESIA RESIDENT
[22] => SURGERY START TIME
[23] => SURGERY END TIME
[24] => SVC UNIT
[25] => FACILITY NAME
[26] => BILLING NUMBER
[27] => BILLING AREA NAME
[28] => PROCEDURE MODIFIER1
[29] => PROCEDURE MODIFIER2
[30] => PROCEDURE MODIFIER3
[31] => PRIMARY DX
[32] => SECONDARY DX
[33] => THE END
)
This is dictionary array Array
(
[CPT CODES] => CPT_Code
[ASA CLASSFICATION] => MG_ASA_Class
[FACILITY NAME] => Facility_Name
[BILLING NUMBER] => Billing_Number
[BILLING AREA NAME] => Billing_Area_Name
[PROCEDURE MODIFIER1] => Procedure_Modifier1
[PROCEDURE MODIFIER2] => Procedure_Modifier2
[PRIMARY DX] => Primary_Dx
[SECONDARY DX] => Secondary_Dx
[INVOICE] => FIN
)
The attribute is: CPT_Code The index is: 18
The attribute is: MG_ASA_Class The index is: 19
The attribute is: Facility_Name The index is: 25
The attribute is: Billing_Number The index is: 26
The attribute is: Billing_Area_Name The index is: 27
The attribute is: Procedure_Modifier1 The index is: 28
The attribute is: Procedure_Modifier2 The index is: 29
The attribute is: Primary_Dx The index is: 31
Error : SECONDARY DX not found
Array ( [0] => PATIENT MRN [1] => PATIENT LAST NAME [2] => PATIENT FIRST NAME [3] => PATIENT MIDDLE NAME [4] => PATIENT SSN [5] => PATIENT SEX [6] => INVOICE [7] => TRANSACTION [8] => DATE OF PROCEDURE [9] => POSTING DATE [10] => LOCATION [11] => CHARGE AMT [12] => PROVIDER [13] => FSC1 [14] => FSC2 [15] => PATIENT DOB [16] => ATTENDING SURGEON [17] => ATTENDING SURGEON [18] => CPT CODES [19] => ASA CLASSFICATION [20] => ANESTHESIA CRNA [21] => ANESTHESIA RESIDENT [22] => SURGERY START TIME [23] => SURGERY END TIME [24] => SVC UNIT [25] => FACILITY NAME [26] => BILLING NUMBER [27] => BILLING AREA NAME [28] => PROCEDURE MODIFIER1 [29] => PROCEDURE MODIFIER2 [30] => PROCEDURE MODIFIER3 [31] => PRIMARY DX [32] => SECONDARY DX [33] => THE END )
Array ( [CPT CODES] => CPT_Code [ASA CLASSFICATION] => MG_ASA_Class [FACILITY NAME] => Facility_Name [BILLING NUMBER] => Billing_Number [BILLING AREA NAME] => Billing_Area_Name [PROCEDURE MODIFIER1] => Procedure_Modifier1 [PROCEDURE MODIFIER2] => Procedure_Modifier2 [PRIMARY DX] => Primary_Dx [SECONDARY DX] => Secondary_Dx [INVOICE] => FIN )
The attribute is: FIN The index is: 6
You should test for a valid $attributeindex !
$attributeindex = array_search($index, $attributearray);
if ($attributeindex===FALSE) {
echo "Error : ".$index." not found <br />";
} else {
echo "<br>The attribute is: ".$value." The index is: ".$attributeindex."<br>";
}
If you get a not found Error you can be shure $index is not found in $attributearray .
Update :
This is very strange !
From your output we can clearly see.
$index == SECONDARY DX
and
$attributearray has a key [32]
[32] => SECONDARY DX
Only to test :
can you add to $attributearray at the end
[33] => 'END'
and see what happens.
Update 2 :
As i can see in the new output you got with
echo "<pre>";
print_r($attributenames);
echo "</pre>";
There is a empty line between [32] and [33].
There must be a invisible sign at the end of [32] => SECONDARY DX
I suspect a new line character.
Array
(
[0] => PATIENT MRN
[1] => PATIENT LAST NAME
....
[30] => PROCEDURE MODIFIER3
[31] => PRIMARY DX
[32] => SECONDARY DX
[33] => THE END
)
Try to remove that character and it should work .
TIP:
If you ever re experiencing similar behavior, you should check with :
for example:
echo bin2hex($attributenames[32]);
Output in windows look at the end :
5345434f4e444152592044580d0a
Where 0d is CR = Carriage return and 0a is LF = Line feed .
ASCII-Tabelle
Try using trim() on the value you are testing for. If there is white space you can't see at the end it won't match.
$attributeindex = trim( array_search($index, $attributearray) );

best way to handle a tree array

I have a tree array from cakephp 2.0 tree behavior noe i need to separate it in levels so i can build a selector per each level. Here is an example of the array:
Array
(
[25] => Global
[26] => _Region1
[29] => __Pais1
[41] => ___Distrito1
[42] => ___Distrito2
[30] => __Pais2
[43] => ___Distrito1
[44] => ___Distrito2
[31] => __Pais3
[45] => ___Distrito1
[32] => __Pais4
[46] => ___Distrito1
[27] => _Region2
[33] => __Pais1
[47] => ___Distrito1
[34] => __Pais2
[48] => ___Distrito1
[35] => __Pais3
[36] => __Pais4
[28] => _Region3
[37] => __Pais1
[38] => __Pais2
[39] => __Pais3
[40] => __Pais4
)
Now what i need is to create one select for global, another select for region1 another for pais1 and another for disctrito1
The problem is that i can use ids to generate it since this will be changing by the user.
What will be the best way to manipulate this array so it builds a select to each level of the array.
Thanks in advance.
You can get the tree as a nested array via find('threaded'). This will give you each item in the tree, with a 'children' key containing all child nodes as an array;
Retrieving Your Data - find('threaded')
However, as indicated, because the content will be modified by the user, the depth of the tree may change. To accommodate for those changes, you'll need to use a 'recursive function' to loop through the tree, regardless of its depth
Mockup Code:
echo $this->MyTreeHelper->buildTreeDropDowns($treeData);
class MyTreeHelper extends AppHelper {
public function buildTreeDropDowns($treeData)
{
$output = '';
foreach($treeData as $item) {
$output .= createAdropDown($item);
if (!empty($item['children'])) {
$output .= $this->buildTreeDropDowns($item['children']);
}
}
return $output;
}
}

Categories