Hi there guys,

I would truly appreciate any help you could give me on this:

PHP/MySql

I have a form that posts an array back to itself:

The post array looks like this:

Array ( [0] => 1 [1] => 2)

I then loop through the array as $type:

The query:

SELECT i.name, d.price, d.date FROM data d INNER JOIN items i ON (d.item_id = i.id) WHERE i.id = '.$type.' ORDER BY i.name

For each of the array I get this:

peanuts, 1000, 20100401
peanuts, 1100, 20100402
tomatoes, 400, 20100401
tomatoes, 401, 20100402

etc

What I need in the end is an array that looks like this:

Array ( [Peanuts] => Array ( [20100401] => 1000 [20100402] => 1100 ) [Tomatoes] => Array ( [20100401] => 400 [20100402] => 401 ) )

Can anyone help me to construct the query/loop that will achieve this?
If you could just use the above data, it would be perfect.

Any help would be sincerely appreciated and the person(s) who help with this will get an honorable mention and link in the source.

Cheers

Jacques

    Well, you're building a multidimensional array. You simply create values like this:

    $array_name[peanuts][20100401]=1000;

    Does this help?

      for a start run the query once, not once for each array value.

      WHERE i.id in(list of ids)

      loop the array to create the list of id's

        4 days later
        <?php
        
        // this represents the equivalent of mysql_fetch_assoc
        $results = array(
            array('name' => 'peanuts', 'price' => 1000, 'date' => 20100401),
            array('name' => 'peanuts', 'price' => 1100, 'date' => 20100402),
            array('name' => 'tomatoes', 'price' => 400, 'date' => 20100401),
            array('name' => 'tomatoes', 'price' => 401, 'date' => 20100402),
        );
        
        $price_list = array();
        foreach($results as $row) {
            if(!isset($price_list[$row['name']])) $price_list[$row['name']] = array();
            $price_list[$row['name']][$row['date']] = $row['price'];
        }
        
        var_dump($price_list);
        
        ?>
        
          Write a Reply...