将数组放入表格和网格形式 - PHP

时间:2017-12-27 09:50:27

标签: php html arrays

我有一系列商品代码,商品名称和价格

$arr = array(
    "101: orange juice - 5.2",
    "504: hot roll - 6.2",
    "106: lime juice - 3",
    "210: mineral water (s) - 2.6",
    "107: PEACH JUICE - 6.99",
    "211: mineral water (m) - 3.7",
    "500: hot dog (r) - 3.7",
    "212: mineral water (l) - 4.9",
    "509: popcorn - 4.2",
    "501: HOT DOG (l) - 5.1",
    "502: apple pie - 12.69",
    "100: apple juice - 4.8",
    "506: pine tart - 0.75",
    "108: coca COLA - 3.25",
);

如何从原始表单对数组进行排序,将其设置为小写,放入表格并添加" RM"在价格面前有2个花车落后,比如

Image A

同时将其放入网格布局格式,如Image B

尝试使用PHP数组来熟悉自己,但是如果我使用Code,Name,Price指定的数组重新声明数组,我只能这样做

$arr1 = array(
array(Item_Code=>101, Item_Name=>"orange juice",Price=>5.2),
...
)

1 个答案:

答案 0 :(得分:1)

试试这个,

<?php
$arr = array(
    "101: orange juice - 5.2",
    "504: hot roll - 6.2",
    "106: lime juice - 3",
    "210: mineral water (s) - 2.6",
    "107: PEACH JUICE - 6.99",
    "211: mineral water (m) - 3.7",
    "500: hot dog (r) - 3.7",
    "212: mineral water (l) - 4.9",
    "509: popcorn - 4.2",
    "501: HOT DOG (l) - 5.1",
    "502: apple pie - 12.69",
    "100: apple juice - 4.8",
    "506: pine tart - 0.75",
    "108: coca COLA - 3.25",
);



$formattedAry = array();
foreach($arr as $a){
  $newArray = array();
  list($code, $namePrice) = explode(":", $a);
  list($name, $price) = explode("-", $namePrice);
  $newArray['Item_Code'] = trim($code);
  $newArray['Item_Name'] = trim($name);
  $newArray['Price'] = trim($price);
  $formattedAry[] = $newArray;
}
echo '<pre>';print_r($formattedAry);
?>