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

159
Views
PHP - Empuje los datos a la matriz en el bucle foreach

Quiero lograr el siguiente formato de matriz:

 { "success": true, "results": [ { "name" : "Choice 1", "value" : "value1", "text" : "Choice 1" }, { "name" : "Choice 2", "value" : "value2", "text" : "Choice 2" } ] }

Sin embargo, estoy usando PHP y un bucle foreach para devolver algunos valores de mi base de datos:

 //Search for clients in our database. $stmt = $dbh->prepare("SELECT * FROM customers"); $stmt->execute(); $showAll = $stmt->fetchAll();

Luego tengo mi primera parte de la matriz y mi bucle foreach:

 $data = array( "success" => false, "results" => array() ); foreach ($showAll as $client) { $data_array[] = array( 'name' => $client['name'], 'value' => $client['name'], 'text' => $client['name'] ); }

Arriba solo salidas:

 [ { "name":"Choice 1", "value":"value 1", "text":"Choice 1" }, { "name":"Choice 2", "value":"value2", "text":"Choice 2" } ]

Por lo tanto, falta la parte superior de mi matriz original, pero quiero recorrer los resultados de cada base de datos en "results": [ ... ]

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Prueba esto

 $data = array( "success" => false, "results" => array() ); foreach ($showAll as $client) { $data['results'][] = array( 'name' => $client['name'], 'value' => $client['name'], 'text' => $client['name'] ); } $data['success'] = true; // if you want to update `status` as well echo json_encode($data);
over 4 years ago · Santiago Trujillo Report

0

Después de crear la matriz $data_array , solo agregue algunas líneas que tengo en mi publicación.

Pruebe este fragmento de código aquí (con entrada de muestra)

 ini_set('display_errors', 1); foreach ($showAll as $client) { $data_array[] = array( 'name' => $client['name'], 'value' => $client['name'], 'text' => $client['name'] ); } // add these lines to your code. $result=array(); $result["success"]=true; $result["results"]=$data_array; echo json_encode($result);
over 4 years ago · Santiago Trujillo Report

0

intente esto ya que tiene una matriz dentro de $ data_array en la clave "Resultados", por lo que también debe usar "resultados" como clave y luego intentar insertar datos en esa matriz

 foreach ($showAll as $client) { $data_array["results"][] = array( 'name' => $client['name'], 'value' => $client['name'], 'text' => $client['name'] ); }
over 4 years ago · Santiago Trujillo 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!