Joe
Joe

Reputation: 124

Add values to array via foreach

I'm trying to add values to an array via foreach but it only returns the word "Array" not the actual strings.

$msg = array();
foreach ($results as $result) {

     $inventory = $result->qoh;
     $inventoryOrder = $result->qo;
     $product = $result->item;
     $totalinv = $inventory+$inventoryOrder;
     if ($inventory <= $threshold) {
         $message = "Inventory for $product has fallen beneath threshold. $inventory remaining.\n";
         $msg[] = array($message); 

     }
}
print (array_values($msg));

I've tried a few different ways and everytime it returns the word "Array"

Upvotes: 0

Views: 1235

Answers (3)

biztiger
biztiger

Reputation: 1487

I think you need to change the following code:

$msg[] = array($message); 

to

array_push($msg, $message);

Upvotes: 0

vivek
vivek

Reputation: 2004

Use var_dump to see the values.

var_dump (array_values($msg));

var_dump will alway show you the type of the result too. Helps a lot in debugging. (Looking at your code, I'm assuming you are doing the same).

Upvotes: 2

dfsq
dfsq

Reputation: 193261

You should use print_r, not print. print is for stings only. Try this:

echo '<pre>'; print_r(array_values($msg)); echo '</pre>';

Upvotes: 5

Related Questions