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

PHP数组使用另一个数组作为字典时丢失的最后一个元素,php,arrays,dictionary,Php,Arrays,Dictionary,我有三个数组。包含来自用户的给定标题信息(存储在$AttributeName中)的文件。第二个将来自用户的数据存储在$data中。所以前两个是相关的,第三个是字典,这样我就可以使用存储在$medicalistancedictionary中的索引获取我想要的信息 这是第一个: Array ( [0] => PATIENT MRN [1] => PATIENT LAST NAME [2] => PATIENT FIRST NAME [3] =>

我有三个数组。包含来自用户的给定标题信息(存储在$AttributeName中)的文件。第二个将来自用户的数据存储在$data中。所以前两个是相关的,第三个是字典,这样我就可以使用存储在$medicalistancedictionary中的索引获取我想要的信息

这是第一个:

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

)
第二个阵列是二维阵列,但每条线相当于一名患者。所以第一个患者看起来像这样,输入x,而不是实际的患者数据:

[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

    )
然后,字典如下所示:

$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"
);
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
我正在做一个嵌套的foreach循环来获取每一行

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....)
}
我不知道为什么它跳过了Secondary_Dx。我检查了拼写错误。我不认为这是我做这件事的方法,因为它只对次要的Dx不起作用。我能想到的唯一一件事是它做了一些奇怪的事情,因为它是数组的最后一个元素。以前有人见过这个吗

编辑: Added Element尝试了这两种方法,并且都使用print\r生成了外观相同的数组:

//array_push($attributenames, "THE END");
$attributenames[] ="THE END";

echo "<pre>";
print_r($attributenames);
echo "</pre>";

您应该测试有效的$attributeindex

; [32]和[33]之间有一条空行。 [32]=>SECONDARY DX的末尾必须有一个不可见的符号 我怀疑有一个新行字符

5345434f4e444152592044580d0a
尝试删除该字符,它应该会起作用

提示:

如果您曾经经历过类似的行为,您应该检查:

例如:

$attributeindex = trim( array_search($index, $attributearray) );
windows中的输出查看结尾:

其中0d为CR=回车,0a为LF=换行

ASCII标签

尝试对要测试的值使用trim。如果在结尾处有你看不到的空白,它将不匹配


看看这三个数组中哪一个是$attributearray?如果您传递整个代码,可能会有所帮助…第一个是$attributearray。我想把整个代码放进去,但是有多个文件,看起来有点凌乱。好吧,在break和dictionary中添加了,改变了上面的输出。这就是我所怀疑的。您能看到输出print\r$attributeName之间的空行吗;[32]和[33]?有一个非可视字符可能是新行。我不认为这会有帮助,因为我知道$index在$attributearray中。我以前使用过这段代码,它适用于所有其他方面,只是不适用于次要的Dx。您应该始终测试它,否则可以使用FALSE。您将看到$index中的内容。然后让我们看看你有什么我不明白你的意思。当我打印R$attributearray时,它看起来就像我上面的第一个数组。在输出打印时不能将其分解以测试错误,对吗?不能停止打印输出。你能显示你只得到最后几行的输出吗?我更新了它,所以在文章的末尾有当前代码和当前输出。我还是不明白你在找什么。那个输出对我来说似乎没有用处。谢谢:
echo "<pre>";
print_r($attributenames);
echo "</pre>";
Array
(
    [0] => PATIENT MRN
    [1] => PATIENT LAST NAME
    ....
    [30] => PROCEDURE MODIFIER3
    [31] => PRIMARY DX
    [32] => SECONDARY DX

    [33] => THE END
)
echo bin2hex($attributenames[32]);
5345434f4e444152592044580d0a
$attributeindex = trim( array_search($index, $attributearray) );