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

时间:2018-11-23 11:06:38

标签: php laravel

我想通过PHP打印客户数据(在表中)。仅我遇到一个问题,它必须来自两个不同的Web商店(双阵列)。第一个网上商店有一个客户,其他两个。

此代码是通过woocommerce API通过连接检索的。 首先,我连接到获取所有“ Woocommerce商店”的数据库

$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 ▶}
    }
  ]
]

谢谢:)

2 个答案:

答案 0 :(得分:1)

一种快速而懒惰的方法是将其以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默认情况下始终处于启用状态,无法禁用)

答案 1 :(得分:1)

好像您需要另一个内部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()更有效,因为这消除了调用函数的开销。