Re: array manipulation

From: Date: Fri, 17 Apr 2009 02:26:01 +0000
Subject: Re: array manipulation
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-291599@lists.php.net to get a copy of this message
Sorry, bout that. My brain wasn't working right. It was a lot simpler than I thought. Forget the foreach. I used a counter. :-[ PJ wrote: > The more I get into arrays, the less I understand. > I have a ridiculously simple task which excapes me completely. > I need to ouput all fields in 1 column from 1 table in two phases sorted > alphabetically. > So, I have the query, I have the results. But since I need to split the > list into 2 parts, I need indexes. I cannot use the table index as it > does not correspond to the alphabetical order of the data column. In > order to get the index, I sort the resulting array and that now gives me > 34 arrays each containing an array of my results. Wonderful! > But how do I now extract the arrays from the array? > > Here is what I'm trying to do: > > $SQL = "SELECT category > FROM categories > ORDER BY category ASC > "; > $category = array(); > if ( ( $results = mysql_query($SQL, $db) ) !== false ) { > while ( $row = mysql_fetch_assoc($results) ) { > $category[$row['category']] = $row; > } > sort($category); > //var_dump($category); > echo "<table >"; > $count = mysql_num_rows($results); > $lastIndex = $count/2 -1; echo $lastIndex; > $ii = 0; > $cat = ''; > //print_r($category['0']['category']); > foreach($category as $index => $value) { > $ii++; > if ($ii != $lastIndex) { > $cat .= "$value, "; > } > else { > $cat .= " & $value<br />"; > } > $catn = preg_replace("/[^a-zA-Z0-9]/", "", $cat); > //echo "<pre>$category</pre>"; > echo "<tr> > <td><a href='../categories/", $catn, > ".php'>", $cat, "</a> > </td> > </tr>" ; > } > } > echo "</table>"; > > What should I be using in the foreach line? > Please help! > > -- unheralded genius: "A clean desk is the sign of a dull mind. " ------------------------------------------------------------- Phil Jourdan --- pj@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php

« previous php.general (#291599) next »