array_search is proving difficult on a complex array

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
RickyT23
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Nov 02, 2011 8:44 am

Thu Mar 22, 2012 3:58 pm

I have been set a college assignment for which I am building a faux directory of restaurants, using XML to store my directory. The XML file looks as follows:

Code: Select all

<?xml version="1.0"?>
<cumbrianRD>

		<Rrestaurant>
			<Rname>Steve's Steak House</Rname>
			<Raddress>Carlisle, Cumbria</Raddress>
			<Rtel>05432 12345</Rtel>
			<Rinfo>Steve's Steak House in Carlisle is one of the finest steak houses in the north of England, with their specialty: - award winning locally produced steaks. Located in the center of historic Carlisle, the grade 2 listed building was once a ... more</Rinfo>
			<Rindex>1</Rindex>
		</Rrestaurant>

		<Rrestaurant>
			<Rname>Fred's Fried Chicken</Rname>
			<Raddress>Windermere, Cumbria</Raddress>
			<Rtel>05432 543210</Rtel>
			<Rinfo>Fred's Fried Chicken is located in the heart of the lake district.  Established now for 15 years, they hae been bringing us Louisiana-style freid chicken like no other.  For a pleasant evening of soul-food and soul music, be sure to ... more</Rinfo>
			<Rindex>2</Rindex>
		</Rrestaurant>

		<Rrestaurant>
			<Rname>The Laughing Tiger</Rname>
			<Raddress>Carlisle, Cumbria</Raddress>
			<Rtel>05432 543210</Rtel>
			<Rinfo>For five years now The Laughing Tiger has been forging a reputation as one of the best Indian Restaurants in Cumbria, with a wide range of India's most delicious dishes ... more</Rinfo>
			<Rindex>3</Rindex>
		</Rrestaurant>
I am using the following script to pass the contents of the XML file into an array:

Code: Select all

function object2array($object){return@json_decode(@json_encode($object),1);}
$xml = simplexml_load_file("places.xml");
$a = object2array($xml);
If I use the print_r function on the array, it gives the following output:

Code: Select all

Array
(
    [Rrestaurant] => Array
        (
            [0] => Array
                (
                    [Rname] => Steve's Steak House
                    [Raddress] => Carlisle, Cumbria
                    [Rtel] => 05432 12345
                    [Rinfo] => Steve's Steak House in Carlisle is one of the finest steak houses in the north of England, with their specialty: - award winning locally produced steaks. Located in the center of historic Carlisle, the grade 2 listed building was once a ... more
                    [Rindex] => 1
                )

            [1] => Array
                (
                    [Rname] => Fred's Fried Chicken
                    [Raddress] => Windermere, Cumbria
                    [Rtel] => 05432 543210
                    [Rinfo] => Fred's Fried Chicken is located in the heart of the lake district.  Established now for 15 years, they hae been bringing us Louisiana-style freid chicken like no other.  For a pleasant evening of soul-food and soul music, be sure to ... more
                    [Rindex] => 2
                )

            [2] => Array
                (
                    [Rname] => The Laughing Tiger
                    [Raddress] => Carlisle, Cumbria
                    [Rtel] => 05432 543210
                    [Rinfo] => For five years now The Laughing Tiger has been forging a reputation as one of the best Indian Restaurants in Cumbria, with a wide range of India's most delicious dishes ... more
                    [Rindex] => 3
                )
)
)
Now I am having problems trying to search the array. Ideally I would be able to fill an array with the index of each of the restaurant's arrays if the Rinfo var contained just keywords from simple searches. I have tried using the following code to return the value of anything, but have had no luck whatsoever :(

I am a bit of a n00b when it comes to this, but I have tried looking on php.net and adapting bits of code i see there. Does anyone know how I could do this?

My last attempt of some code:

Code: Select all

function object2array($object){return@json_decode(@json_encode($object),1);}
$xml = simplexml_load_file("places.xml");
$a = object2array($xml);
print_r($a); // prints the array successfully
$searchr = array_search ('a', $a, false); // Have tried characters, words, exact entire and case sensitive contents from the arrays
print_r ($searchr); // shows nothing

RickyT23
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Nov 02, 2011 8:44 am

Fri Mar 23, 2012 11:37 pm

bump?! What Am I doing wrong. In sure that the problem is because the array has an extra parent container, - is there any way I can get rid of that container? Or a way of simpliflying the array but still being able to get the index of positive restaurants?

RickyT23
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Nov 02, 2011 8:44 am

Sat Mar 24, 2012 11:35 am

OK, I have managed to get rid of the extra level of array with the following statement:

Code: Select all

$b=$a['Rrestaurant']; 
I think I will be able to do it now :)

Silly, huh?

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests