doucheyi1347 2013-05-09 17:02
浏览 94
已采纳

PHP数组最后一个元素因使用另一个数组作为字典而丢失

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
  • 写回答

2条回答 默认 最新

  • douyan2821 2013-05-09 17:35
    关注

    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

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料