Jump to content

Quickly modify elements in a multidimensional array


cyberRobot

Recommended Posts

I currently have a multidimensional array that looks like:

 

<?php
//...

$sectionArray = array(
				  array(
						'label'       => 'Training and Education',
						'subSections' => array(
											   array(
													 'label'      => 'All Centers:',
													 'id'         => 'trainEdu_allCenters',
													 'checkFound' => false
											   )
										 )
				  ),
				  array(
						'label'       => 'Technical Assistance',
						'subSections' => array(
											   array(
													 'label'      => 'Activities that do not require extensive technical or engineering expertise:',
													 'id'         => 'techAssist_laymanExp',
													 'checkFound' => false
											   ),
											   array(
													 'label'      => 'Activities that require engineering expertise:', 
													 'id'         => 'techAssist_engExp',
													 'checkFound' => false
											   )
										 )
				  ),
//...
?>

 

 

The current setup seems to work well for displaying the list of main and sub sections. But the issue I'm having is updating the checkFound part of the array. For example, when someone fills out a form and they check one of the "techAssist_laymanExp" boxes, I want the checkFound element for that id to be set to true.

 

Note that I know that I could use a foreach loop, but I was hoping there was an array function(s) that could help out. Something that could quickly find the id and allow me to jump to the corresponding checkFound element.

 

 

For what it's worth, my checkboxes currently look like this:

 

<input type='checkbox' name='techAssist_laymanExp22' id='techAssist_laymanExp22' />

 

Where "techAssist_laymanExp" comes from the array above and "22" indicates the ID of the checkbox in the database.

Link to comment
Share on other sites

I don't know of any such function. After all, a loop (or two) can do it easily enough.

$id = "techAssist_laymanExp";
foreach ($sectionArray as $k1 => $section) foreach ($sections["subSections"] as $k2 => $subsection) if ($subsection["id"] == $id) $sectionArray[$k1]["subSections"][$k2]["checkFound"] = true;

Link to comment
Share on other sites

Unless theres a way of passing the array key's through aswell then you've pretty much got to do as suggested and search through it manually. But that wouldn't be very reliable or secure for that fact.

 

The only other thing you could do is maintain an array of pointers. I imagine you generate the array dynamically. so Yafter creating the array of which you need a ponter to its values you could do something like

 

$pointers[$array['id']] = &$sectionArray[1]['subSections'][0];

This would be created whenever you add an array containing a label,id and checkvalue. and the first and second array keys [1] and [0] in the example, would be found from the current loop that populates the main array.

 

Because there simply pointers you can just then do

$id = "techAssist_laymanExp"; //-- passed from form

$pointers[$id]['checkValue']=true;

 

I like the pointers way as i tend to think that creating a second array on initialisation of the code is better and more "future proof" in regards to performance than looping. However its not suitable in all applications, really just depends on how your array is generated

 

[/code]

Link to comment
Share on other sites

Thanks for the suggestions requinix and djlee! Given the impending deadline, I'm planning to use the foreach loops.

 

My main concern with using the foreach loops is that my form currently has 80 checkboxes. Since most of the checkboxes fall under the same sub-section ID, the foreach loops would execute more times than necessary. But I was able to limit the number of executions by creating a seperate array to track which sub-section IDs were already changed.

 

Thanks again! :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.