Page 1 of 1

In Array, In Array

Posted: Sat Mar 03, 2007 8:43 pm
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!

Posted: Sat Mar 03, 2007 9:53 pm
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.

Posted: Sun Mar 04, 2007 7:15 am
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?

Posted: Sun Mar 04, 2007 9:17 am
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.