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

226
Views
Insertar matriz en una matriz codificada json en PHP

Tengo la función retIKey (..):

 function retIKey($text, $callback) { return json_encode([ 'inline_keyboard'=>[ [ ['text'=>"$text", 'callback_data'=>"$callback"] ] ], ]); }

Tengo alguna información que debería agregar a esta matriz. Quiero agregar ['text'=>"sql1", 'callback_data'=>"sql1"] en la matriz usando for loop. Por ejemplo, el resultado debería ser así después del bucle for:

 function retIKey($text, $callback) { return json_encode([ 'inline_keyboard'=>[ [ ['text'=>"$text", 'callback_data'=>"$callback"], ['text'=>"sql1", 'callback_data'=>"sql2"] ], [ ['text'=>"sql2", 'callback_data'=>"sal2"] ] ], ]); }

¿Qué tengo que hacer? ¿Cómo puedo hacer esto?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Su función devuelve la matriz json codificada. Entonces, para agregar valores a la matriz fuera de la función después, primero debe decodificar la matriz. Después de agregar los valores, puede codificar nuevamente si es necesario.

Funciona así:

 function retIKey($text, $callback) { return json_encode([ 'inline_keyboard' => [ [ 'text' => "$text", 'callback_data' => "$callback" ] ] ]); } $jsonArray = retIKey('sql1', 'sql1'); $array = json_decode($jsonArray, true); $array['inline_keyboard'][] = [ 'text' => 'sql2', 'callback_data' => 'sql2' ];

$array ahora se parece a:

 array (size=1) 'inline_keyboard' => array (size=2) 0 => array (size=2) 'text' => string 'sql1' (length=4) 'callback_data' => string 'sql1' (length=4) 1 => array (size=2) 'text' => string 'sql2' (length=4) 'callback_data' => string 'sql2' (length=4)

Si es necesario, puede codificar la matriz nuevamente después con:

 $jsonArray = json_encode($array);
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!