What is the correct way to increment multiple inputs of a php array

Asked

Viewed 39 times

1

Good morning friends , in my studies with arrays I find myself in a dilemma , I have a answer that returns me several products and in these products we have the category and other information in JSON :

   Products: [
    {
    SKU: "xxxxxxx",
    SKUMain: "xxxxxxx",
    IsFreeRange: false,
    IsVirtual: false,
    IsDownloadable: false,
    ParentRechargeProduct: "",
    PriceUN: "23,00",
    RateADM: "0,00",
    Freight: "15,12",
    Handling: "1,68",
    PriceTotal: "39,80",
    Name: " xxxxx",
    Description: "descrição",
    Enable: true
    CategoryId: 13,
    Category: "ENTRETENIMENTO",
    },
    {
    SKU: "xxxxx",
    SKUMain: "xxxxxx",
    IsFreeRange: false,
    IsVirtual: false,
    IsDownloadable: false,
    ParentRechargeProduct: "",
    PriceUN: "17,00",
    RateADM: "0,00",
    Freight: "15,12",
    Handling: "1,68",
    PriceTotal: "33,80",
    Name: " CINÉPOLIS PADRÃO",
    Description: "descrição ",
    Enable: true,
    CategoryId: 13,
    Category: "ENTRETENIMENTO",
    },
    {
    SKU: "xxxxxxxxx",
    SKUMain: "xxxx",
    IsFreeRange: false,
    IsVirtual: false,
    IsDownloadable: false,
    ParentRechargeProduct: "",
    PriceUN: "100,00",
    RateADM: "8,00",
    Freight: "15,12",
    Handling: "1,68",
    PriceTotal: "124,80",
    Name: "xxxxxxx",
    Description: "descrição ",
    Enable: true,
    CategoryId: 8,
    Category: "RESTAURANTES",
    }]

well, I developed a code that captures the category and checks if the index already exists in the array and if it exists increases it, making that at the end of the processing the code returns me each category and its quantity inside the array products.

the code last used in PHP was this:

 $lista = json_decode($listaDeProdutos,true);
    $categorias = array('categoria'=>array());
    foreach($lista['Products'] as $pod){
        if(array_key_exists($pod['Category'], $categorias['categoria'])){
            $categorias['categoria'][$pod['Category']]['qtd']++;

        }else{
            $categorias['categoria'][$pod['Category']]['qtd'] = 1; 
        }

    }

   echo json_encode($categorias);

and my answer was this :

{
categoria: {
ENTRETENIMENTO: {
qtd: 10
},
RESTAURANTES: {
qtd: 11
},
DEPARTAMENTO: {
qtd: 69
},
SAÚDE E BELEZA: {
qtd: 20
},
MODA E ACESSÓRIOS: {
qtd: 33
},
HIPERMERCADOS: {
qtd: 94
},
COMBUSTÍVEL: {
qtd: 9
},
ARTIGOS ESPORTIVOS: {
qtd: 17
},
VINHOS: {
qtd: 14
},
VIAGENS: {
qtd: 10
},
GAMES: {
qtd: 5
},
BRINQUEDOS: {
qtd: 5
},
CASA E DECORAÇÃO: {
qtd: 5
},
CULTURA: {
qtd: 3
},
RECARGA: {
qtd: 25
},
PRÉ-PAGOS: {
qtd: 1
},
}
}

my doubt is whether what I did is good practice or is there something more robust in a similar problem.

  • I saw no problem...

1 answer

0

Dude, it depends on what you need. If you need to use it, you need to create an array that way. You have to think about how it will be used at the end and if this out you will be able to interact.

Browser other questions tagged

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