3 Replies - 809 Views - Last Post: 10 July 2010 - 12:13 PM Rate Topic: -----

#1 kevin_mchugh   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 75
  • Joined: 08-April 09

Can I test for the existance of a data structure?

Posted 10 July 2010 - 10:28 AM

Hello Again,

My question this time is fairly straightforward....I hope. Is there a simple way in PHP to test for the existence of a data structure?

I am parsing XML pages using a system of nested arrays. When the page only contains one element it returns one less array than it would normally. If you see what I mean.

So my question is can I test for an array that may only potentially exist then have it act a certain way depending on the outcome.

I was thinking of using a "Try/Catch" clause then trying to access the element and having some sort of status flag that would tell me whether it existed or not. Then using that status flag determine what code to run.

I tried using the Try/Catch method directly but it seemed to ignore it.

I REALLY hope this makes some kind of sense.

Thanks for any help, even if its just a reference to a certain part of the PHP documentation.

As always, apologies to any moderators I may upset for asking stupid questions in the wrong place.

Regards

Kevin

Is This A Good Question/Topic? 0
  • +

Replies To: Can I test for the existance of a data structure?

#2 ghillieLEAD   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 208
  • Joined: 08-March 10

Re: Can I test for the existance of a data structure?

Posted 10 July 2010 - 10:56 AM

EDIT: A knowledge of the language always helps, moopet seems to have this one for ya.


I'm not really sure what you are asking for, but I will try my best to answer your question with a bit of pseudo code.

// a function that returns an array from the given index of the nested array 
Array GetArray(NestedArray parent, int index)
{
    return parent[index];
}

...

int index = 0;
Array returnArray = GetArray(myNestedArray, index);

while (returnArray != NULL)
{
    // do something with the returned array

    index++;
    returnArray = GetArray(myNestedArray, index);
}


This post has been edited by ghillieLEAD: 10 July 2010 - 11:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 moopet   User is offline

  • binary decision maker
  • member icon

Reputation: 343
  • View blog
  • Posts: 1,189
  • Joined: 02-April 09

Re: Can I test for the existance of a data structure?

Posted 10 July 2010 - 10:59 AM

Well, you can use array_key_exists($key, $array) or isset($array[$key)) and it'll work on nested arrays.
If you're using the sort of pseudo-arrays an object like SimpleXML uses it might get annoyingly trickier.
But yes, you should be able to use a try.. catch block no problems.
Was This Post Helpful? 1
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

Re: Can I test for the existance of a data structure?

Posted 10 July 2010 - 12:13 PM

View Postmoopet, on 10 July 2010 - 05:59 PM, said:

But yes, you should be able to use a try.. catch block no problems.

try Ö catch is only useful, when you are working with exceptions. i.e. standard error messages donít interact with try Ö catch.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1