Re: Arrays
| From: | James Kaufman | Date: | Wed, 28 Apr 2004 12:41:44 +0000 |
| Subject: | Re: Arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-184861@lists.php.net to get a copy of this message | ||
On Wed, Apr 28, 2004 at 12:11:39PM +0300, Phpu wrote:
> I have a problem with arrays.
>
> I have this script:
>
> $query = "SELECT model_name, brand_name FROM products WHERE
> product_id='$product_id'";
> $result = mysql_query($query) or die("error in query - $query -".mysql_error());
> while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
> foreach ($line as $element) {
> $jelement[] = $element;
> }
> }
>
> i want each element to be stored in a different array.
>
> For ex: If i print now the array $jelement i have:
>
> model_name1-brand_name1
> model_name2-brand_name2
> .........
>
> I want 2 arrays: array1 will contain [model_name1,model_name2, ....] and array2
> [brand_name1,brand_name2......]
>
> How can i do that?
>
Try something like:
foreach ($line as $key=>$element) {
$model_array[] = $key;
$brand_array[] = $element;
}
or:
for ($i=0; $i<count($line); $i++) {
$model_array[$i] = $key;
$brand_array[$i] = $element
}
The second approach guarantees that element n of one array corresponds with
element n of the other array.
--
Jim Kaufman
Linux Evangelist
public key 0x6D802619
http://www.linuxforbusiness.net
---
The reason ESP, for example, is not considered a viable topic in contemporary
psychology is simply that its investigation has not proven fruitful...After
more than 70 years of study, there still does not exist one example of an ESP
phenomenon that is replicable under controlled conditions. This simple but
basic scientific criterion has not been met despite dozens of studies
conducted over many decades... It is for this reason alone that the topic
is now of little interest to psychology... In short, there is no
demonstrated phenomenon that needs explanation.
-- Keith E. Stanovich,
"How to Think Straight About Psychology", pp. 160-161