Re: Multi-dimensional array issue >solved<

From: Date: Tue, 21 Aug 2001 19:39:23 +0000
Subject: Re: Multi-dimensional array issue >solved<
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-63758@lists.php.net to get a copy of this message
Thanks; after some all-nighter testing have come up with a solution. Will definitely have to access the individual variables as ("quoted_strings") instead of ($variable). Looks like this works: <? $out = array(0 => array("name", "srch", "sel", "case", "qvalues" => array(0 => array("values", "valtypes"), 1 => array("values", "valtypes"), 2 => array("values", "valtypes"), 3 => array("values", "valtypes"), 4 => array("values", "valtypes"), 5 => array("values", "valtypes"), 6 => array("values", "valtypes"), 7 => array("values", "valtypes"), 8 => array("values", "valtypes"), 9 => array("values", "valtypes")), 1 => array("name", "srch", "sel", "case", "qvalues" => array(0 => array("values", "valtypes"), 1 => array("values", "valtypes"), 2 => array("values", "valtypes"), 3 => array("values", "valtypes"), 4 => array("values", "valtypes"), 5 => array("values", "valtypes"), 6 => array("values", "valtypes"), 7 => array("values", "valtypes"), 8 => array("values", "valtypes"), 9 => array("values", "valtypes")), ))); echo "<pre>"; $out[0]["srch"] = "outfreesrch"; $out[0]["sel"] = "outfreesel"; $out[0]["case"] = "outfreecase"; $out[0]["qvalues"][0]["values"] = "outfreeqval0"; $out[0]["qvalues"][0]["valtypes"] = "outfreeqtype0"; $out[0]["qvalues"][1]["values"] = "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]["values"], "\n"; echo "<br> valtypes : ", $out[0]["qvalues"][0]["valtypes"], "\n"; echo "<br> Subarray index : 1 \n"; echo "<br> vals : ", $out[0]["qvalues"][1]["values"], "\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]["values"] = "outnextqval0"; $out[1]["qvalues"][0]["valtypes"] = "outnextqtype0"; $out[1]["qvalues"][1]["values"] = "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]["values"], "\n"; echo "<br> valtypes : ", $out[1]["qvalues"][0]["valtypes"], "\n"; echo "<br> Subarray index : 1 \n"; echo "<br> vals : ", $out[1]["qvalues"][1]["values"], "\n"; echo "<br> valtypes : ", $out[1]["qvalues"][1]["valtypes"], "\n"; echo "</pre>"; ?>  "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 (#63758) next »