0

I know same question is asked before, and I tried that solution too. So anyone before giving downvote, it's request to go through my question. So I am trying to show the array values using foreach function. When I try to var_dump that value, it is showing the correct value, but when I am trying to echo it's value, it is just showing a word 'Array' for each of the values. Why so? What am I missing?

index.php

<? $genders = $db->execute("select distinct(gender) from clothing where pid = 1); ?>

The above query get the values M and F.

<? foreach ($genders as $gender) { ?>
   <p><? echo $gender['gen'] ?></p>
   <? //var_dump($gender); ?>
<? } ?>

The echo shows result: Array Array and var_dump shows result: M F

What I am missing?

4
  • Can you paste here output of var_dump($genders); Commented Aug 22, 2018 at 18:06
  • Maybe because the values are inside another array. Can you show us your var_dump($gender). Commented Aug 22, 2018 at 18:06
  • "var_dump shows result: M F". No, that's not what the output of var_dump() would be. You should show that the actual, exact output is. Commented Aug 22, 2018 at 18:09
  • $gender must still be an array, not a string. What do you get when you print_r($genders) (or var_dump if object) Commented Aug 22, 2018 at 18:11

3 Answers 3

2

It is returning an array of arrays, you need to access it like so:

<? $genders = $db->execute("select distinct(gender) gen from clothing where pid = 1); ?>

Note that I added an alias to distinct(gender) gen, for easy access.

<? foreach ($genders as $gender) { ?>
   <p><? echo $gender['gen'] ?></p>
<? } ?>
Sign up to request clarification or add additional context in comments.

1 Comment

That's what i am missing. Thanks a lot. Appreciate!
0

When the echo shows you the word 'Array', that means you are trying to print an array through the echo. whereas echo is not able to print array. In order print array you have to use print_r(array) or var_dump(array). Both of these functions are able to print array. That is why when you use var_dump it shows you the correct value.

Now apply same thing in foreach loop, you are tring to print an array through echo. You should add key in that array so that echo will be able to print it. Just like given below....

<? foreach ($genders as $gender) { ?>
       <p><?php echo $gender['gen'] ?></p> 
<? } ?>

Comments

0

Generally echo is used for simpler data types like string or ints. In case of arrays you should use var_dump or print_r which displays full information and this is what you are after. If you use echo on array you will get only an information that you are trying to display an array but you will not get the actual information you need. If you insist on using echo you can do it but you need to specify the index of an array you are interested in, for instance:

echo $array[0];

It will display for instance a string hello.

I've found this thread on quora if you need so more information.

https://www.quora.com/Learning-PHP-What-is-the-difference-between-var_dump-and-echo-statements

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.