我需要创建一个如下所示的数组:
$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”和"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);非常感谢!
发布于 2013-06-18 03:00:22
你需要创建一个数组的数组。在您的循环中,更改以下行:
$va_body['bundles'][$table.".".$elem] = array("convertCodesToDisplayText" => true);在$va_body['bundles']之后添加了[]。
所有这些操作都是不断地向数组中添加新的包。您的原始代码会覆盖每次迭代的包。这就是为什么你只能得到最后一个。
更新以更贴近OP的确切需求。
发布于 2013-06-18 03:24:00
$va_body = array();
$va_body['bundles'] = array();
foreach ($bund AS $elem)
{
$va_body['bundles']["{$table}.{$elem}"] = array("convertCodesToDisplayText" => true);
}https://stackoverflow.com/questions/17154761
复制相似问题