Add empty items at the end of array

Asked

Viewed 53 times

-1

This is what I need to do, and I’m not up to it. I have to create an array to generate a json output with 50 items for example, but if the database contains only 30 it completes the 50 items with empty values. With the same Keys, but no values.

while ($rowx = $stmtx->fetch(PDO::FETCH_ASSOC)){

                    extract($rowx);

                    $emp_itemx=array(
                        "idhis" => $idhis,      
                        "id_user_cartao" => $id_user_cartao,
                        "ponto" => $ponto,
                        "id_user" => $id_user,
                        "qtde_pontos" => $qtde_pontos,
                        "iduserpromo" => $iduserpromo
                    );

                }

                echo json_encode($emp_itemx);

  • Why this line of PDO doesn’t look like this: $rowx = $stmtx->fetchAll(); since several copies of bd will come, fetchAll is more indicated

  • thus it would not be necessary to call the function Extract since the result would already come like $emp_itemx= array([0] =>array( "idhis" => "xxxx", "id_user_card" => "xxxx", "dot" => "xxxx", "id_user" => "xxxx", "qtde_points" => "xxxx", "iduserpromo" => "xxxx") );

  • Marcos, thanks for the tip, but my problem is as I described above, the question of the database data I’m not having problems. I really need to complete the blank data in the array!!

  • the array you represented is wrong, it is easier to iterate over the return using a foreach at the end rather than while

  • back Array ( [0] => Array ( [idhis] => 'xxx', [id_user_card] => 'xxx', [dot] => 'xxx', [id_user] => 'xxx', [qtde_points] => 'xxx', [idupromoser] => 'xxx'&#Xa#; )

  • All right, but how could I complete the result I need?

Show 1 more comment

2 answers

1

Fill an array with a given value through the function array_fill() and combine two or more arrays by function array_merge()

<?php
$array1 = []; 

//Define um elemento vazio para posterior preenchimento
$vazio = [
  "idhis" => null,      
  "id_user_cartao" => null,
  "ponto" => null,
  "id_user" => null,
  "qtde_pontos" => null,
  "iduserpromo" => null
];

//Simula um preenchimento de 30 itens de $array1
foreach(range(1,30) as $n){
   $array1[] = [
    "idhis" => -$n,      
    "id_user_cartao" => $n,
    "ponto" => $n * 10,
    "id_user" => $n * 100,
    "qtde_pontos" => $n * 1000,
    "iduserpromo" => $n * 10000
  ];
}

//Cria um array 20 elementos definidos como sendo vazios
$array2 = array_fill(0, 20, $vazio);

//Junta $array1 e $array2
$resultado = array_merge($array1 , $array2);

print_r($resultado);

Test code running on https://repl.it/repls/RemarkableWatchfulDemand

Array
(
    [0] => Array
        (
            [idhis] => -1
            [id_user_cartao] => 1
            [ponto] => 10
            [id_user] => 100
            [qtde_pontos] => 1000
            [iduserpromo] => 10000
        )

    [1] => Array
        (
            [idhis] => -2
            [id_user_cartao] => 2
            [ponto] => 20
            [id_user] => 200
            [qtde_pontos] => 2000
            [iduserpromo] => 20000
        )

    [2] => Array
        (
            [idhis] => -3
            [id_user_cartao] => 3
            [ponto] => 30
            [id_user] => 300
            [qtde_pontos] => 3000
            [iduserpromo] => 30000
        )

    [3] => Array
        (
            [idhis] => -4
            [id_user_cartao] => 4
            [ponto] => 40
            [id_user] => 400
            [qtde_pontos] => 4000
            [iduserpromo] => 40000
        )

    [4] => Array
        (
            [idhis] => -5
            [id_user_cartao] => 5
            [ponto] => 50
            [id_user] => 500
            [qtde_pontos] => 5000
            [iduserpromo] => 50000
        )

    [5] => Array
        (
            [idhis] => -6
            [id_user_cartao] => 6
            [ponto] => 60
            [id_user] => 600
            [qtde_pontos] => 6000
            [iduserpromo] => 60000
        )

    [6] => Array
        (
            [idhis] => -7
            [id_user_cartao] => 7
            [ponto] => 70
            [id_user] => 700
            [qtde_pontos] => 7000
            [iduserpromo] => 70000
        )

    [7] => Array
        (
            [idhis] => -8
            [id_user_cartao] => 8
            [ponto] => 80
            [id_user] => 800
            [qtde_pontos] => 8000
            [iduserpromo] => 80000
        )

    [8] => Array
        (
            [idhis] => -9
            [id_user_cartao] => 9
            [ponto] => 90
            [id_user] => 900
            [qtde_pontos] => 9000
            [iduserpromo] => 90000
        )

    [9] => Array
        (
            [idhis] => -10
            [id_user_cartao] => 10
            [ponto] => 100
            [id_user] => 1000
            [qtde_pontos] => 10000
            [iduserpromo] => 100000
        )

    [10] => Array
        (
            [idhis] => -11
            [id_user_cartao] => 11
            [ponto] => 110
            [id_user] => 1100
            [qtde_pontos] => 11000
            [iduserpromo] => 110000
        )

    [11] => Array
        (
            [idhis] => -12
            [id_user_cartao] => 12
            [ponto] => 120
            [id_user] => 1200
            [qtde_pontos] => 12000
            [iduserpromo] => 120000
        )

    [12] => Array
        (
            [idhis] => -13
            [id_user_cartao] => 13
            [ponto] => 130
            [id_user] => 1300
            [qtde_pontos] => 13000
            [iduserpromo] => 130000
        )

    [13] => Array
        (
            [idhis] => -14
            [id_user_cartao] => 14
            [ponto] => 140
            [id_user] => 1400
            [qtde_pontos] => 14000
            [iduserpromo] => 140000
        )

    [14] => Array
        (
            [idhis] => -15
            [id_user_cartao] => 15
            [ponto] => 150
            [id_user] => 1500
            [qtde_pontos] => 15000
            [iduserpromo] => 150000
        )

    [15] => Array
        (
            [idhis] => -16
            [id_user_cartao] => 16
            [ponto] => 160
            [id_user] => 1600
            [qtde_pontos] => 16000
            [iduserpromo] => 160000
        )

    [16] => Array
        (
            [idhis] => -17
            [id_user_cartao] => 17
            [ponto] => 170
            [id_user] => 1700
            [qtde_pontos] => 17000
            [iduserpromo] => 170000
        )

    [17] => Array
        (
            [idhis] => -18
            [id_user_cartao] => 18
            [ponto] => 180
            [id_user] => 1800
            [qtde_pontos] => 18000
            [iduserpromo] => 180000
        )

    [18] => Array
        (
            [idhis] => -19
            [id_user_cartao] => 19
            [ponto] => 190
            [id_user] => 1900
            [qtde_pontos] => 19000
            [iduserpromo] => 190000
        )

    [19] => Array
        (
            [idhis] => -20
            [id_user_cartao] => 20
            [ponto] => 200
            [id_user] => 2000
            [qtde_pontos] => 20000
            [iduserpromo] => 200000
        )

    [20] => Array
        (
            [idhis] => -21
            [id_user_cartao] => 21
            [ponto] => 210
            [id_user] => 2100
            [qtde_pontos] => 21000
            [iduserpromo] => 210000
        )

    [21] => Array
        (
            [idhis] => -22
            [id_user_cartao] => 22
            [ponto] => 220
            [id_user] => 2200
            [qtde_pontos] => 22000
            [iduserpromo] => 220000
        )

    [22] => Array
        (
            [idhis] => -23
            [id_user_cartao] => 23
            [ponto] => 230
            [id_user] => 2300
            [qtde_pontos] => 23000
            [iduserpromo] => 230000
        )

    [23] => Array
        (
            [idhis] => -24
            [id_user_cartao] => 24
            [ponto] => 240
            [id_user] => 2400
            [qtde_pontos] => 24000
            [iduserpromo] => 240000
        )

    [24] => Array
        (
            [idhis] => -25
            [id_user_cartao] => 25
            [ponto] => 250
            [id_user] => 2500
            [qtde_pontos] => 25000
            [iduserpromo] => 250000
        )

    [25] => Array
        (
            [idhis] => -26
            [id_user_cartao] => 26
            [ponto] => 260
            [id_user] => 2600
            [qtde_pontos] => 26000
            [iduserpromo] => 260000
        )

    [26] => Array
        (
            [idhis] => -27
            [id_user_cartao] => 27
            [ponto] => 270
            [id_user] => 2700
            [qtde_pontos] => 27000
            [iduserpromo] => 270000
        )

    [27] => Array
        (
            [idhis] => -28
            [id_user_cartao] => 28
            [ponto] => 280
            [id_user] => 2800
            [qtde_pontos] => 28000
            [iduserpromo] => 280000
        )

    [28] => Array
        (
            [idhis] => -29
            [id_user_cartao] => 29
            [ponto] => 290
            [id_user] => 2900
            [qtde_pontos] => 29000
            [iduserpromo] => 290000
        )

    [29] => Array
        (
            [idhis] => -30
            [id_user_cartao] => 30
            [ponto] => 300
            [id_user] => 3000
            [qtde_pontos] => 30000
            [iduserpromo] => 300000
        )

    [30] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [31] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [32] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [33] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [34] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [35] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [36] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [37] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [38] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [39] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [40] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [41] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [42] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [43] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [44] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [45] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [46] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [47] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [48] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

    [49] => Array
        (
            [idhis] => 
            [id_user_cartao] => 
            [ponto] => 
            [id_user] => 
            [qtde_pontos] => 
            [iduserpromo] => 
        )

)

0


in this case I am assuming that there is only one record returned from the BD:

$emp_itemx=array(array(
                "idhis" => 'xxx',      
                "id_user_cartao" => 'xxxx',
                "ponto" => 'xxx',
                "id_user" => 'xxx',
                "qtde_pontos" => 'xxx',
                "iduserpromo" => 'xxxx'
            ));


for($i = count($emp_itemx);$i < 50;$i++){
$emp_itemx[$i] = array(
"idhis" => '',      
"id_user_cartao" => '',
"ponto" => '',
"id_user" => '',
"qtde_pontos" => '',
    "iduserpromo" => '');

}

this way it will add as many as it takes to reach the 50 records

  • I’ll test and I’ll let you know!! Thanks

  • Marcos, she is repeating the amount that is missing to fill 50 after each item that comes from the bank and not only end of all!! If I have 20 lines in the bank it puts over 30 after each one, getting 600 lines in the array.

  • is that the question, you must have entered the for inside the while

  • Really, I falter. It’s completing the right one outside the while, but it’s only returning 1 item coming from the bank!!

  • so I told you about replacing fetch() with fetchAll();

  • like: $emp_itemx = $stmtx->fetchAll(); you save a while() and an Extract(); and you won’t even need to iterate that array. json_encode already understands

  • Who knows. Thanks Marcão. I was suffering here, now that I got your reasoning, I didn’t need any of that, I’m not used to PDO, I always use msqli.

  • Here’s how I did it: &#xA;$card_arrx=array();&#xA; $emp_itemx = $stmtx->fetchAll();&#xA;&#xA; for($i = count($emp_itemx);$i < 15;$i++){&#xA; //echo "cont: ".$i;&#xA; $emp_itemx[$i] = array(&#xA; "idhis" => '', &#xA; "id_user_cartao" => '',&#xA; "ponto" => '',&#xA; "id_user" => '',&#xA; "qtde_pontos" => '',&#xA; "iduserpromo" => '');&#xA; }

  • inserts into the question the lines of code that come before, not understanding this line $card_arrx=array();

  • Mark, I just started the array type variable.

  • so it worked?

  • It did work, thanks to two tips. Thank you!!

Show 7 more comments

Browser other questions tagged

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