How to join two array variables into one in php?

Asked

Viewed 710 times

2

Good afternoon

I have the following code

<?php
include_once("con.php");

$pdo = conectar();

$data = file_get_contents("php://input");
$data = json_decode($data);

$idempresa = $data->idempresa;
$subcategoria = "Recebimento";

$buscaContaEntrada=$pdo->prepare('SELECT * FROM conta WHERE empresa_idempresa=:idempresa AND subcategoria=:subcategoria');
$buscaContaEntrada->bindValue('idempresa', $idempresa);
$buscaContaEntrada->bindValue('subcategoria', $subcategoria);
$buscaContaEntrada->execute();

while ($linha=$buscaContaEntrada->fetch(PDO::FETCH_ASSOC)) {

    $return[] = array(
        'idconta'   => $linha['idconta'],
        'conta' => utf8_encode($linha['conta']),
        'idsubcategoria'    => utf8_encode($linha['subcategoria_idsubcategoria']),
    );
}

echo json_encode($return);
?>

But I need to take the given idsubcategory, do another select, in the database, and the result, play within $Return... How can I do this?

  • Let me try to understand: you want to take the value of the key idsubcategoria and make an appointment sql with that amount ?

  • No, this I already do...@Magichat. What I want to do is unite this $Return[] to another one that I will mount.

  • Could you add how is Aria (var_dump) and how you want it to be? I think this would help.

3 answers

1

To unite 2 arrays in 1 you can use the function array_merge, example :

<?php
$array1 = array("cor" => "vermelho", 2, 4);
$array2 = array("a", "b", "cor" => "verde", "forma" => "trapezoide", 4);
$result = array_merge($array1, $array2);
echo json_encode($result);
?>
//saida
{"cor":"verde","0":2,"1":4,"2":"a","3":"b","forma":"trapezoide","4":4}
  • Dude, it’s actually supposed to be an Object array, json. I’ve used array_merge, but it’s not cool. The positions [0], [1]... should be with names, understand?

  • Good if I understand right now, see the issue...

  • Get what Ecisi @Magichat?

  • ah I made an edition, see the output, as it is the output that you would like ?

1


Dude, take off that first bracket

$return[] = array(

Vira

$return = array(

Because this way you are creating 2 levels of array (Return[0][data]) and when you will read in js is giving Object because it is actually Object same.

To simply merge two arrays is array_merge, but my answer there solves what you want

0

Creates a function that searches the data for the Idcategory and then adds its return to your array.

while ($linha=$buscaContaEntrada->fetch(PDO::FETCH_ASSOC)) {
    $novaVariavelParaInserirNoArray = buscarInformacoesNoBanco($linha['subcategoria_idsubcategoria']);//funcao que realiza sua busca.
    $return[] = array(
        'idconta'   => $linha['idconta'],
        'conta' => utf8_encode($linha['conta']),
        'idsubcategoria'    => utf8_encode($linha['subcategoria_idsubcategoria']),
        'informacoesTrazidasDoBanco' => $novaVariavelParaInserirNoArray
    );
}

I believe that should suffice.

  • In the console appears an array with 2 objects inside. [Object, Object]. I used the php array_merge command, but it was not cool

  • But how to add the return in the array?

  • you can treat the fetch return that the "lookup" function brings, thus causing it to be returned either an array or a value. If it is an array just use the function array_merge with the result of the "lookupNoBanco" function and its $Return. If on the other hand the return of this search you perform using the "subcategoria_idsubcategory" is a value, you can use the function array_push.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.