note 102665 deleted from function.eval by peehaa

From: Date: Mon, 09 Dec 2013 11:58:41 +0000
Subject: note 102665 deleted from function.eval by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-197864@lists.php.net to get a copy of this message
Note Submitter: Jordan Rutty ---- This function is to automatically sort a 2 dimensional array (like that from mysql fetches) into unlimited dimension array's <?php /* @param $array mysql fetch array @param $row_key array of rowkeys to be used ex array('country', 'state', 'city'); */ function LoopRowKey($array, $row_key, &$result=null){ foreach($array as $key => $value){ $i=''; $row_key = (array)$row_key; foreach($row_key as $var){ if(isset($value[$var])){ $var = $value[$var]; $i .= "['$var']"; } } eval("\$result$i".'[]'." = \$value;"); } return $result; } //this function will turn: $array = array{ array{ 'id' => '1', 'country' => 'canada', 'state' => 'QC', 'city' => 'montreal', } array{ 'id' => '2', 'country' => 'canada', 'state' => 'ON', 'city' => 'toronto', } array{ 'id' => '3', 'country' => 'canada', 'state' => 'QC', 'city' => 'quebec', } }; $row_key = array('country', 'state'); $result = LoopRowKey($array, $row_key); //will give $result = array{ 'canada' => array{ 'QC' => { '0' => array{ 'id' => '1', 'country' => 'canada', 'state' => 'QC', 'city' => 'montreal', } '1' => array{ 'id' => '3', 'country' => 'canada', 'state' => 'QC', 'city' => 'quebec', } } } 'ON' => { '0' => array{ 'id' => '2', 'country' => 'canada', 'state' => 'ON', 'city' => 'toronto', } } }; ?>

« previous php.notes (#197864) next »