Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
Insert array into an json encoded array in PHP

I have function retIKey(..):

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

I have some information that I should add to this array. I want to add ['text'=>"sql1", 'callback_data'=>"sql1"] into the array using for loop. For example, the result should be like this after the for loop:

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

What should I do? How can I do this?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Your function return the array json encoded. So to add values to the array outside of the function afterwards, you have to decode the array first. After adding the values, you can encode again if needed.

It works like this:

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 now looks like:

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)

If needed, you can encode the array again afterwards with:

$jsonArray = json_encode($array);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda