0

我必须构建一个数组作为以下从 $_POST 超全局收集值的数组。ca_objets 值将从变量中收集,点后的部分是我需要从 $_POST 收集的值。

 $va_body=array(
        "bundles" => array(
            "ca_objects.description" => array("convertCodesToDisplayText" => true),
            "ca_objects.description" => array("convertCodesToDisplayText" => true),
            "ca_object.representations.caption" => array("convertCodesToDisplayText" =>  true)
        )
    );

这是我使用的代码:

<?php
$table="ca_objects";
$bund=$_POST['find_bundles']; //This is an array with the values I need.

$va_body=array("bundles" => array()); //I declare the array outside the foreach loop. 

我不确定如何编写循环代码以填充 $va_body 数组中的“bundles”数组并获得我需要的结果。任何帮助将不胜感激。

   foreach ($bund as $elem ) {
    // code here
        }

    ?>
4

2 回答 2

1

您可以通过其关联键访问数组,如下所示:

foreach ($bund as $elem ) {
    // code here
    // The empty square brackets tells php to push the $elem value into the next available array key
    $va_body['bundles'][] = $elem;
}
于 2013-06-17T00:18:57.810 回答
0

要获取点之后的值作为新键,您需要拆分字符串。因此,如果您使用形式的 foreach 循环:foreach($array as $key => $value)您在点之后拆分值

   foreach ($bund as $elem => $value) {
        $strings = explode('.', $elem); // Split the strings at the dot
        $ca_objects[$bundles][$strings[count(strings) - 1]] = $value; // Add to array with $elem => $value
    }
于 2013-06-17T00:22:16.590 回答