note 102665 added to function.eval

From: Date: Mon, 28 Feb 2011 03:52:31 +0000
Subject: note 102665 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-177381@lists.php.net to get a copy of this message
This function is to automatically sort a 2 dimensional array (like that from mysql fetches) into unlimited dimension array's /* @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', } } }; ---- Server IP: 69.147.83.197 Probable Submitter: 75.152.22.75 ---- Manual Page -- http://www.php.net/manual/en/function.eval.php Edit -- https://master.php.net/note/edit/102665 Del: integrated -- https://master.php.net/note/delete/102665/integrated Del: useless -- https://master.php.net/note/delete/102665/useless Del: bad code -- https://master.php.net/note/delete/102665/bad+code Del: spam -- https://master.php.net/note/delete/102665/spam Del: non-english -- https://master.php.net/note/delete/102665/non-english Del: in docs -- https://master.php.net/note/delete/102665/in+docs Del: other reasons-- https://master.php.net/note/delete/102665 Reject -- https://master.php.net/note/reject/102665 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177381) next »