Philip Kirkbride
Philip Kirkbride

Reputation: 22879

For each in Array PHP

I want to echo out each date in my Array within a PHP file. Right now my code is

foreach ($result_array as $date) {
   echo $date;
}

which results in

ArrayArrayArrayArrayArrayArray

How can I modify my code to show each date rather then Array?

EDIT

I'm building the array like so...

    $result_array =  array();
    $result_set = mysql_query("SELECT DISTINCT `saledate` FROM `phoneappdetail` WHERE salebarn = 'OSI'");

    while ( $row = mysql_fetch_array($result_set) ) {
        $result_array[] = $row;
    }

Upvotes: 1

Views: 16937

Answers (1)

Zbigniew
Zbigniew

Reputation: 27594

It prints Array because $date is array, modify your script like this:

foreach ($result_array as $date) {
   echo $date['saledate'];
}

Read more on how mysql_fetch_array works, it returns array (indexed, associative or both), so you need to choose which field you want to use.

Note from manual:

Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

mysqli_fetch_array()

PDOStatement::fetch()

Upvotes: 8

Related Questions