Re: Multi-dimensional array issue

From: Date: Tue, 21 Aug 2001 13:27:50 +0000
Subject: Re: Multi-dimensional array issue
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-63697@lists.php.net to get a copy of this message
Ok, let me make sure I understand: I see that you included brackets after the $out ($out[] = array instead of $out = array). Does this denote that the $out is now a zero-based array? $out[0] is now an array that has $name, $srch, $sel and $case as single elements in that array. $out[1] is the next array with the same variable names, but different values. If I declare it as: $out[] = array("name"=>$name, "srch"=>$srch, "sel"=>$sel, "case"=>$case, $qvalues[] => array("vals" =>$vals, "valtypes" =>$valtypes)); can I access (for example) the $case variable in the 3rd $out array as $out[2][$case] or do I assign/access it as $out[2]["case"]? Here is what I'd like to do: (and how would you declare this?) I want 9 arrays (keyed as 0-8) for $out. (accessed as $out[0], $out[1], etc.) each of these arrays has individual $name, $srch, $sel, $case variables that are strings. I want to assign them individual values and access them as: $out[0][$name] = "some_text"; echo $out[0][$name]; {shows the "some_text" string value of the $name variable in the "zero-eth" element of the $out array} $out[4][$case] {shows the string value of the $case variable in the "fourth" element of the $out array} A step further now: As the fifth element in each of these 9 $out arrays I also need a sub-array that has identical properties to the $out array, but only has 2 elements in each of it's arrays. I want to access them the same way: $out[3][$qvalues[9][$vals] = "some_value"; will assign the text string "some_value" to the $vals variable inside the10th $qvalue array for the 4th $out array. I want to end up with something like this: $out=array [0] $name=string $srch=string $sel=string $case=string $qvalues=array [0] $vals=string $valtypes=string [1] $vals=string $valtypes=string [1] $name=string $srch=string $sel=string $case=string $qvalues=array [0] $vals=string $valtypes=string [1] $vals=string $valtypes=string {and so on for a few thousand times} I guess one of the main issues is whether I can access (for example) the $case variable in the $out array as [$case] or ["case"]. I don't need to initialize the string variables in the declaration of the array, but do I still need to access them as (for example) the ["case"] element of the $out array or can I access them as [$case]? Thanks for your help; I'm having to un-learn some of my C and Pascal, but not too much.....<g> "Tim Ward" <tim.ward@stivesdirect.com> wrote in message news:D2AC2CC91E72D511BD5600500416D3C0068C23@hunters-mail.itnetadmin.com... > The first thing is that 2-dimensional arrays don't really exist in php. Any > element of any array can be another array. The second important point is > that all php arrays are associative. If you don't declare the key of an > element (e.g. $out[] = "fred") when creating it you get the next integer > available as the key. Your initial declaration creates ... > > $out[0] = $name; > $out[1] = $srch; > $out[2] = $sel; > $out[3] = $case; > $out[$qvalues][0] = $vals; > $out[$qvalues][1] = $valtypes; > > with $name, etc. being whatever the variables are at the time the array is > initialised. > > What I think you wanted (I'm not entirely sure) is more along the lines of > .. > for ($i=0; $I < $num_out; $I++) > $out[] = array("name"=>$name, "srch"=>$srch, > "sel"=>$sel, > "case"=>$case, $qvalues => array($vals, $valtypes)); > > I'm not entirely sure what you're trying to do but I hope this points you in > the right direction. > > Tim > ---------- > From: Ken Hopkins [SMTP:hoppy@austin.rr.com] > Sent: 20 August 2001 21:02 > To: php-general@lists.php.net > Subject: Multi-dimensional array issue > > Hi from a recent PHP convert, > > Having a heck of a time declaring and accessing 2 dimensional info > in an array. > any input on where to start looking for the answer would be greatly > appreciated. > > I want to have a base array of 5 elements. The first 4 elements are > variables, and the fifth is an array of 2 elements. > seemed harmless enough at the time;) > > Coding conventions suggest access to the first level of elements is: > $basearray[base_numerical_offset][$basearray_variable] > and this works great for accessing the first 4 elements of the base > array. > > Accessing the fifth elements' variables in the next dimension is > where it fails for me: > > $basearray[base_numerical_offset][$subarray][sub_numerical_offset][$subarray > _variable] > > The documentation/snippets/samples, etc. have been to no avail; > still can't get to elements in the > second dimension properly. Attempts to populate the second level > array trahses the entire structure. > > I have the following declaration: > $out => array($name, $srch, $sel, $case, $qvalues => array($vals, > $valtypes); > $out is the name of the base array > $qvalues is the name of the sub-array under the base array > "$out" > I need for each $out array to consist of a name, srch, sel, case and > then a variable number of qvalues array elements that each have single vals > and valtypes elements. > > Use the following snippet to see what happens; it's not what I had > hoped to see. > I hope I'm just doing something wrong.. > > Thanks, Ken. > mailto:hoppy@austin.rr.com > > <? > > $out = array($name, $srch, $sel, $case, $qvalues => array($vals, > $valtypes)); > echo "<pre>"; > $out[0][$name] = "free"; > $out[1][$name] = "next"; > for ($i=0; $i<2; $i++) > { > echo "<br> OutIndex: ", $i, " Name: ", $out[$i][$name]; > } > > $out[0][$srch] = "outfreesrch"; > $out[0][$sel] = "outfreesel"; > $out[0][$case] = "outfreecase"; > $out[0][$qvalues][0][$vals] = "outfreeqval0"; > $out[0][$qvalues][0][$valtypes] = "outfreeqtype0"; > $out[0][$qvalues][1][$vals] = "outfreeqval1"; > $out[0][$qvalues][1][$valtypes] = "outfreeqtype1"; > echo "<br>Basearray index : ", 0, "\n"; > echo "<br> name : ", $out[0][$name], "\n"; > echo "<br> srch : ", $out[0][$srch], "\n"; > echo "<br> sel : ", $out[0][$sel], "\n"; > echo "<br> case : ", $out[0][$case], "\n"; > echo "<br> Subarray index : 0 \n"; > echo "<br> vals : ", > $out[0][$qvalues][0][$vals], "\n"; > echo "<br> valtypes : ", > $out[0][$qvalues][0][$valtypes], "\n"; > echo "<br> Subarray index : 1 \n"; > echo "<br> vals : ", > $out[0][$qvalues][1][$vals], "\n"; > echo "<br> valtypes : ", > $out[0][$qvalues][2][$valtypes], "\n"; > echo"<br><br>"; > $out[1][$srch] = "outnextsrch"; > $out[1][$sel] = "outnextsel"; > $out[1][$case] = "outnextcase"; > $out[1][$qvalues][0][$vals] = "outnextqval0"; > $out[1][$qvalues][0][$valtypes] = "outnextqtype0"; > $out[1][$qvalues][1][$vals] = "outnextqval1"; > $out[1][$qvalues][1][$valtypes] = "outnextqtype1"; > echo "<br>Basearray index : ", 1, "\n"; > echo "<br> name : ", $out[1][$name], "\n"; > echo "<br> srch : ", $out[1][$srch], "\n"; > echo "<br> sel : ", $out[1][$sel], "\n"; > echo "<br> case : ", $out[1][$case], "\n"; > echo "<br> Subarray index : 0 \n"; > echo "<br> vals : ", > $out[1][$qvalues][0][$vals], "\n"; > echo "<br> valtypes : ", > $out[1][$qvalues][0][$valtypes], "\n"; > echo "<br> Subarray index : 1 \n"; > echo "<br> vals : ", > $out[1][$qvalues][1][$vals], "\n"; > echo "<br> valtypes : ", > $out[1][$qvalues][1][$valtypes], "\n"; > echo "</pre>"; > ?>

« previous php.general (#63697) next »