简体   繁体   中英

Give name to array in Json/PHP

In AJAX I retrieve in database the last date of a document by category (for example the category invoice, orders, etc.) thanks to this request:

  // In each category there can be several documents
  $stmt = $bdd->prepare('SELECT file_name, max(file_creation) as file_creation, file_category
    FROM all_files
    WHERE ... 
    GROUP BY file_category');
  $stmt ->execute(array(
    ...
  ));
  $arr = $stmt->fetchAll();
  echo json_encode($arr);

So I get it back in JSON:

[
   {
      "File_name": "order_18"
      "0": "order_18"
      "File_creation": "2018-11-28"
      "1": "2018-11-28"
      "File_category": invoice "
      "3": invoice "
   }
   {
      "File_name": "order_18"
      "0": "order_18"
      "File_creation": "2018-11-25"
      "1": "2018-11-25"
      "File_category": "order"
      "3": "order"
   }
]

I then want to place each data in the right places with jquery, like this:

$ ('#label-order').text('') // the text will have to be: 2018-11-25
$ ('#label-invoice').text('') // the text will have to be: 2018-11-28

The problem is that I do not know how to recover the data that interests me to put it in the right places because the number of categories will increase over time

So I thought of doing something like that, to get the data back to data ["invoice"] ["file_creation"] and data ["order"] ["file_creation"] :

[
   "invoice": {
      "File_name": "order_18"
      "0": "order_18"
      "File_creation": "2018-11-28"
      "1": "2018-11-28"
      "File_category": invoice "
      "3": invoice "
   }
   "order": {
      "File_name": "order_18"
      "0": "order_18"
      "File_creation": "2018-11-25"
      "1": "2018-11-25"
      "File_category": "order"
      "3": "order"
   }
]

Is that possible? If so, how can I do? Is there a better solution?

Here is a code to have in a result list of invoices and orders separately. After receiving data on front-end you can use simple code to group all items:

var data = [
   {
      "File_name": "order_18",
      "0": "order_18",
      "File_creation": "2018-11-28",
      "1": "2018-11-28",
      "File_category": "invoice",
      "3": "invoice "
   },
   {
      "File_name": "order_18",
      "0": "order_18",
      "File_creation": "2018-11-25",
      "1": "2018-11-25",
      "File_category": "order",
      "3": "order"
   }
]

var categories = {
    invoices: [],
    orders: []
}

data.map((item) => {
  if(item['File_category'] === 'order') {
    categories.orders.push(item)
  } else if(item['File_category'] === 'invoice') {
    categories.invoices.push(item)
  }
}) 

console.log(categories)

Than you can just loop over specific categories like categories.invoices or categories.orders and easily append it to body.

Doing this using the PHP code:

<?php
//$arrays = $stmt->fetchAll();
$arrays=
[
   [
      "File_name"=>"order_18",
      "File_creation"=>"2018-11-28",
      "File_category"=>"invoice",
   ],
   [
      "File_name"=>"order_18",
      "File_creation"=>"2018-11-25",
      "File_category"=>"order",
   ]
];
foreach($arrays as $index=>$array)
{
    if(isset($array["File_category"]))
    {
        $key=$array["File_category"];
        unset($array["File_category"]);
        $arrays[$key][] = $array;
        unset($arrays[$index]);
    }
}
print_r($arrays);
//echo json_encode($arrays);
?>

The result will be:

Array
(
    [invoice] => Array
        (
            [0] => Array
                (
                    [File_name] => order_18
                    [File_creation] => 2018-11-28
                )
        )
    [order] => Array
        (
            [0] => Array
                (
                    [File_name] => order_18
                    [File_creation] => 2018-11-25
                )
        )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM