Organizing the response array

Asked

Viewed 50 times

2

How to organize the index of the 2nd dimension array?

Seeking

  $stmt = getConn()->query("SELECT hash,id,quantidade FROM pergaminhos");
  $resp = $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_COLUMN);

reply

Array
(
    [1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [2] => Array
        (
            [0] => 4
        )
)

desired response

Array
(
    [1] => Array
        (
            [1] => 1
            [2] => 1
            [3] => 1
        )

    [2] => Array
        (
            [4] => 1
        )
)

written form for better understanding

Array
(
    [valor_do_hash] => Array
        (
            [valor_do_id] => valor_do_quantidade
            [valor_do_id] => valor_do_quantidade
            [valor_do_id] => valor_do_quantidade
        )

    [valor_do_hash] => Array
        (
            [valor_do_id] => valor_do_quantidade
        )
)
  • But the goal is each key of each sub-array to increase in 1 ? Or is there any case where that is not true? And what is the intention in doing so? It would not be better when you use the change you want to the key before modifying ?

  • @Isac actually, he is already getting the result, so much so that I told him that, as he already has the result the correct is to delete my answer. For me to be able to delete the answer, he must uncheck it as correct/accepted.

2 answers

2

Use the function array_map to traverse the array and apply a function to each traversed element:

$Resposta = array_map(function($Valor) {
  // Faz alguma coisa com "$Valor"
}, $Resposta);

use the function array_values to take all values of the current element "$Valor":

$Chaves = array_values($Valor);

then use the function array_fill_keys to create an array, being the values of $Chaves as indices and 1 as the value of each element in that array:

return array_fill_keys($Chaves, 1);

ready, the complete code:

$Resposta = [
  1 => [
    0 => 1,
    1 => 2,
    2 => 3,
  ],
  2 => [
    0 => 4
  ]
];
$Resposta = array_map(function($Valor) {
  $Chaves = array_values($Valor);
  return array_fill_keys($Chaves, 1);
}, $Resposta);

if you give a print_r will have the exit:

Array
(
  [1] => Array
  (
    [1] => 1
    [2] => 1
    [3] => 1
  )

  [2] => Array
  (
    [4] => 1
  )
)

See working on repl.it

References

  • but ai not worth making a loop and organize n? ai is just hiding the loop... the loop in myql is not lighter n?

  • Actually I think I’ll do so look, I think I use less loop motors, since there in the fetchall I’m already using internal loop motors

  • $stmtz22 = getConn()->query("SELECT hash,id,quantity FROM scrolls"); $wwwwz = $stmtz22->fetchAll(PDO::FETCH_OBJ); $org_receita_padrao = []; foreach($wwz as $data){ $org_receita_padrao[$data->hash][$data->id] = $data->quantity; } print_r($org_receita_padrao);

  • to trying to pass these function to mysql

  • could not remove

2


$stmt = getConn()->query("SELECT hash,id,quantidade FROM pergaminhos");
$resp = $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_COLUMN);

$novaResp = array();

foreach ($resp as $linha) {
    $novaResp[$linha[0]][$linha[1]] = $linha[2];
}

print_r($novaResp);
  • a doubt fetchAll(PDO::FETCH_GROUP|PDO::FETCH_COLUMN) is php working or mysql? because if it is php it would be better to already work everything on foreach, because I do not know if I am sure more with the group and the column will activate 2 engines one time internal in the fetch and another in the foreach, ai axo lighter work in the foreach

Browser other questions tagged

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