我正在尝试将一些JSON我从API响应中添加到电子邮件中,并希望将JSON输出到HTML表中。问题是JSON响应看起来不像我之前使用过的任何东西。
$vs_getClickPath = wp_remote_get('url');
$clickPath_body = wp_remote_retrieve_body( $vs_getClickPath );
$notification['message'] .= $clickPath_body;
这给了我以下内容:
[
["date\/time", "page name visited", "city", "region", "country", "company name\/isp", "identity", "search\/referral"],
["2016-05-17 10:56:05", "Home Page", "Buffalo", "New York", "United States", "I-evolve Technology Services", "", ""]
]
所有键首先出现在一个数组中,然后出现在另一个数组中。尝试使用索引只是获取单个字符,foreach不起作用。我已尝试json_decode
,但它不会输出print_r
或var_dump
的任何内容。
无论如何,我能够遍历这个并将数据输出到HTML表格中吗?第一个数组是表头,之后的每个数组都是表行。
答案 0 :(得分:3)
我使用json_decode($str, true)
并返回了一个可以使用的数组:
$str = '[["date\/time","page name visited","city","region","country","company name\/isp","identity","search\/referral"],["2016-05-17 10:56:05","Home Page","Buffalo","New York","United States","I-evolve Technology Services","",""]]';
$json = json_decode($str, true);
echo '<pre>';
print_r($json);
返回
Array
(
[0] => Array
(
[0] => date/time
[1] => page name visited
[2] => city
[3] => region
[4] => country
[5] => company name/isp
[6] => identity
[7] => search/referral
)
[1] => Array
(
[0] => 2016-05-17 10:56:05
[1] => Home Page
[2] => Buffalo
[3] => New York
[4] => United States
[5] => I-evolve Technology Services
[6] =>
[7] =>
)
)
在true
的调用中设置json_decode()
会使函数返回一个数组。如果你不想要一个数组,你可以通过将该选项退出呼叫来返回一个对象。
答案 1 :(得分:0)
从输出中,结果是一个数组数组
[
[
"date\/time",
"page name visited",
"city",
"region",
"country",
"company name\/isp",
"identity",
"search\/referral"
],
[
"2016-05-17 10:56:05",
"Home Page",
"Buffalo",
"New York",
"United States",
"I-evolve Technology Services",
"",
""
]
]
你可以在foreach中找到foreach的值。
foreach ($clickPath_body as $array) {
foreach ($array as $string) {
echo $string;
}
echo "<br>";
}