Showing word 'Array' for each array values instead of value itself inside foreach loop - php

Showing word 'Array' for each array values instead of value itself inside foreach loop - php



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?


foreach


var_dump


echo



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?





Can you paste here output of var_dump($genders);
– Shujaat
Aug 22 at 18:06





Maybe because the values are inside another array. Can you show us your var_dump($gender).
– bastien
Aug 22 at 18:06



var_dump($gender)





"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.
– Patrick Q
Aug 22 at 18:09


var_dump()





$gender must still be an array, not a string. What do you get when you print_r($genders) (or var_dump if object)
– Thomas
Aug 22 at 18:11




3 Answers
3



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.


distinct(gender)


gen


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





That's what i am missing. Thanks a lot. Appreciate!
– John Caverns
Aug 22 at 18:10



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>
<? ?>



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế