In Array, In Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

In Array, In Array

Post by tecktalkcm0391 »

I have an array like this:

Code: Select all

$array = array(array("Dog", "1", "1"), array("Cat", "1", "1"), array("Bird", "1", "1"));
I want to see if "Dog" is in the array's array ($array[0][0]) and somehow it tell me it's location so I can do some stuff with it.

Also to say change "Dog", "1" to "Dog", "2", how would I change the 1 to 2... $array[0][2]?

Thanks!
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Hmm.. what I suspect is that you should be using objects to represent these data-types, but that's more theory.

I think the way to do this is brute-force search it. Do a foreach() loop on the outer array and then another foreach() loop on the inner array: the indices will tell you where it is.

As for the second question, I'm not sure what you mean. $array[0][1] = "2" should do the trick.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Ambush Commander wrote:Hmm.. what I suspect is that you should be using objects to represent these data-types, but that's more theory.

I think the way to do this is brute-force search it. Do a foreach() loop on the outer array and then another foreach() loop on the inner array: the indices will tell you where it is.

As for the second question, I'm not sure what you mean. $array[0][1] = "2" should do the trick.
What do you mean by using objects to represent these data-types?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Objects, defined as a class construct whereby it may have one or more instance variables, sometimes called properties (or data members), and one or more functions, called methods.

There is a potential for two classes here. One for the individual data elements (the stored arrays) and one for the outer array providing functionality like searching for something in the contents.
Post Reply