我需要创建一个如下所示的数组:
$va_body=array(
"bundles" => array(
"$table.$elem" => array("convertCodesToDisplayText" => true),
"$table.$elem" => array("convertCodesToDisplayText" => true),
)
);
$table
是一个不会改变的字符串,$elem
是从数组中提取出来的。我接近了下面的代码,但它最终只有来自$bund
,的最后一个值$bund
是一个有两个值的数组。我猜在每个循环中都重新声明了数组?
$va_body=array(); // declare the array outside the loop
foreach ($bund as $elem ) {
$va_body['bundles'] = array($table.".".$elem=>array("convertCodesToDisplayText" => true));
}
$bund
数组有两个元素“description”和“type_id”。
$va_body['bundles'][] // Adding [] doesn't work as it modifies the expected outcome.
print_r($va_body)
看起来像这样:
Array (
[bundles] => Array (
[ca_objects.type_id] => Array (
[convertCodesToDisplayText] => 1
)
)
)
我需要它是:
Array (
[bundles] => Array (
[ca_objects.description] => Array (
[convertCodesToDisplayText] => 1
)
[ca_objects.type_id] => Array (
[convertCodesToDisplayText] => 1
)
)
)
提前致谢。
@phpisuber01 使用:
$va_body['bundles'][] = array($table.".".$elem=>array("convertCodesToDisplayText" => true));
print_r($va_body);
看起来像这样:
Array (
[bundles] => Array (
[0] => Array (
[ca_objects.description] => Array (
[convertCodesToDisplayText] => 1
)
)
[1] => Array (
[ca_objects.type_id] => Array (
[convertCodesToDisplayText] => 1
)
)
)
)
我需要它是这样的:
Array (
[bundles] => Array (
[ca_objects.description] => Array (
[convertCodesToDisplayText] => 1
)
[ca_objects.type_id] => Array (
[convertCodesToDisplayText] => 1
)
)
)
@phpisuber01 的回答:
$va_body['bundles'][$table.".".$elem] = array("convertCodesToDisplayText" => true);
非常感谢!