繁体   English   中英

从XML在PHP多维数组中搜索值

[英]Search value in PHP multidimentional array from xml

我有一个一直试图在PHP中搜索的xml文件。 XML包含房间及其可进入的月份。 这是xml:

<rooms>
    <room>
        <id>1</id>
        <name>room1</name>
        <desc>description1</desc1>
        <date>10-2014</date>
    </room>
    <room>
        <id>2</id>
        <name>room2</name>
        <desc>description2</desc1>
        <date>09-2014</date>
    </room>
    <room>
        <id>3</id>
        <name>room3</name>
        <desc>description3</desc1>
        <date>12-2014</date>
    </room>
    <room>
        <id>4</id>
        <name>room4</name>
        <desc>description4</desc1>
        <date>10-2014</date>
    </room>
</rooms>

因此,通过阅读关于stackexchange的其他文章,我设法做到了以下几点:

$xml = simplexml_load_file('./inc/rooms.xml');

$rooms = $xml->room;

$room = array();

foreach ($rooms as $unsortedroom){

    $room[] = $unsortedroom;

}

$ room现在应该是多维数组,并显示如下:

Array
(
    [0] => SimpleXMLElement Object
        (
            [id] => 1
            [name] => room1
            [desc] => description1
            [date] => 10-2014
    )
    [1] => SimpleXMLElement Object
        (
            [id] => 2
            [name] => room2
            [desc] => description2
            [date] => 09-2014
    )
    [2] => SimpleXMLElement Object
        (
            [id] => 3
            [name] => room3
            [desc] => description3
            [date] => 12-2014
    )
    [3] => SimpleXMLElement Object
        (
            [id] => 4
            [name] => room4
            [desc] => description4
            [date] => 10-2014
    )
)

我的问题是,例如,如何搜索此数组以显示该数组中日期为10-2014的所有数组? 因此,新数组如下所示:

Array
(
    [0] => SimpleXMLElement Object
        (
            [id] => 1
            [name] => room1
            [desc] => description1
            [date] => 10-2014
    )
    [1] => SimpleXMLElement Object
        (
            [id] => 4
            [name] => room4
            [desc] => description4
            [date] => 10-2014
    )
)

我在这里尝试了很多代码,以至于无法整日复制和粘贴所有我尝试过的代码。 最近三天一直在尝试。 他们似乎都没有工作。 开始认为也许这是不可能的。 非常感谢您对我的建议。 这可能真的很容易。 我不知道...无论如何,如果有人可以帮助我,那将使我无比沮丧。

尝试看一下: http : //php.net/manual/zh/function.array-search.php

例:

<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?>

刚刚注意到您正在使用数组中的对象让我更新。

更新资料

这样的事情应该可以解决问题

function arraySearch($array, $needle)
{
   foreach($array as $key => $obj)
   {
      if ( $obj->date === $needle )
         return $key; // or what ever you want to return $obj to return the whole object.
   }
   return false;
}

像这样调用此函数:

$arr[] = arraySearch($yourxmlarray, "10-2014");

更新2:

测试它现在可以正常工作:

<?php
$xml = simplexml_load_file('rooms.xml');

$rooms = $xml->room;
$room = array();
foreach ($rooms as $unsortedroom) {
    $room[] = $unsortedroom;
}

function arraySearch($array, $needle) {
    foreach($array as $key => $obj) {
        if ( $obj->date == $needle )
            $returnarr[] = $obj; // or what ever you want to return $obj to return the whole object.
        return $returnarr;
    }
    return false;
}

var_dump(arraySearch($room, "10-2014"));

编辑:
例:

$choosen=array();
for($a=0;a<count($room);$a++)
     if($room[$a]->date == $date)
          $choosen[]=$a;

打印:

Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)

如果要功能:

function search($arr,$needle){
    $choosen=array();
    for($a=0;a<count($room)$a++)
        if($room[$a]->date == $date)
            $choosen[]=$a;
    return $choosen;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM