简体   繁体   中英

Search value in PHP multidimentional array from xml

I have an xml file which I've been trying to search through in PHP. The XML contains rooms and the months they're available to move into. Here is the 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>

So, from reading other posts on stackexchange I've managed to do the following:

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

$rooms = $xml->room;

$room = array();

foreach ($rooms as $unsortedroom){

    $room[] = $unsortedroom;

}

And $room is now supposedly a multidimensional array and displays as the following:

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

My question is how can I search through this array to display all arrays inside the array which have the date 10-2014, for instance? So a new array would look like the following:

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

I've tried so many codes on here that I couldn't possibly copy and paste all those I've tried as it'd take all day. Been trying it for the last three days. None of them seem to be working. Starting to think maybe it's impossible. I'd be extremely grateful for your comments on what I could do. It's probably really easy. I dunno... Anyways, if anyone can help me with this, it'd relieve me of so much frustration.

Try looking at this: http://php.net/manual/en/function.array-search.php

Example:

<?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;
?>

Just noticed you're using an object in an array let me update.

Update

Something like this should do the trick

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

Call this function like this:

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

Update 2:

Tested it this works now:

<?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"));

EDIT:
example:

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

Should prints:

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

If you want function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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