我正在尝试创建一个类,通过该类我应该能够创建具有任意深度的数组/值的数组。但是我找不到解决方法。任何建议/提示/帮助都很好。
这是代码
class CreateArray{
public function __construct() {
}
private $array = [];
public function add_value($value){
$this->array[] = $value;
return $this;
}
public function sub_array_start() {
// What to do here?!
return $this;
}
public function sub_array_end() {
// What to do here?!
return $this;
}
public function get() {
return $this->array;
}
}
$d_array = new CreateArray();
$d_array = $d_array
->add_value([1, 2, 3])
->sub_array_start()
->add_value([3, 2, 8])
->add_value([4, 2, 5])
->sub_array_start()
->add_value([4, 2, 5])
->add_value([3, 2, 8])
->sub_array_end()
->add_value([4, 2, 5])
->sub_array_end()
->add_value([1, 2, 3])
->add_value([1, 2, 3])
->sub_array_start()
->add_value([3, 2, 8])
->add_value([4, 2, 5])
->sub_array_start()
->sub_array_start()
->add_value([3, 2, 8])
->add_value([4, 2, 5])
->sub_array_start()
->add_value([3, 2, 8])
->add_value([4, 2, 5])
->sub_array_end()
->sub_array_end()
->add_value([3, 2, 8])
->add_value([4, 2, 5])
->sub_array_end()
->sub_array_end();
->get();
print_r($d_array);
以上代码应创建这样的数组-
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => Array
(
[0] => 3
[1] => 2
[2] => 8
)
[1] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
[2] => Array
(
[0] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
[1] => Array
(
[0] => 3
[1] => 2
[2] => 8
)
)
[3] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[3] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[4] => Array
(
[0] => Array
(
[0] => 3
[1] => 2
[2] => 8
)
[1] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
[2] => Array
(
[0] => Array
(
[0] => Array
(
[0] => 3
[1] => 2
[2] => 8
)
[1] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
[2] => Array
(
[0] => Array
(
[0] => 3
[1] => 2
[2] => 8
)
[1] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
)
)
[1] => Array
(
[0] => 3
[1] => 2
[2] => 8
)
[2] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
)
)
)
答案 0 :(得分:2)
我认为最简单(但肯定不是有效的)方法是在数组中保留当前键的简单列表。然后,您可以遍历并在正确的位置添加值/子数组(如果=&不熟悉,请查阅PHP手册以获取参考)。在代码中:
class DownloadCriteriaReportWithAwql {
public static function runExample(AdWordsSession $session, $reportFormat){
// Create report query to get the data for last 7 days.
$query = (new ReportQueryBuilder())
->select([
'CampaignId',
'AdGroupId',
'Id',
'Criteria',
'CriteriaType',
'Impressions',
'Clicks',
'Cost',
'Conversions'
])
->from(ReportDefinitionReportType::CRITERIA_PERFORMANCE_REPORT)
->where('Status')->in(['ENABLED'])
->duringDateRange(ReportDefinitionDateRangeType::LAST_7_DAYS)
->build();
// Download report as a string.
$reportDownloader = new ReportDownloader($session);
// Optional: If you need to adjust report settings just for this one
// request, you can create and supply the settings override here.
// Otherwise, default values from the configuration
// file (adsapi_php.ini) are used.
$reportSettingsOverride = (new ReportSettingsBuilder())->includeZeroImpressions(false)->build();
$reportDownloadResult = $reportDownloader->downloadReportWithAwql(
sprintf('%s', $query),
$reportFormat,
$reportSettingsOverride
);
//print "Report was downloaded and printed below:\n";
$datos = $reportDownloadResult->getAsString();
return ($datos);
}
public static function main(){
// Generate a refreshable OAuth2 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
// See: AdWordsSessionBuilder for setting a client customer ID that is
// different from that specified in your adsapi_php.ini file.
// Construct an API session configured from a properties file and the
// OAuth2 credentials above.
$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
$string = self::runExample($session, DownloadFormat::XML);
$xml = new \SimpleXMLElement($string);
return $xml;}}
注意:上一个class CreateArray{
private $array = [];
private $keys = [];
public function __construct() {
}
public function add_value($value){
$tmp =& $this->array;
foreach ($this->keys as $key) {
$tmp =& $tmp[$key];
}
$tmp[] = $value;
return $this;
}
public function sub_array_start() {
$tmp =& $this->array;
foreach ($this->keys as $key) {
$tmp =& $tmp[$key];
}
$tmp[] = [];
end($tmp);
$this->keys[] = key($tmp);
reset($tmp);
return $this;
}
public function sub_array_end() {
array_pop($this->keys);
return $this;
}
public function get() {
return $this->array;
}
}
之后还有一个额外的;
,但是示例代码可以正常工作(除非我在快速比较print_r的过程中忽略了某些内容;)>
答案 1 :(得分:0)
我很无聊,所以我开始了。我的大脑现在很累,但这可能会帮助您或其他人朝正确的方向前进:
app()->runningInConsole()
答案 2 :(得分:0)
我找到了一种方法。虽然我不确定它是否完美。但是这是我自己的解决方案-
class CreateArray{
public function __construct() {
}
private $json_string = '';
public function add_value($value){
$value = json_encode($value, true);
$this->json_string .= $value . ',';
return $this;
}
public function sub_array_start() {
$this->json_string .= '[';
return $this;
}
public function sub_array_end() {
$this->json_string = substr($this->json_string, 0, -1);
$this->json_string .= '],';
return $this;
}
public function get() {
$value = '[' . $this->json_string . ']';
$value = str_replace('],]', ']]', $value);
$value = str_replace('},]', '}]', $value);
$value = json_decode($value);
return $value;
}
}