从 $_GET 数据构建数组
Building an array from $_GET Data
我正在使用 facebook api 制作一个 flexible_spec 定位数组。我在数组中有三个选项。第一个是必需的,其他的是可选的。
所以我试图根据变量是否通过 $_GET 传递来构建一个数组。
最终数组应如下所示:
["flexible_spec"]=>
array(3) {
[0]=>
array(1) {
["interests"]=>
array(1) {
[0]=>
string(13) "6003220643158"
}
}
[1]=>
array(1) {
["interests"]=>
array(1) {
[0]=>
string(13) "6002866944422"
}
}
[2]=>
array(1) {
["exclusions"]=>
array(1) {
[0]=>
string(0) ""
}
}
第一个兴趣数组将始终出现,但如果 $_GET 响应不是空数组,则应构建第二个和第三个兴趣数组(我敢肯定,我在这里可能会把事情复杂化!)
所以我的代码看起来像这样但是 returns 错误,因为它标识数组未关闭(在不正确的地方缺少分号和逗号)
$flex_array = array(
array(
'interests' => $interests
),
if (!empty($mustinterests)) {
array(
'interests' => $mustinterests
),
}
if (!empty($excludeinterests)) {
array(
'exclusions' => $excludeinterests
)
}
);
然后在 $reach_estimate:
中调用这个数组
$targeting_spec = array(
'geo_locations' => array(
'countries' => ['GB'],
),
'page_types' => $pieces,
'flexible_spec' => $flex_array
);
$reach_estimate = $account->getReachEstimate(
array(),
array(
'currency' => 'GBP',
'optimize_for' => $OptimizationGoal,
'targeting_spec' => $targeting_spec,
));
我想不出比这更好的方法来构建阵列了吗?
不能在数组定义中使用 if 语句。这是语法错误。您可以使用单独的语句来构建您想要的数组。
$flex_array = array(array('interests' => $interests));
if (!empty($mustinterests)) {
$flex_array[] = array('interests' => $mustinterests);
}
if (!empty($excludeinterests)) {
$flex_array[] = array('exclusions' => $excludeinterests);
}
确保不要在数组中放置 if 语句!
我正在使用 facebook api 制作一个 flexible_spec 定位数组。我在数组中有三个选项。第一个是必需的,其他的是可选的。
所以我试图根据变量是否通过 $_GET 传递来构建一个数组。
最终数组应如下所示:
["flexible_spec"]=>
array(3) {
[0]=>
array(1) {
["interests"]=>
array(1) {
[0]=>
string(13) "6003220643158"
}
}
[1]=>
array(1) {
["interests"]=>
array(1) {
[0]=>
string(13) "6002866944422"
}
}
[2]=>
array(1) {
["exclusions"]=>
array(1) {
[0]=>
string(0) ""
}
}
第一个兴趣数组将始终出现,但如果 $_GET 响应不是空数组,则应构建第二个和第三个兴趣数组(我敢肯定,我在这里可能会把事情复杂化!)
所以我的代码看起来像这样但是 returns 错误,因为它标识数组未关闭(在不正确的地方缺少分号和逗号)
$flex_array = array(
array(
'interests' => $interests
),
if (!empty($mustinterests)) {
array(
'interests' => $mustinterests
),
}
if (!empty($excludeinterests)) {
array(
'exclusions' => $excludeinterests
)
}
);
然后在 $reach_estimate:
中调用这个数组$targeting_spec = array(
'geo_locations' => array(
'countries' => ['GB'],
),
'page_types' => $pieces,
'flexible_spec' => $flex_array
);
$reach_estimate = $account->getReachEstimate(
array(),
array(
'currency' => 'GBP',
'optimize_for' => $OptimizationGoal,
'targeting_spec' => $targeting_spec,
));
我想不出比这更好的方法来构建阵列了吗?
不能在数组定义中使用 if 语句。这是语法错误。您可以使用单独的语句来构建您想要的数组。
$flex_array = array(array('interests' => $interests));
if (!empty($mustinterests)) {
$flex_array[] = array('interests' => $mustinterests);
}
if (!empty($excludeinterests)) {
$flex_array[] = array('exclusions' => $excludeinterests);
}
确保不要在数组中放置 if 语句!