从xml中搜索PHP多维数组中的值

时间:2014-09-09 09:33:06

标签: php arrays xml search

我有一个xml文件,我一直试图在PHP中搜索。 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
    )
)

我在这里尝试了很多代码,我无法复制并粘贴我尝试过的所有那些代码,因为它需要一整天。过去三天一直在尝试。他们似乎都没有工作。开始思考也许这是不可能的。我非常感谢你对我能做什么的评论。这可能很容易。我不知道......无论如何,如果有人可以帮助我,这会让我感到非常沮丧。

2 个答案:

答案 0 :(得分:1)

试着看看这个: http://php.net/manual/en/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"));

答案 1 :(得分:1)

修改
例如:

$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;
}