我尝试使用此函数迭代数组:
public static function getDataForChartAlexaDailyRank($data)
{
$asd = [];
$new = [];
$site = [];
for ($x = 0; $x < sizeof($data->data); $x++) {
foreach ((array)@$data->data->{$x} as $value) {
array_push($site, intval($value->{"Response"}->{"TrafficHistoryResult"}->{"Alexa"}->{"TrafficHistory"}->{"Site"}));
}
foreach ((array)@$data->data->{$x} as $value) {
array_push($new, intval($value->{'Response'}->{'TrafficHistoryResult'}->{'Alexa'}->{'TrafficHistory'}->{'HistoricalData'}->{'Data'}->{'Rank'}));
}
$asd[] = ['name' => $site, 'data' => $new];
$new = [];
}
return json_encode($asd);
}
数组如下所示:
["data"]=>
object(stdClass)#229 (2) {
["mainUrl"]=>
object(stdClass)#235 (1) {
["Response"]=>
object(stdClass)#238 (2) {
["OperationRequest"]=>
object(stdClass)#237 (1) {
["RequestId"]=>
string(36) "1d7824a5-dc09-4efb-9f2a-b35055abc04d"
}
["TrafficHistoryResult"]=>
object(stdClass)#241 (1) {
["Alexa"]=>
object(stdClass)#242 (1) {
["TrafficHistory"]=>
object(stdClass)#243 (4) {
["Range"]=>
string(2) "31"
["Site"]=>
string(25) "https://app.klipfolio.com"
["Start"]=>
string(10) "2016-07-01"
["HistoricalData"]=>
object(stdClass)#244 (1) {
["Data"]=>
array(31) {
[0]=>
object(stdClass)#245 (4) {
["Date"]=>
string(10) "2016-07-01"
["PageViews"]=>
object(stdClass)#246 (2) {
["PerMillion"]=>
string(3) "2.5"
["PerUser"]=>
string(4) "5.50"
}
["Rank"]=>
string(5) "30467"
["Reach"]=>
object(stdClass)#247 (1) {
["PerMillion"]=>
string(2) "30"
}
}
}
}
}
}
}
}
}
["competitorUrl1"]=>
object(stdClass)#338 (1) {
["Response"]=>
object(stdClass)#339 (2) {
["OperationRequest"]=>
object(stdClass)#340 (1) {
["RequestId"]=>
string(36) "ac7ba0b9-c789-5f6a-f1c8-714587b494e9"
}
["TrafficHistoryResult"]=>
object(stdClass)#341 (1) {
["Alexa"]=>
object(stdClass)#342 (1) {
["TrafficHistory"]=>
object(stdClass)#343 (4) {
["Range"]=>
string(2) "31"
["Site"]=>
string(23) "http://onlinesupport.io"
["Start"]=>
string(10) "2016-07-01"
["HistoricalData"]=>
object(stdClass)#344 (0) {
["Data"]=>
array(31) {
[0]=>
object(stdClass)#245 (4) {
["Date"]=>
string(10) "2016-07-01"
["PageViews"]=>
object(stdClass)#246 (2) {
["PerMillion"]=>
string(3) "2.5"
["PerUser"]=>
string(4) "5.50"
}
["Rank"]=>
string(5) "30467"
["Reach"]=>
object(stdClass)#247 (1) {
["PerMillion"]=>
string(2) "30"
}
}
}
}
}
}
}
}
}
}
数组存储在变量$ data(因此访问数组的$ data-data)中,我必须遍历两个对象(["mainUrl"]
和["competitorUrl1"]
才能得到{{1 }和["Site"]
并将它们存储到变量["Rank"]
中。我写的函数尝试这样做。我说尝试因为我不知道如何正确地迭代对象具有不同的名称($asd
和["mainUrl"]
)。我很抱歉,如果我没有充分解释它,但我是php的菜鸟,所以如果您有任何疑问请询问。谢谢非常适合你的时间。
答案 0 :(得分:0)
foreach ((array)@$data->data[$x]->{'Response'}->{'TrafficHistoryResult'}->{'Alexa'}->{'TrafficHistory'}->{'HistoricalData'}->{'Data'} as $value) {
你忘记了->{'mainUrl'}
吗?所以它将是如下:
public static function getDataForChartAlexaDailyRank($data)
{
$asd = [];
$new = [];
for ($x = 0; $x < sizeof($data->data); $x++) {
$site = $data->data[$x]->{'mainUrl'}->{'Response'}->{'TrafficHistoryResult'}->{'Alexa'}->{'TrafficHistory'}->{'Site'};
foreach ($data->data[$x]->{'mainUrl'}->{'Response'}->{'TrafficHistoryResult'}->{'Alexa'}->{'TrafficHistory'}->{'HistoricalData'}->{'Data'} as $value) {
array_push($new, intval($value->{'Rank'}));
}
$asd[] = ['name' => $site, 'data' => $new];
$new = [];
}
return json_encode($asd);
}