Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's a monstrosity but it does what you want. Using push in a recursive function fails because the reference is destroyed after the second function call, so if you don't know how deep the key is there's a choice between using an arbitrary number of loops and hoping for the best, importing variables and using eval to push new values, or pulling apart and rebuilding the array. I chose eval. The description of what you wanted is a little different from pushing a value because you're looking for a key-value pair within an array, not an array. This function finds the key value pair and adds whatever you want as a sibling, if no value is specified the value is added as sibling to the first key matched. If no pushval is specified it will return the chain of keys that points to the matched key/key-value.</p> <p><code>$ref_array</code> is the multi-array to be modified, <code>$key</code> is the key you're looking for, <code>$val</code> is the value of the key you're looking for, <code>$newkey</code> is the new key that will reference the new value, <code>$pushval</code> is the new value to be indexed by <code>$newkey</code>.</p> <p><strong>DON'T</strong> pass an argument for the <code>$val_array</code> parameter. It's for use with recusive calls only. It's how the function distinguishes new calls from recursive calls, and it's how the function finds the key-value without disrupting the pass-by-reference.</p> <pre><code>function deepPush(&amp;$ref_array, $key, $val=null, $newkey=null, $pushval=null, $val_array=null) { static $r, $keys; #reset static vars on first call if(!$val_array){ $r = 0; $keys = array();} #cap recursion if($r &gt; 100){ trigger_error('Stack exceeded 100'); return;} #init val_array $val_array = ($r) ? $val_array : $ref_array; #specified search value??? $search_val = ($val!==null &amp;&amp; !in_array($val, $val_array)) ? true : false; if(!array_key_exists($key, $val_array) || $search_val) { $i=0;foreach($val_array as $k=&gt;$v){ if(gettype($v) == 'array') { if($i&gt;0){/*dead-end*/array_pop($keys); /*keep recusion accurate*/$r-=$i;} $keys[] = $k;/*build keychain*/ $r++; $i++; /*increment recursion, iteration*/ if(deepPush($ref_array, $key, $val, $newkey, $pushval, $v)){ /*close stack on 1st success*/return $keys;} }//if }//foreach }//if else{ if($pushval === null){return $keys;} #add $newkey to the keychain $keys[] = $newkey; #process $pushval based on type $pushval = (gettype($pushval) == 'string') ? sprintf("'%s'", $pushval) : var_export($pushval, true); #link keys together to form pointer $p = '$ref_array'; for($j=0,$c=count($keys); $j&lt;$c; $j++) { $k = $keys[$j]; $p .= "['$k']"; }//for #concat the value to be pushed $p .= sprintf("= %s;",$pushval); #push it eval($p); $keys = array(); return true; }//else } deepPush($array, 'id', 4, 'children', $addThis); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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