dsiuz86842 2014-01-09 20:03
浏览 49
已采纳

在多维php数组中查找父键

I would like to find out the correct key that contains the array selected. Right now the code echoes out the next array key of 3 and not 2. This seems very basic. I could fix this by simply subtracting 1, but that seems problematic because the last artist array echoes a position of 0. Can anyone help? (I need to find this key so I can properly set next and previous values.)

sample_entries.xml

<?xml version="1.0"?>
<entries>
    <entry>
        <rank>1</rank>
        <id>koons_jeff</id>
        <firstname>Jeff</firstname>
        <lastname>Koons</lastname>
        <bcountry>US</bcountry>
        <byear>1955</byear>
    </entry>
    <entry>
        <rank>2</rank>
        <id>richter_gerhard</id>
        <firstname>Gerhard</firstname>
        <lastname>Richter</lastname>
        <bcountry>DE</bcountry>
        <byear>1932</byear>
    </entry>
    <entry>
        <rank>5</rank>
        <id>doig_peter</id>
        <firstname>Peter</firstname>
        <lastname>Doig</lastname>
        <bcountry>UK</bcountry>
        <byear>1959</byear>
    </entry>
    <entry>
        <rank>7</rank>
        <id>marden_brice</id>
        <firstname>Brice</firstname>
        <lastname>Marden</lastname>
        <bcountry>US</bcountry>
        <byear>1938</byear>
    </entry>
</entries>

index.php

<?php
$xml = simplexml_load_file('sample_entries.xml');
$path = $xml->xpath('entry');
//strip simple xml tags.
$array = json_decode( json_encode($path) , 1);
print_r($array);
// cannot change the above XML structure.

echo '<br><br>';


// set page's unique identifier.
$artist = 'doig_peter'; 


foreach($array as $element => $inner_array) { 

    if($artist == $inner_array[id]) {
        $current_artist = $inner_array;
        extract($current_artist);
        echo '<b>Current Artist: </b>'.$firstname.' '.$lastname.' - '.$bcountry.'-'.$byear.'<br><br>';
        echo key($array); 
    }
}
?>

Here is the current output front the above code. I would like the last number "[3]" to report the correct key that contains the data for the id, which is "[2]":

Array ( [0] => Array ( [rank] => 1 [id] => koons_jeff [firstname] => Jeff [lastname] => Koons [bcountry] => US [byear] => 1955 ) [1] => Array ( [rank] => 2 [id] => richter_gerhard [firstname] => Gerhard [lastname] => Richter [bcountry] => DE [byear] => 1932 ) [2] => Array ( [rank] => 5 [id] => doig_peter [firstname] => Peter [lastname] => Doig [bcountry] => UK [byear] => 1959 ) [3] => Array ( [rank] => 7 [id] => marden_brice [firstname] => Brice [lastname] => Marden [bcountry] => US [byear] => 1938 ) )

Current Artist: Peter Doig - UK-1959

3

  • 写回答

1条回答 默认 最新

  • dounieqi6959 2014-01-09 21:49
    关注

    Instead of

    echo key($array);
    

    you want

    echo $element;
    

    since $element is the variable assigned in your foreach loop to hold the current key.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64