Note that there are some explanatory texts on larger screens.

plurals
  1. POBuilding a multidimensional array on the fly
    text
    copied!<p>How do you build a multidimensional array on the fly? </p> <p>I know this is an open ended question, but I am having a hell of a time with it.</p> <p>What I have is the possibility of the a piece of an array needing to cause the array to add another child.</p> <pre><code>Array( [0] =&gt; Array ( [parentID] =&gt; 0 [component] =&gt; 23941/01 [decoration] =&gt; [hasChild] =&gt; T [proccessed] =&gt; T [cmp0] =&gt; Array ( [parentID] =&gt; 0 [component] =&gt; COLC01 [decoration] =&gt; [hasChild] =&gt; F [proccessed] =&gt; T ) [cmp1] =&gt; Array ( [parentID] =&gt; 0 [component] =&gt; PPH [decoration] =&gt; [hasChild] =&gt; F [proccessed] =&gt; T ) ) [1] =&gt; Array ( [parentID] =&gt; 1 [component] =&gt; 23930/20SB [decoration] =&gt; [hasChild] =&gt; T [proccessed] =&gt; T [cmp0] =&gt; Array ( [parentID] =&gt; 1 [component] =&gt; 23930/20 [decoration] =&gt; [hasChild] =&gt; T [proccessed] =&gt; F ) ) [2] =&gt; Array ( [parentID] =&gt; 2 [component] =&gt; SC070400SB [decoration] =&gt; [hasChild] =&gt; T [proccessed] =&gt; T [cmp0] =&gt; Array ( [parentID] =&gt; 2 [component] =&gt; SC070400 [decoration] =&gt; [hasChild] =&gt; F [proccessed] =&gt; T ) )) </code></pre> <p>The outer arrays [0,1,2] I generated with simple array pushes. When I start to traverse the array to get to the lower levels, I am having a problem getting more than a layer deep.</p> <p><strong>Here is my major question</strong>: How do I dynamically add elements to the array, while still being able to check for a key value to see if I should go deeper? Both the processed and hasChild flags tell me I can go deeper, but isset and array_key_exists doesn't seem to work on newly added elements. In fact when I print the array to the screen it doesn't even show any elements like [cmp0]...</p> <p>I am not sure how much my code is going to help, but I'll lay it out here for browsing.</p> <pre><code>function explodeBOM(&amp;$BOMArray,$library,$sonar){ $saveKey = ''; $childCounter = 0; $childKey = ''; foreach($BOMArray as &amp;$bomLine){ $childKey = 'cmp' . (string)$childCounter; while(array_key_exists($childKey,$bomLine)){ explodeBOM($bomLine[$childKey],$library,$sonar); } if($bomLine['proccessed'] == "F"){ $saveKey = $bomLine['parentID']; $bomItems = getBOM($bomLine['component'],$bomLine['decoration'],$library,$sonar); foreach($bomItems as $newBomLine){ $itemMasterInfo = getItemMasterInfo($newBomLine['BMCMP#'],$newBomLine['BMCMD#'],$library,$sonar); if($itemMasterInfo['ITPRTC'] != 'P' AND $itemMasterInfo['ITPRTC'] != 'C'){ // add it to the array $componetArray = array(); $componetArray['parentID'] = $saveKey; $componetArray['component'] = $newBomLine['BMCMP#']; $componetArray['decoration'] = $newBomLine['BMCMD#']; $componetArray['hasChild'] = parentOrChildBOM($newBomLine['BMCMP#'],$newBomLine['BMCMD#'],$library,$sonar); if ($componetArray['hasChild'] == 'F'){ $componetArray['proccessed'] = 'T'; }else{ $componetArray['proccessed'] = 'F'; } //array_push($bomLine,$componetArray); $childKey = 'cmp' . (string)$childCounter; $childCounter++; $bomLine[$childKey] = $componetArray; $bomLine['proccessed'] = 'T'; reset($BOMArray); } } $childCounter = 0; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload