如何从Wordpress插件中的JSON var_dump解析出变量 [英] How to parse out variables from a JSON var_dump in a Wordpress Plugin
问题描述
我试图从我的API中提取JSON数据,并从每个键获取数据,以便在HTML / CSS中显示。现在无法获取数据。
I'm trying to pull in the JSON data from my API, and get the data out of each key to display nicely in HTML/CSS. Right now having trouble getting the data.
这是我的db对象:
[
{
"_id":"54bd5fbb646174009a450001",
"productname":"Product 1",
"overview":"Overview Title",
"benefits":
[
"List item 1",
"List item 2",
"List item 3"
]
}
]
我发现此回答在这里,var_dump工作,它在页面上显示我的db对象。
这是我更新的wordpress php插件:
I found this answer here, and the var_dump is working, it displays my db object on the page. This is my updated wordpress php plugin:
<?php
add_shortcode('mis', function($atts, $data) {
$service_url = 'http://local.web.tt.com:8615/api/users';
// Initiate curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$service_url);
$result=curl_exec($ch);
curl_close($ch);
// Will dump a beauty json :3
// var_dump(json_decode($result, true));
$data = (json_decode($result, true));
var_dump($data);
$data = add_shortcode (
array(
'name' => 'name',
'overview' => 'overview',
'benefits' => 'benefits'
), $data
);
extract($data);
$content .='
<style>li { margin-left: 20px; }</style>
<h2>$name</h2>
<p>$overview</p>
<ul>
<li>$data["benefits"][0]</li>
<li>$data["benefits"][2]</li>
</ul>';
return $content;
});
但我无法获取数据:
>
这是我的问题是:
$data = (json_decode($result, true));
var_dump($data);
$data = add_shortcode (
array(
'name' => 'name',
'overview' => 'overview',
'benefits' => 'benefits'
), $data
);
extract($data);
$content .='
<style>li { margin-left: 20px; }</style>
<h2>$name</h2>
<p>$overview</p>
<ul>
<li>$data["benefits"][0]</li>
<li>$data["benefits"][1]</li>
</ul>';
return $content;
如何将数据从var_dump中获取到正确的键/ vars在$ content变量?
How do I get the data from the var_dump, into the right keys/vars in my $content variable?
推荐答案
这部分代码很奇怪 - 它打算做什么?
This section of code is quite strange - what is it intended to do?
$data = add_shortcode (
array(
'name' => 'name',
'overview' => 'overview',
'benefits' => 'benefits'
), $data
);
extract($data);
要访问您的数据,您只需要
To access your data you can simply do
$data[0]['productname']
你也有你的输出变量在一个字符串(''
)。将数据输入到输出try:
You also have your output variables inside a string (inside ''
). To bring your data in to the output try:
$data = (json_decode($result, true));
var_dump($data);
$product = $data[0];
$content = '<style>li { margin-left: 20px; }</style>';
$content .='<h2>' . $product['productname'] . '</h2>
<p>' . $product['overview'] . '</p>
<ul>
<li>' . $product["benefits"][0] . '</li>
<li>' . $product["benefits"][1] . '</li>
</ul>';
或有一个循环:
$data = (json_decode($result, true));
$content = '<style>li { margin-left: 20px; }</style>';
foreach($data as $product) {
$content .='<h2>' . $product['productname'] . '</h2>
<p>' . $product['overview'] . '</p>
<ul>';
foreach($product['benefits'] as $benefit) {
$content .= '<li>' . $benefit . '</li>';
}
$content .= '</ul>';
}
这篇关于如何从Wordpress插件中的JSON var_dump解析出变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!