将PHP数组卸载到html表中的最佳方法是什么



我想通过PHP打印自定义数据(在表中(。只是我遇到了一个问题,它必须来自两个不同的web商店(一个双数组(。第一家网店有一位顾客,另外两位顾客。

此代码由连接通过woocomerce API检索。首先,我连接到数据库,获取所有"Wooccommerce Stores">

$webshops = DB::table('webshops')->get();
$costumerdata = array();

然后我循环遍历数据以获得所有数据

foreach ($webshops as $webshop => $value) {
$wc_api = new WC_API_Client($value->WEBSHOP_CONSUMER_KEY, $value->WEBSHOP_CONSUMER_SECRET, $value->WEBSHOP_URL);
// array_push($costumerlist, $wc_api->get_customers());
foreach ($wc_api->get_customers() as $costumer) {  
array_push($costumerdata, $costumer);
}
}

当我在数据上抛出DD时,

dd($costumerdata);

下一个数据将从脚本中返回。现在我想把这个数组放在HTML/PHP列表中。我如何在一张表中列出所有客户的完整列表(我认为我没有正确地循环(。

array:2 [▼
0 => array:1 [▼
0 => {#252 ▼
+"id": 2
+"created_at": "2018-11-22T14:29:01Z"
+"email": "TheScruffieSpadexMuffin@gmail.cop"
+"first_name": "Bernardo"
+"last_name": "Mcscrufflemuffle"
+"username": "thescruffiespadexmuffin"
+"role": "customer"
+"last_order_id": 21
+"last_order_date": "2018-11-22T14:29:01Z"
+"orders_count": 1
+"total_spent": "0.00"
+"avatar_url": "https://secure.gravatar.com/avatar/57918972c32605a2acc4d15771f09f69?s=96&d=mm&r=g"
+"billing_address": {#253 ▶}
+"shipping_address": {#254 ▶}
}
]
1 => array:2 [▼
0 => {#245 ▼
+"id": 3
+"created_at": "2018-11-22T17:48:21Z"
+"email": "info@xxx-it.nl"
+"first_name": "Bladieblabla"
+"last_name": "Vla"
+"username": "info"
+"role": "customer"
+"last_order_id": 28
+"last_order_date": "2018-11-23T10:08:06Z"
+"orders_count": 2
+"total_spent": "0.00"
+"avatar_url": "https://secure.gravatar.com/avatar/ba4da010e0ff01978cdf0d3e176dfcea?s=96&d=mm&r=g"
+"billing_address": {#256 ▶}
+"shipping_address": {#257 ▶}
}
1 => {#258 ▼
+"id": 4
+"created_at": "2018-11-23T10:10:48Z"
+"email": "asdaasd@xxx-it.nl"
+"first_name": "Lyla"
+"last_name": "Futurama"
+"username": "asdaasd"
+"role": "customer"
+"last_order_id": 29
+"last_order_date": "2018-11-23T10:10:49Z"
+"orders_count": 1
+"total_spent": "0.00"
+"avatar_url": "https://secure.gravatar.com/avatar/a4626c2ac8236e09229e3599d0302c84?s=96&d=mm&r=g"
+"billing_address": {#259 ▶}
+"shipping_address": {#260 ▶}
}
]
]

谢谢:(

看起来您需要另一个内部foreach来循环$customer。看起来$wc_api->get_customers()返回了一系列客户

foreach ($webshops as $webshop => $value) {
$wc_api = new WC_API_Client($value->WEBSHOP_CONSUMER_KEY, $value->WEBSHOP_CONSUMER_SECRET, $value->WEBSHOP_URL);
foreach ($wc_api->get_customers() as $costumers) {  
foreach ($customers as $customer) {
$costumerdata[] = $customer;
}
}
}

此外,如果一次只向数组中添加一个项,则执行$costumerdata[] = $costumer;比使用array_push()更有效,因为这样可以消除调用函数的开销。

快速而懒惰的方法是将其丢弃,格式为printf或JSON_PRETTY_PRINT,并记住对输出进行html编码,例如

echo '<pre>';
echo htmlentities(print_r($costumerdata,true), ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE | ENT_DISALLOWED, 'UTF-8', true);
echo '</pre>';

echo '<pre>';
echo htmlentities(json_encode($costumerdata,JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_LINE_TERMINATORS), ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE | ENT_DISALLOWED, 'UTF-8', true);
echo '</pre>';

如果laravel内置了一些花哨的东西来做同样的事情,我不会感到惊讶,但我不知道。(我一般不了解laravel,但我了解PHP。(

ps,JSON_UNESCAPED_LINE_TERMINATORS是在PHP7.1中首次引入的,如果您的代码需要与旧版本的PHP兼容,则应该添加一个

if(!defined("JSON_UNESCAPED_LINE_TERMINATORS")){
define("JSON_UNESCAPED_LINE_TERMINATORS",0);
}

(在7.1之前,JSON_UNESCAPED_LINE_TERMINATORS默认情况下始终处于打开状态,无法禁用(

相关内容

最新更新