数组索引出现两次怎么解决呢?

时间:2017-12-05 09:15:04

标签: php arrays

我有数组值,它将包含multidimenasional数组的相同数组索引。两个结果都来自svg结果值,我将svg simplexmlelement对象转换为数组。请查看我的以下代码并就此提出建议。如何用0,1,2等建立重新索引

print '<pre>';
    print_r(array_values(array($svg_array_values)));

Array
(
    [0] => Array
        (
            [@attributes] => Array
                (
                    [id] => Layer_1
                    [data-name] => Layer 1
                    [viewBox] => 0 0 72 72
                    [width] => 234
                    [height] => 170
                    [preserveAspectRatio] => none
                )

            [defs] => Array
                (
                    [style] => .cls-1{fill-rule:evenodd;}
                )

            [title] => Arrow 20
            [path] => Array
                (
                    [@attributes] => Array
                        (
                            [class] => cls-1
                            [d] => M70.47,34.06,48.11,11.7a2.75,2.75,0,0,0-4.69,1.94l1.72,11.93h-34a10.43,10.43,0,0,0,0,20.86h34L43.42,58.37a2.76,2.76,0,0,0,2.75,2.74,2.77,2.77,0,0,0,1.94-.8L70.47,37.94a2.73,2.73,0,0,0,0-3.88Z
                            [fill] => #000000
                        )

                )

        )

)
Array
(
    [0] => Array
        (
            [@attributes] => Array
                (
                    [id] => Layer_1
                    [data-name] => Layer 1
                    [viewBox] => 0 0 72 72
                    [width] => 160
                    [height] => 99
                    [preserveAspectRatio] => none
                )

            [defs] => Array
                (
                    [style] => .cls-1{fill-rule:evenodd;}
                )

            [title] => Arrow 18
            [polygon] => Array
                (
                    [@attributes] => Array
                        (
                            [class] => cls-1
                            [points] => 71.28 36 55.49 20.21 55.49 31.19 0.72 31.19 0.72 40.81 55.49 40.81 55.49 51.79 71.28 36 71.28 36
                        )

                )

        )

)

输出应

Array
(
    [0] => Array
        (
            [@attributes] => Array
                (
                    [id] => Layer_1
                    [data-name] => Layer 1
                    [viewBox] => 0 0 72 72
                    [width] => 234
                    [height] => 170
                    [preserveAspectRatio] => none
                )

            [defs] => Array
                (
                    [style] => .cls-1{fill-rule:evenodd;}
                )

            [title] => Arrow 20
            [path] => Array
                (
                    [@attributes] => Array
                        (
                            [class] => cls-1
                            [d] => M70.47,34.06,48.11,11.7a2.75,2.75,0,0,0-4.69,1.94l1.72,11.93h-34a10.43,10.43,0,0,0,0,20.86h34L43.42,58.37a2.76,2.76,0,0,0,2.75,2.74,2.77,2.77,0,0,0,1.94-.8L70.47,37.94a2.73,2.73,0,0,0,0-3.88Z
                            [fill] => #000000
                        )

                )

        )

)
Array
(
    [1] => Array
        (
            [@attributes] => Array
                (
                    [id] => Layer_1
                    [data-name] => Layer 1
                    [viewBox] => 0 0 72 72
                    [width] => 160
                    [height] => 99
                    [preserveAspectRatio] => none
                )

            [defs] => Array
                (
                    [style] => .cls-1{fill-rule:evenodd;}
                )

            [title] => Arrow 18
            [polygon] => Array
                (
                    [@attributes] => Array
                        (
                            [class] => cls-1
                            [points] => 71.28 36 55.49 20.21 55.49 31.19 0.72 31.19 0.72 40.81 55.49 40.81 55.49 51.79 71.28 36 71.28 36
                        )

                )

        )

)

1 个答案:

答案 0 :(得分:0)

您使用了array_values()函数,此函数删除了相同的值迭代。所以如果你想接受输出,请不要使用此功能。只需添加foreach循环并使用array_key_exist()进行检查。像这样:

$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (array_key_exists("Volvo",$a))
  {
  echo "Key exists!";
  }
else
  {
  echo "Key does not exist!";
  }