在php中显示多维数组的值

时间:2015-09-04 14:25:02

标签: php arrays

我想在php中显示多维数组以显示html中的所有数组值我想显示这些值:coupon_link,coupon_description,store,expire_date.Please建议我如何以表格格式显示数组示例如下

    Array
(
    [status_code] => 200
    [status_text] => Success
    [count] => 183
    [data] => Array
        (
            [0] => Array
                (
                    [coupon_urlh] => 02940b49613d6588f538d6c22feaf12a142d63ab
                    [coupon_title] => 
                    [coupon_code] => 
                    [coupon_description] => Get the Motorola Nexus 6 starting at Rs. 34,999. No coupon code required.
                    [published_date] => 
                    [expiry_date] => 
                    [last_tested] =>    Verified Today
                    [success_rate] => 
                    [crawl_time] => 20150630110739
                    [coupon_link] => http://dl.flipkart.com/dl/mobiles/pr?p[0]=facets.brand[]=Motorola&p[1]=facets.availability[]=Exclude Out of Stock&sid=tyy,4io&filterNone=true&q=nexus 6&affid=contact&affExtParam1=p:rQd1Du50OXFe
                    [store] => Flipkart
                )

            [1] => Array
                (
                    [coupon_urlh] => 0753512477c9d48369590db9670d16063b9e9b7b
                    [coupon_title] => 
                    [coupon_code] => 
                    [coupon_description] => Get minimum 50% off on books displayed on the landing page. No coupon code required.
                    [published_date] => 
                    [expiry_date] => 
                    [last_tested] =>    Verified Today
                    [success_rate] => 
                    [crawl_time] => 20150630110822
                    [coupon_link] => http://dl.flipkart.com/dl/books/~selected-general-books/pr?p[0]=facets.discount_range[]=More than 50%&sid=bks&filterNone=true&affid=contact&affExtParam1=p:cCPZiRc86rkW
                    [store] => Flipkart
                )

            [2] => Array
                (
                    [coupon_urlh] => 0aff177f5e76bf20ab00c55fd4f8a8f30332de08
                    [coupon_title] => 
                    [coupon_code] => 
                    [coupon_description] => Get minimum 50% off on sunglasses.
                    [published_date] => 
                    [expiry_date] => 
                    [last_tested] =>    Verified Today
                    [success_rate] => 
                    [crawl_time] => 20150630110831
                    [coupon_link] => http://dl.flipkart.com/dl/sunglasses/pr?p[0]=facets.discount_range[]=More than 50%&sid=26x&affid=contact&affExtParam1=p:y3x7w6O3TkL0
                    [store] => Flipkart
                )

            [3] => Array
                (
                    [coupon_urlh] => 16249e29e9b70776e9713d8a88c6854d9e315080
                    [coupon_title] => 
                    [coupon_code] => 
                    [coupon_description] => Get minimum 50% off on car accessories. No coupon code required.
                    [published_date] => 
                    [expiry_date] => 
                    [last_tested] =>    Verified Today
                    [success_rate] => 
                    [crawl_time] => 20150630110806
                    [coupon_link] => http://dl.flipkart.com/dl/offers/car-accessories?affid=contact&affExtParam1=p:FLlJjJezfFPq
                    [store] => Flipkart
                )

            [4] => Array
                (
                    [coupon_urlh] => 17bc8b23813a0cdaa07c2da63412831a3c7fc615
                    [coupon_title] => 
                    [coupon_code] => 
                    [coupon_description] => Deals of The Day: Avail exciting offers everyday on various categories. No coupon code required.
                    [published_date] => 
                    [expiry_date] => 
                    [last_tested] =>    Verified Today
                    [success_rate] => 
                    [crawl_time] => 20150630110729
                    [coupon_link] => http://dl.flipkart.com/dl/offers?affid=contact&affExtParam1=p:bewu0vDGfKyd
                    [store] => Flipkart
                )

        )

)

4 个答案:

答案 0 :(得分:0)

您可以在php中使用foreach loop

foreach($your_array['data'] as $element){
    echo $element['coupon_description']."<br />";
    echo $element['store']."<br />";
    .....
}

答案 1 :(得分:0)

您可以通过执行以下操作来访问搜索到的值:

// retrieve the coupon description for the first element of your array
echo $array['data'][0]['coupon_description'];

答案 2 :(得分:0)

如果在PHP(http://php.net/manual/en/function.print-r.php)中使用函数print_r,则可以返回变量的可读格式。此功能将准确显示您在问题中显示的内容。唯一的问题是HTML不会解析标签,新行和空格。但是如果您使用HTML标记<pre>,它就会这样做。

所以我实际上一直使用这段代码来调试变量。我实际上在PHPStorm中为它做了一个简短的调整,以便更快地进行调试:

echo '<pre>';
print_r($data);
echo '</pre>';
die("Debug in file " . __FILE__ . ':' . __LINE__);

所以如果$ data是array('hey', 'there', 'cool', 'kid', array('how', 'are', 'you')),那么这将输出:

Array
(
    [0] => hey
    [1] => there
    [2] => cool
    [3] => kid
    [4] => Array
        (
            [0] => how
            [1] => are
            [2] => you
        )

)

Debug in file /var/www/vhosts/test.local/test.php:10

这不仅适用于数组。您可以在基本上所有变量上使用它。在解析来自API或其他返回非常长响应的系统的响应时,它非常有用。

答案 3 :(得分:0)

以下是多维数组的示例:

$cars = array
  (
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15)
  );

如果没有 foreach 循环,您可以像这样回显多维数组:

for ($row = 0; $row < 4; $row++) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 3; $col++) {
    echo "<li>".$cars[$row][$col]."</li>";
  }
  echo "</ul>";
}