0

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"

1
  • 2
    Use print_r to print the array... print will always only print 'Array' Commented Mar 21, 2012 at 18:48

3 Answers 3

5

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

echo '<pre>'; print_r(array_values($msg)); echo '</pre>';
Sign up to request clarification or add additional context in comments.

1 Comment

This got the array to print but it also adds in this Array ( [0] => Array ( [0] => Inventory for Batteries - AA has fallen beneath threshold. 0 remaining. )
2

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).

Comments

0

I think you need to change the following code:

$msg[] = array($message); 

to

array_push($msg, $message);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.