Re: Dynamically Populating Multi-Dimensional Arrays

From: Date: Thu, 31 May 2012 16:34:28 +0000
Subject: Re: Dynamically Populating Multi-Dimensional Arrays
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-318043@lists.php.net to get a copy of this message
On Thursday, May 31, 2012, Mackintosh, Mike wrote: > Hi All, > > > > I was working on a project and needed to dynamically populate a > multi-dimensional array based on the size of an array. The script does > exactly what I want it to do, but I can't imagine that it's done in the > best way. I used an eval to construct the array based on the number of > values passed to the index array. When attempting to do the same with > Variable Variables, the addition of brackets caused the PHP Parser to > die. > > > > Does anyone have any suggestions or alternatives to achieving the same > results? > > > > Thanks, > > > > Mike > > > > Array: > > > > $array = array( > > array('name' => 'Person1', 'username' => > 'username1', > 'join_date' => 12233445566, 'state' => 'NJ'), > > array('name' => 'Person2', 'username' => > 'username2', > 'join_date' => 12233445566, 'state' => 'NJ'), > > array('name' => 'Person3', 'username' => > 'username3', > 'join_date' => 12233445996, 'state' => 'NY'), > > array('name' => 'Person4', 'username' => > 'username4', > 'join_date' => 12233445996, 'state' => 'NJ'), > > array('name' => 'Person5', 'username' => > 'username5', > 'join_date' => 12233445566, 'state' => 'NJ'), > > array('name' => 'Person6', 'username' => > 'username6', > 'join_date' => 12233445566, 'state' => 'NY'), > > array('name' => 'Person7', 'username' => > 'username7', > 'join_date' => 12233445776, 'state' => 'NY'), > > array('name' => 'Person8', 'username' => > 'username8', > 'join_date' => 12233445566, 'state' => 'NY'), > > array('name' => 'Person9', 'username' => > 'username9', > 'join_date' => 12233445996, 'state' => 'NJ'), > > ); > > > > > > Function: > > > > function createIndex($array, $index){ > > $index_array = array(); > > > > foreach($array as $result){ > > > > if(is_array($index)){ > > $key = '$index_array'; > > for($i=0;$i<=sizeof($index)-1;$i++){ > > You should move this sizeof($index) part out of the loop. It should look like this $len=sizeof($index); for($i=0;$i<$len;$i++) -- Shiplu.Mokadd.im ImgSign.com | A dynamic signature machine Innovation distinguishes between follower and leader

« previous php.general (#318043) next »