php - array_key_exists 二维数组 php

标签 php arrays multidimensional-array simplexml

这是一个复杂的问题......:

编辑!!

我试过这个...:

但它不起作用...:

if ($citation->MeshHeadingList)
            {//b

                foreach ($citation->MeshHeadingList->MeshHeading as $mesh_heading)
                {//c
                $mesh_tag = $mesh_heading->DescriptorName;
                //If Qualifier name excists:
                if (isset($mesh_heading->QualifierName))
                    {//d


                    if (!array_key_exists($mesh_tag, $mesh_tags_array))
                        {//e
                        $mesh_tags_array[$mesh_tag][] = 0;
                        foreach ($mesh_heading->QualifierName as $qualifier_name)
                            {//f
                                if (!array_key_exists($qualifier_name, $mesh_tags_array)
                                    {//g
                                    $mesh_tags_array[][$qualifiername] = 0;
                                    }//g
                                else
                                    {//g
                                    $mesh_tags_array[][$qualifiername] += 1;
                                    }//g
                            }//f

                        if ($mesh_heading->DescriptorName->attributes() == "Y")
                            {//f
                            $major = 3;
                            }//f
                            else
                            {//f
                            $major = 0;
                            }//f
                        $mesh_tags_array[$mesh_tag][] += $position_rank + $date_rank + $major;

                        }//e

                    else
                        {//e
                        if ($mesh_heading->DescriptorName->attributes() == "Y")
                            {//f
                            $major = 3;
                            }//f
                        else
                            {//f
                            $major = 0;
                            }//f                
                        if (!array_key_exists($mesh_tag, $mesh_tags_array))
                            {//f
                            $mesh_tags_array[$mesh_tag][] = 0;
                            }//f
                        else
                            {//f
                            $mesh_tags_array[$mesh_tag][] += $position_rank + $date_rank + $major;
                            }//f

                        }//e

                    }//d
                }//c 
            }//b
            else
            {//b
            echo "mesheading is missing in article " . $i . "<br />";

            }//b

这里是 xml 对象:

 [MeshHeadingList] => SimpleXMLElement Object
                            (
                                [MeshHeading] => Array
                                    (
                                        [0] => SimpleXMLElement Object
                                            (
                                                [DescriptorName] => Acoustic Stimulation
                                                [QualifierName] => methods
                                            )

                                        [1] => SimpleXMLElement Object
                                            (
                                                [DescriptorName] => Adult
                                            )

                                        [2] => SimpleXMLElement Object
                                            (
                                                [DescriptorName] => Auditory Perception
                                                [QualifierName] => physiology
                                            )

                                        [3] => SimpleXMLElement Object
                                            (
                                                [DescriptorName] => Electroencephalography
                                                [QualifierName] => methods
                                            )

                                        [4] => SimpleXMLElement Object
                                            (
                                                [DescriptorName] => Event-Related Potentials, P300
                                                [QualifierName] => Array
                                                    (
                                                        [0] => genetics
                                                        [1] => physiology
                                                    )
                    )
                   [5] => SimpleXMLElement Object
                                            (
                                                [DescriptorName] => Schizotypal Personality Disorder
                                                [QualifierName] => Array
                                                    (
                                                        [0] => diagnosis
                                                        [1] => genetics
                                                        [2] => psychology
                                                        [3] => clinical physiology
                                                    )

                                            )

                 [MeshHeading] => Array
                                    (
                                        [0] => SimpleXMLElement Object
                                            (
                                                [DescriptorName] => Adolescent
                                            )


                    [1] => SimpleXMLElement Object
                                            (
                                                [DescriptorName] => Event-Related Potentials, P300
                                                [QualifierName] => Array
                                                    (
                                                        [0] => genetics
                                                        [1] => physiology
                            [2] => methods
                                                    )

                    )
                                        [2] => SimpleXMLElement Object
                                            (
                                                [DescriptorName] => Attention
                                            )

                                        [3] => SimpleXMLElement Object
                                            (
                                                [DescriptorName] => Female
                                            )

                                        [4] => SimpleXMLElement Object
                                            (
                                                [DescriptorName] => Genetic Predisposition to Disease
                                                [QualifierName] => Array
                                                    (
                                                        [0] => genetics
                                                        [1] => psychology
                                                    )

                                            )

                                         [5] => SimpleXMLElement Object
                                            (
                                                [DescriptorName] => Schizotypal Personality Disorder
                                                [QualifierName] => Array
                                                    (
                                                        [0] => diagnosis
                                                        [1] => genetics
                                                        [2] => psychology
                                                    )

                                            )

我想在下面的数组中输出它:

Acoustic Stimulation (1)
-methods (1)

Adult (1)

Auditory Perception (1)
-physiology (1)

Electroencephalography (1)
-methods (1)

Event-Related Potentials, P300 (2)
-genetics (2)
-physiology (2)
-methods (1)

Schizotypal Personality Disorder (2)
-diagnosis (2)
-genetics  (2)
-psychology (2)
-clinical physiology (1)

Adolescent (1)

Attention (1)

Female (1)

Genetic Predisposition to Disease (1)
-genetics (1)
-psychology (1)

说明:如果 Discriptor 名称存在,它会添加一个值 1,如果 QualifierName 存在于同一个 DiscriptorName 中,它也会为 QualifierName 添加一个值......

如您所见,它必须在第一个“array_key_exists”中检查,然后在数组中的数组中检查...?

我开始使用一维数组,但这要复杂得多...??

问候, 泰斯

最佳答案

您可以只使用 isset

if (isset($stat[$descriptorName][$qualifierName]))

关于php - array_key_exists 二维数组 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4736448/

相关文章:

c++ - 将二维数组保存到文件 C++

java - 尝试通过用户输入填充二维数组怎么办?

javascript - 多维数组与对象数组

php - 如何处理解析和 fatal error ?

php - 模拟库可以用来创建多个日志文件吗?

php - 页脚上的 Wordpress 4 列菜单

php - 使用 PHP 将 CSS 样式应用于 MySQL 数据获取

c - 如何将数组中的字符转换为字符串

javascript - 如何使用 Javascript 执行字符串拆分并访问数组的值?

C Turtle 图形不需要的数组输出