I have to build an array as the following one gathering values from $_POST superglobal. ca_objets value would be gather from a variable and the part after the dot is what I need to gather from $_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)
)
);
This is the code I use:
<?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.
I'm not sure how to code the loop in order to populate the "bundles" array inside $va_body array and get the result I need. Any help will be greatly appreciated.
foreach ($bund as $elem ) {
// code here
}
?>
You can just access the array by its associative key, like this:
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;
}
To get the value after the dots as the new key, you need to split the string. So if you use a foreach loop of the form: foreach($array as $key => $value)
you split the values after the dots
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
}