Hello Friend我的问题很简单,但我很难理解。
以下是一些代码: -
$graph1 = new gapi($ga_email,$ga_password);
$dimensions = array('date');
$metrics = array('visits');
$graph1->requestReportData($ga_profile_id,
$dimensions,
$metrics,
'date'
);
$graph2 = new gapi($ga_email,$ga_password);
$dimensions = array('date');
$metrics = array('visits');
$graph2->requestReportData($ga_profile_id,
$dimensions,
$metrics,
'date',
$filter, // Filter the data
'2012-09-07', // Start Date
'2012-10-07', // End Date
1, // Start Index
500 // Max results
);
我有一个数组可以说
$resultsg1 = $graph1->getResults();
和
$resultsg2 = $graph2->getResults();
我想从每个循环代码中获取此值
data.addRows([
<?php
foreach($results as $result)
{
echo '["'.date("l, F j, Y",strtotime($result->getDate())).'", '.$result->getVisits().'," Visits :'.$result->getVisits().'","'.date("l, F j, Y",strtotime($result->getDate())).'",'.$result->getVisits().'," Visits :'.$result->getVisits().'"],';
}
?>
]);
现在我的问题我需要这样的输出: -
foreach($resultsg1 as $result1 and resultsg2 as $results2 )
{
echo '["'.date("l, F j, Y",strtotime($result1->getDate())).'", '.$result1->getVisits().'," Visits :'.$result1->getVisits().'","'.date("l, F j, Y",strtotime($result2->getDate())).'",'.$result2->getVisits().'," Visits :'.$result2->getVisits().'"],';
}
?>
]);
任何帮助都非常感谢.... :)
注意: - 此代码的目的是简单地创建一个包含两个月的图表。
答案 0 :(得分:1)
您可以使用SPL的MultipleIterator
<?php
echo 'phpversion: ', phpversion(), "\n";
$resultsg1 = data(1);
$resultsg2 = data(2);
$iterator = new MultipleIterator();
$iterator->attachIterator(new ArrayIterator($resultsg1));
$iterator->attachIterator(new ArrayIterator($resultsg2));
foreach( $iterator as $e ) {
echo 'r1:', $e[0]->bar(), ', r2:', $e[1]->bar(), "\n";
}
// boilerplate
function data($n) {
return array_map(function($e) use($n) { return new Foo($n.'-'.$e); }, range(1, 9));
}
class Foo {
public function __construct($n) {
$this->n = $n;
}
public function bar() {
return $this->n;
}
}
打印
phpversion: 5.4.7
r1:1-1, r2:2-1
r1:1-2, r2:2-2
r1:1-3, r2:2-3
r1:1-4, r2:2-4
r1:1-5, r2:2-5
r1:1-6, r2:2-6
r1:1-7, r2:2-7
r1:1-8, r2:2-8
r1:1-9, r2:2-9
答案 1 :(得分:0)
foreach($results1 as $k => $result)
{
echo $result->getDate() , $results2[$k]->getDate()
}
如果长度不一样,需要用isset检查($ results 2 [$ k])
答案 2 :(得分:0)
不确定我是否完全理解你的要求,但如果你有多个数组并希望将它们连接在一起然后循环遍历它们,请查看array_merge以及更复杂的结构数组是否合并递归
$results = array_merge($result1, $resultg1);
foreach($结果为$ result) ....