Django Johnson
Django Johnson

Reputation: 1449

Trying to echo array's contents as <options>s inside a <select>

I have array that looks like this when I var_dump() it:

array(1) { ["bussiness"]=> array(5) { [0]=> string(8) "business" [1]=> string(9) "bossiness" [2]=> string(8) "busyness" [3]=> string(9) "bushiness" [4]=> string(9) "fussiness" } }

I am trying to get each of those elements to be an inside a single .

I thought it would be as simple as iterating through the array with a foreach and echoing each element in the array inbetween an and , but I am trying that and it doesn't seem to be working.

The output that I am getting is a with two s one says Array and the other is blank.

So the markup is like this:

<select>
    <option>Array</option>
    <option></option>
</select>

What am I doing wrong and why isn't the code echoing the array's contents. It is not even iterating through the entire array, just two items hence the two s.

Here is my code:

var_dump($bad_words);
echo "Did you mean: <select> \n";
foreach($bad_words as $suggestion){
    echo "<option>".$suggestion."</option> \n";
}
echo "</select>";

Upvotes: 0

Views: 745

Answers (3)

Chayemor
Chayemor

Reputation: 3707

If it helps you understand what array of arrays mean, have a read. The var_dump() you did is the answer to your question, let's have a look.

 array(1) { ["bussiness"]=> array(5) { [0]=> string(8) "business" [1]=> string(9) "bossiness" 
 [2]=> string(8) "busyness" [3]=> string(9) "bushiness" [4]=> string(9) "fussiness" } }

First we have array(1). This tells us that $bad_words is an array that contains 1 value (of what, we don´t yet know). Ok, so far so good. Then we have

["bussiness"]=> array(5)

This is the first and only element in your array $bad_words. What do we have? ["bussiness"] represents the key by which you can access the value of that element.

$bad_words['bussiness'] is { [0]=> string(8) "business" [1]=> string(9) "bossiness" [2]=> 
 string(8) "busyness" [3]=> string(9) "bushiness" [4]=> string(9) "fussiness" }

You can see then that the value accessed is another array containing 4 values, and the keys to each value of that array are numerical ones (0,1,2...).

Looking at your foreach loop here is what is happening:

foreach($bad_words as $suggestion){ --> $suggestion = array("business", "bossiness", "busy...);
    echo "<option>".$suggestion."</option> \n";
}

Hence the fact that when you try to print it out you get "Array". That´s why the answers posted above in one way or another acess the first element of $bad_words and loop over it´s values (since it's an array).

That´s the meaning of an array of arrays. A tip for next time, would be to do other kind of var_dumps() such as:

foreach($bad_words as $suggestion){
    var_dump($suggestion);
}

That might help you with figuring out what is going on.

Hope this helps dissolve a bit the tangle that arrays can be at times!

Upvotes: 0

jancha
jancha

Reputation: 4967

should be:

var_dump($bad_words);
echo "Did you mean: <select> \n";
foreach($bad_words["bussiness"] as $k=>$v){
    echo "<option value=\"$k\">".$v."</option> \n";
}
echo "</select>";

Upvotes: 1

Orangepill
Orangepill

Reputation: 24645

You are dealing with an array of arrays

Your code should read.

var_dump($bad_words);
echo "Did you mean: <select> \n";
foreach($bad_words["bussiness"] as $suggestion){
    echo "<option>".$suggestion."</option> \n";
}
echo "</select>";

If you only ever want to deal with the first element in the nested array you can use

var_dump($bad_words);
$term = array_shift($bad_words);
echo "Did you mean: <select> \n";
foreach($term as $suggestion){
    echo "<option>".$suggestion."</option> \n";
}
echo "</select>";

Upvotes: 3

Related Questions