0

I have an array that looks like this:

Array (
    [0] => Array ( [denumireObiect] => Telefon ) 
    [1] => Array ( [denumireObiect] => Laptop ) 
    [2] => Array ( [denumireObiect] => Tableta ) 
    [3] => Array ( [denumireObiect] => Obiect ) 
)

I am trying to take all of those words and make them options for a <select> tag.

This is the code I am using for that:

foreach ($result as $i) {
            echo '<option value = ', $result[$i],'>', $result[$i], '</option>';
          }

This paragraph gives me Illegal offset type error.

This is a var_dump($result) result.

array(4) { 
[0]=> array(1) { 
["denumireObiect"]=> string(7) "Telefon" 
} 

[1]=> array(1) 
{ ["denumireObiect"]=> string(6) "Laptop" } 

[2]=> array(1) {
["denumireObiect"]=> string(7) "Tableta" 
} 

[3]=> array(1) {
["denumireObiect"]=> string(6) "Obiect" } 
} 
...

EDIT:

I have tried doing it like this:

foreach ($result as $i => $val) {
        echo '<option value = ', $i,'>', $i, '</option>';
      }

and it returns 0, 1, 2, 3

Any help is appreciated!

5
  • is $result the array you mentioned and could you please provide a var_dump($result)? In your foreach you should be abled to access $i without referencing the $result array. But without knowing $result und $i I can not tell you how exactly Commented Sep 17, 2020 at 17:46
  • @j4g0 I have edited the post. added a var_dump result and tried a new way to do this. Hope it helps! Commented Sep 17, 2020 at 17:57
  • I have what feels like a very dumb question, but it might be an internationalization thing... does using , as a string concatenator work? AFAIK you need to use . (php.net/manual/en/language.operators.string.php), but maybe that's because I'm in the US. echo "test","ing"; vs echo "test"."ing"; - I would expect the first to throw an error, while the second would output testing. Commented Sep 17, 2020 at 18:26
  • 1
    i do not think it is concatenating but rather one echo outputting several strings. It threw me off at first, too :D Commented Sep 17, 2020 at 18:28
  • whoa, TIL. I had no idea you could list things like that to be echoed. Sure enough, it's documented on the echo page: php.net/manual/en/function.echo.php Commented Sep 17, 2020 at 18:38

1 Answer 1

1

@WOUNDEDStevenJones made a good point. I have updated my code to make it more readable. Note that you would have to rename the $result variable to $results.

Since every $result from your code is an associative array with the same "denumireObiect" key, you could get the values like this:

foreach ($results as $result) {
    echo "<option value=\"{$result['denumireObiect']}\">{$result['denumireObiect']}</option>";
}

If you do not like using the interpolation you could reformat the echo statement like this:

echo '<option value="' . $result['denumireObiect'] . '">' . $result['denumireObiect'] . '</option>';

or use your format from before :)

Sign up to request clarification or add additional context in comments.

3 Comments

This gives me Trying to access array offset on value of type int error.
I removed the => $val from the foreach, we do not need it
To be more clear, $i is usually used as the index counter in loops (i.e. for($i=0; $i<count($result); $i++)) so I would recommend keeping this style and either writing foreach ($result as $i => $val) and then using $val['denumireObiect'] in the loop (or $result[$i]['denumireObiect'], but that seems unnecessarily silly in this case). This way, $i is essentially still an index counter, which is about the only thing single character variable names should be used for.

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.