array_key_存在于二维数组php中

发布于 2024-10-12 23:20:31 字数 8912 浏览 6 评论 0原文

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

编辑!

我尝试了这个...:

但它不起作用...:

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

这里是 xmlobject:

 [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”中检查它,然后在数组中的数组中检查...?

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

问候, 泰斯

It's a complex question...:

EDIT!!

I tried this...:

But it doesn't work...:

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

And here the xmlobject:

 [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
                                                    )

                                            )

i want to output it in the following array:

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)

Explantion: If the Discriptor name exists, it adds a value of one, if the QualifierName exists in the same DiscriptorName it adds a value to the QualifierName too...

As you can see, it must be checked in the first "array_key_exists" and later in the array in the array...?

I got working with a one dimensional array but this is way more complex...??

Regards,
Thijs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开雨落又逢春i 2024-10-19 23:20:31

您可以只使用isset

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

You can just use isset.

if (isset($stat[$descriptorName][$qualifierName]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文