我在使用Piwik API提供的数组时遇到问题,因为它在PHP格式中输出代码:
array (
0 =>
array (
'idsite' => '2',
'name' => 'coolrob335.com',
'main_url' => 'http://coolrob335.com',
'ts_created' => '2012-06-02 21:05:56',
'ecommerce' => '0',
'timezone' => 'Europe/London',
'currency' => 'GBP',
'excluded_ips' => '',
'excluded_parameters' => '',
'group' => '',
'feedburnerName' => NULL,
),
1 =>
array (
'idsite' => '3',
'name' => 'robbrazier.com',
'main_url' => 'http://robbrazier.com',
'ts_created' => '2012-06-04 14:17:28',
'ecommerce' => '0',
'timezone' => 'Europe/London',
'currency' => 'GBP',
'excluded_ips' => '',
'excluded_parameters' => '',
'group' => '',
'feedburnerName' => NULL,
),
)
我想知道的是:有没有办法直接操作这个数组,因为使用$ obj [0] ['idsite'],$ obj [0] - > idsite不工作(我知道后者不起作用,因为它不是stdClass对象。)
那么,有没有办法转换上面链接中显示的数组
array (
0 =>
array (
'idsite' => '2',
类似
array (
0 =>
array (
['idsite'] => '2',
或将其转换为stdClass对象,以便可以操作它? (我搜索过SO和google将多维数组转换为stdClass对象,在这种情况下没有一个解决方案。可能是因为它们不是[] => ''
旁注: 我想要做的是获取'idsite'和'name',将其转换为带有'name'作为链接标题的列表,例如它会是
$url = "{$user_data['piwik_server']}/index.php?module=API&method=API.get&idSite={$obj->id}&format=PHP&prettyDisplay=1&serialize=0&token_auth={$user_data['apikey']}"
<a href="<?php $url; ?>"><?php $obj->name; ?></a>
(我知道这很可能必须在foreach / while循环中才能有效地工作)
答案 0 :(得分:1)
使用您提供的阵列,以下代码:
function toObject($any) {
$rv = new stdClass();
if (is_array($any)) {
foreach($any as $k => $v) {
$rv->$k = is_array($v)
? toObject($v)
: $v;
}
} else {
$rv = (object) $any;
}
return $rv;
}
$o = toObject($a);
var_dump($o);
将生成此输出:
object(stdClass)#1 (2) {
["0"]=>
object(stdClass)#2 (11) {
["idsite"]=>
string(1) "2"
["name"]=>
string(14) "coolrob335.com"
["main_url"]=>
string(21) "http://coolrob335.com"
["ts_created"]=>
string(19) "2012-06-02 21:05:56"
["ecommerce"]=>
string(1) "0"
["timezone"]=>
string(13) "Europe/London"
["currency"]=>
string(3) "GBP"
["excluded_ips"]=>
string(0) ""
["excluded_parameters"]=>
string(0) ""
["group"]=>
string(0) ""
["feedburnerName"]=>
NULL
}
["1"]=>
object(stdClass)#3 (11) {
["idsite"]=>
string(1) "3"
["name"]=>
string(14) "robbrazier.com"
["main_url"]=>
string(21) "http://robbrazier.com"
["ts_created"]=>
string(19) "2012-06-04 14:17:28"
["ecommerce"]=>
string(1) "0"
["timezone"]=>
string(13) "Europe/London"
["currency"]=>
string(3) "GBP"
["excluded_ips"]=>
string(0) ""
["excluded_parameters"]=>
string(0) ""
["group"]=>
string(0) ""
["feedburnerName"]=>
NULL
}
}
答案 1 :(得分:0)
试试这个
$arr=array (
array (
'idsite' => '2',
'name' => 'coolrob335.com',
'main_url' => 'http://coolrob335.com',
'ts_created' => '2012-06-02 21:05:56',
'ecommerce' => '0',
'timezone' => 'Europe/London',
'currency' => 'GBP',
'excluded_ips' => '',
'excluded_parameters' => '',
'group' => '',
'feedburnerName' => NULL,
),
array (
'idsite' => '3',
'name' => 'robbrazier.com',
'main_url' => 'http://robbrazier.com',
'ts_created' => '2012-06-04 14:17:28',
'ecommerce' => '0',
'timezone' => 'Europe/London',
'currency' => 'GBP',
'excluded_ips' => '',
'excluded_parameters' => '',
'group' => '',
'feedburnerName' => NULL,
),
);
foreach($arr as $k=>$v) $obj[$k]=(object)$v;
echo $obj[0]->idsite; // 2
echo $obj[0]->name; // coolrob335.com
答案 2 :(得分:0)
将PIwik API与PHP数据一起使用时应该做的是在HTTP请求中设置&amp; serialize = 1(而不是你正在执行的serialize = 0),然后运行
$value = unserialize($value);
然后,您将获得准备使用的阵列。