You are here: Re: [PHP] a basic array question! « PHP « IT news, forums, messages
Re: [PHP] a basic array question!

Posted by "Richard Lynch" on 07/01/05 02:11

On Sat, June 25, 2005 2:01 pm, bruce said:
> feel kind of foolish posting this.. but i can't seem to figure it out for
> now..

Here's a way to tackle this kind of thing in the future:

Divide and Conquer.

print_r($foo) prints out a BUNCH of stuff.

Focus only on the outer layer:

> i have an array, i can do a print_r($foo) and get the following:
> Array
> (
> [bookmark] => 1
> [facets] => Array
..
..
..


Then simply do:

print_r($foo['facets']);

Now you can ignore a bunch of crap you don't care about.

Focus on the outer layer and begin again:

Array
(
[0] => Array
(
[lastname] => <form id="facet-lastname">
<input type="hidden" name="list" value="" />
<input type="hidden" name="offset" value="0" />
<input type="hidden" name="orderBy" value="username" />
<input type="hidden" name="sort" value="asc" />
<strong>Name:</strong>
<input type="text" name="_lastname" value="" style="width: 125px" />
<input type="submit" value="Search" />
</form>
}

Okay, now we have:

print_r($foo['facets'][0]);


Array
(
[lastname] => <form id="facet-lastname">
<input type="hidden" name="list" value="" />
<input type="hidden" name="offset" value="0" />
<input type="hidden" name="orderBy" value="username" />
<input type="hidden" name="sort" value="asc" />
<strong>Name:</strong>
<input type="text" name="_lastname" value="" style="width: 125px" />
<input type="submit" value="Search" />
</form>
}

Finally, print_r($foo['facets'][0]['lastname'] is your answer.

It won't always be [] -- If you are using objects you might need -> in
there somewhere.

Figuring out where gets a lot easier if you focus on the Big Picture --
only the outer layer of your data structure, and drill down.

If at some point you know you need *ALL* the elements of an array, that's
easy enough.

That's the point at which you put in a loop to walk through them.

BUUUUUT

During your development, write the loop, see that it dumps out all the
elements, or at least a whole lot of stuff, and then stick an "exit;" at
the end, and focus on JUST the first element to tear that apart.

You can take the exit; out when you are all done, and it will work for all
the elements, assuming your data is well-formed.

In the meantime, focus on the outer layer, and focus on just ONE element
at a time, to avoid confusing yourself.

I do this all the time, especially when I'm dealing with somebody else's
structured data, and I don't really know what they've done.

--
Like Music?
http://l-i-e.com/artists.htm

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация