note 73779 added to ref.array

From: Date: Fri, 09 Mar 2007 21:29:12 +0000
Subject: note 73779 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-124282@lists.php.net to get a copy of this message
I needed to make it so arrays that looked like this: <?php array( "cn" => "Bob", "bill", "bobby", "phoneNumber" => 5555555, 6666666, 7777777); ?> looked like this: <?php array( "cn" => array("bob","bill","bobby"), "phoneNumber" => array(5555555, 6666666, 7777777) ); ?> so I came up with this: <?php class arrayManipulation { function setPointer(&$array, $key) { //sets the internal array pointer to point to the index $key reset($array); //move pointer to first element $offSet = 0; while( $array[$key] != current($array) && current($array) ) { //loop through until the pointer is the value needed $offSet++; next($array); } return $offSet; } function collapseChildren($array) { //puts all integer keyed array elements into the preceeding string keyed element as an array (collapsing them) while($tmp = current($array)) { /* make a temporary array to make life easier */ $tmpArr = $array; /* put the pointer of the temporary array one more than the working array: */ $this->setPointer( $tmpArr, key($array) ); //synch the pointer up with working array next($tmpArr); //advance the pointer one if(key($tmpArr) !== NULL) { if( $nextIndex = key($tmpArr) ) { /* echo "<table border='1'><tr><td>Current Index:</td><td>".key($array)."</td></tr> <tr><td>Next Index:</td><td>".$nextIndex."</td></tr></table><br />"; */ // for debugging if(gettype($nextIndex) == "integer") { $array[key($array)] = array( $array[key($array)]); //save the current element's value into the sub-array we will be working with next /* loop through the integer elements and put them in the array, removing them as we go: */ do //using do loop because we alrready know the test condition is true {// collapse them! $array[key($array)][] = $array[$nextIndex]; //add the next (integer keyed element) value to the multi-dimm array unset($array[$nextIndex]); //remove the saved index /* go on to the next index: */ next($tmpArr); $nextIndex = key($tmpArr); } while( gettype($nextIndex) == "integer" ); //done collapsing sub elements } //done with integer type if statement } //done with saving the next index conditional } //done with making sure we have a non-null key conditional next($array); //move on to the next element(s) } //done with while loop(ing through the $array) return $array; } } ?> Hope this helps. It's not perfect so please improve uppon it if you want. ---- Server IP: 64.71.164.2 Probable Submitter: 128.143.60.168 ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- https://master.php.net/note/edit/73779 Del: integrated -- https://master.php.net/note/delete/73779/integrated Del: useless -- https://master.php.net/note/delete/73779/useless Del: bad code -- https://master.php.net/note/delete/73779/bad+code Del: spam -- https://master.php.net/note/delete/73779/spam Del: non-english -- https://master.php.net/note/delete/73779/non-english Del: in docs -- https://master.php.net/note/delete/73779/in+docs Del: other reasons-- https://master.php.net/note/delete/73779 Reject -- https://master.php.net/note/reject/73779 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124282) next »