Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
utilize PHP array as HTML selection

What I got.

I got an PHP which performs a user LDAP query and stores the result in an array. Within the PHP I JSON_encode($myArray) the array and pass this to an JavaScript file with print_r($myArray). In JavaScript I fill a HTML selection with the myArray as source.

The PHP results looks like, which is fine:

["Mickey Mouse","Donald Duck","Minnie Mouse"]

Whats the problem?

Usually I would fill a source based selection like this, which works with JSON files but not in this case:

var fillUserSelection

for (var key in myArray) {
   fillUserSelection += "<option>" + myArray[key] + "</option>"
}
document.getElementbyId("").innerHTML = fillUserSelection

I expected Mickey Mouse, Donald Duck and Minnie Mouse as options. Instead I receive each char as options. Like [,",M,i,c,k,e,y.. etc.

What I want.

I want only Mickey Mouse, Donald Duck and Minnie Mouse as options. What do I miss?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try something like this.

var myArray = JSON.parse(<?php echo json_encode($phpArray) ?>);

Now your array will work.

about 4 years ago · Juan Pablo Isaza Report

0

    <?php $myarray = array("Mickey Mouse","Donald Duck","Minnie Mouse"); ?>
    
    <?php json_encode($myarray); ?>
    
    
      <?php for ($i = 0; $i < count($myarray); $i++) {?>
      
      <?php echo "$myarray[$i] <br/>"; ?>
      
      <?php } ?>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!