cyberRobot Posted July 12, 2011 Share Posted July 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241833-quickly-modify-elements-in-a-multidimensional-array/ Share on other sites More sharing options...
requinix Posted July 12, 2011 Share Posted July 12, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/241833-quickly-modify-elements-in-a-multidimensional-array/#findComment-1241925 Share on other sites More sharing options...
djlee Posted July 12, 2011 Share Posted July 12, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/241833-quickly-modify-elements-in-a-multidimensional-array/#findComment-1241962 Share on other sites More sharing options...
cyberRobot Posted July 12, 2011 Author Share Posted July 12, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/241833-quickly-modify-elements-in-a-multidimensional-array/#findComment-1241969 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.