Object for Array php

Asked

Viewed 65 times

0

Hello, I have a problem, this code below is returning a 'type' object, I need 'type' to be array.

while ($obj = $stmt->fetch(PDO::FETCH_ASSOC)) {
      $stmt2 = $db->query('SELECT * FROM wines a, uva b WHERE a.id_tipo = '.$obj['idtipo'].' AND a.id_uva = b.id GROUP BY a.id_uva');
       while ($obj2 = $stmt2->fetch(PDO::FETCH_ASSOC)) {
             $wines[$obj['tipo']][] = $obj2;
       }

}

He returns like this

{"ESPUMANTES": [
    {
        "id": "5",
        "name": "MIOLO MILESSIME",
        "uva": "BRUT"
    },

],
"VINHOS BRANCOS": [
    {
        "id": "1",
        "name": "CASA VALDUGA LEOPOLDINA GRAN RESERVA",
        "uva": "CHARDONNAY"
    },

]}

I need "SPARKLING" and "WHITE WINES" to be inside an array.

{
[
   1: "ESPUMANTES" [
        {
            "id": "1",
            "name": "CASA VALDUGA LEOPOLDINA GRAN RESERVA",
            "uva": "CHARDONNAY"
        }
    ] 
],
[
    2: "VINHOS BRANCOS" [
        {
            "id": "1",
            "name": "CASA VALDUGA LEOPOLDINA GRAN RESERVA",
            "uva": "CHARDONNAY"
        }
]

}

  • If I understand correctly. You want it to stay that way: array("ESPUMANTES", "VINHOS BRANCOS"). That’s it?

  • basically that, but within each other array with the information

  • For each wine can it have more than one category? That’s it?

  • Example... MIOLO MILESSIME can be "SPARKLING" and "WHITE WINES". That’s it?

  • You could show the desired end result?

  • something like this, why I need to iterate it at the angle with ngFor the types of wines that are SPARKLING WINES or WHITE WINES, in a list that opens a modal with the wines themselves.

  • I get it. I’ll post an answer and you’ll see if it works.

Show 2 more comments

3 answers

1


Try it like this:

$x = 0;
while ($obj = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $stmt2 = $db->query('SELECT * FROM wines a, uva b WHERE a.id_tipo = '.$obj['idtipo'].' AND a.id_uva = b.id GROUP BY a.id_uva');
    while ($obj2 = $stmt2->fetch(PDO::FETCH_ASSOC)) {
        $wines[$x][$obj['tipo']] = $obj2;
    }
    $x++;
}
  • 1

    That’s right, my darling! hahaha, thank you so much for helping me, and helping the community that will have this doubt in the future, I’ve been wrapped up in this project for a while because I’m not good with php worth it!! <3

  • XD quiet! Sometimes it is difficult to understand the problem so we take a little time. But that’s it. Good Luck!

0

0

Patrick, if you don’t have to be in PHP the transformation to array, you can do it as follows.

Where I set the value to x, it would be in the case of your server’s sponse.

let x = {
"ESPUMANTES": [
    {
        "id": "5",
        "name": "MIOLO MILESSIME",
        "uva": "BRUT"
    }
],
"VINHOS BRANCOS": [
    {
        "id": "1",
        "name": "CASA VALDUGA LEOPOLDINA GRAN RESERVA",
        "uva": "CHARDONNAY"
    }
],
"VINHOS TINTOS": [
    {
        "id": "4",
        "name": "ERRAZURIZ 1870 RESERVA CARMENERE ",
        "uva": "CARMENERE"
    }
]
};

let z = Object.values(x);
z.forEach(res => { console.log(res) }); 

Browser other questions tagged

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