开发者

PHP - Pulling single value from multidimensional array

开发者 https://www.devze.com 2022-12-19 13:06 出处:网络
this开发者_StackOverflow社区 is probably quite simple but I could do with some help. I am trying to create a small PHP function that will display a single value form a multidimensional array when the

this开发者_StackOverflow社区 is probably quite simple but I could do with some help.

I am trying to create a small PHP function that will display a single value form a multidimensional array when the user used two dropdown boxes to select the row and column of the array.

So, the user will make a selection from the first dropdown box, which will contain the titles of the rows, and then make a selection from a second dropdown box, which will contain the titles of the columns. Once the selections have been made, the function then needs to output the value for the specific row and column selected.

I thought I had created an array that would work but, sadly no. I have 6 rows and 6 columns in my data table.

Also, is there a JQuery or Javascript alternative?

Just looking for a few pointers to get me on my way.

Thanks in advance,

Micanio


You could either do this on the server-side or through JS.

JS: Have the script update a hidden form field with the value using the onChange() event of the drop downs. Then simply grab that hidden field when the form is posted back to the server (of source always checking for valid data).

PHP: The form will provide the two values $_POST['field1'] and $_POST['field2'] (which of course you will sanitize before using). The script could define a multidimensional array that you could feed those two values into:

$finalValue = $mdArray[$SanitizedField1][$SanitizedField2];

From there just store the $finalValue however you'd like.


$data = array();
for ($i=0;$i<6;$i++) {
    $data[$i] = array();
    for ($j=0;$j<6;$j++)
        $data[$i][$j] = rand(0,100);
}

This should create an array similar to what you have described.

You can then access it like so...

echo $data[0][3];

Your form would need two select fields, both will values 0-5 then you can take the both of them and use them to access a value in your array.


If I understand your question correctly, you need something like a user selects a drop down list. Once selected, the option populates a second list. A real world example would be a user selects on Country to fill in a form and it will populates the States drop down list.

This kind of functionality is usually done with javascript not server side php.

http://javascript.internet.com/forms/country-state-drop-down.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号