note 73779 deleted from ref.array by nlopess

From: Date: Sat, 10 Mar 2007 10:44:44 +0000
Subject: note 73779 deleted from ref.array by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-124298@lists.php.net to get a copy of this message
Note Submitter: bennyhur at gmail dot com ---- 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.

« previous php.notes (#124298) next »