2

I need to create an array like the following:

$va_body=array(
    "bundles" => array(
        "$table.$elem" => array("convertCodesToDisplayText" => true),
        "$table.$elem" => array("convertCodesToDisplayText" => true),
    )
);

$table is a string that does not change and $elem is extracted from an array. I got close with the following code, but it ends up with only the last value from $bund, $bund is an array with two values. I guess the array is redeclared in each loop?

$va_body=array(); // declare the array outside the loop
foreach ($bund as $elem ) {
    $va_body['bundles'] = array($table.".".$elem=>array("convertCodesToDisplayText" => true));
}

$bund array has two elements "description" and "type_id".

$va_body['bundles'][] // Adding [] doesn't work as it modifies the expected outcome.

print_r($va_body) looks like this:

Array (
    [bundles] => Array (
        [ca_objects.type_id] => Array (
            [convertCodesToDisplayText] => 1
            )
        )
    ) 

I need it to be:

Array (
    [bundles] => Array (
        [ca_objects.description] => Array (
            [convertCodesToDisplayText] => 1
        )
        [ca_objects.type_id] => Array (
            [convertCodesToDisplayText] => 1
        )
    )
)

Thanks in advance.

@phpisuber01 Using:

$va_body['bundles'][] = array($table.".".$elem=>array("convertCodesToDisplayText" => true));

print_r($va_body); looks like this:

 Array (
    [bundles] => Array (
        [0] => Array (
            [ca_objects.description] => Array (
                [convertCodesToDisplayText] => 1
            )
        )
        [1] => Array (
            [ca_objects.type_id] => Array (
                [convertCodesToDisplayText] => 1
            )
        )
    )
) 

And I need it to be like this:

Array (
    [bundles] => Array (
        [ca_objects.description] => Array (
            [convertCodesToDisplayText] => 1
        )
        [ca_objects.type_id] => Array (
            [convertCodesToDisplayText] => 1
        )
    )
)

Answer by @phpisuber01:

$va_body['bundles'][$table.".".$elem] = array("convertCodesToDisplayText" => true);

Thank you very much!

2 Answers 2

1

You need to make an array of arrays. In your loop change the following line:

$va_body['bundles'][$table.".".$elem] = array("convertCodesToDisplayText" => true);

Added [] after $va_body['bundles'].

All this does is keep adding the new bundles into the array. Your original code is overwriting bundles each iteration. That's why you only get the last one.

Updated to get closer to OP's exact needs.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer! I already tried that but It add extra keys to the array I need to get. I modified my question to show yo what happens.
well... let me tell you that not only it did work. I learned greatly about arrays and finally... I would hug you if you were here!!!! THANKS!!!!!!!!!!!!!!!!!!!
0
$va_body = array();
$va_body['bundles'] = array();

foreach ($bund AS $elem)
{
    $va_body['bundles']["{$table}.{$elem}"] = array("convertCodesToDisplayText" => true);
}

2 Comments

Thanks for your answer!. It happens to be right by the way but I already accepted @phpisuber01 answer.
No problem, I realized that after posting. Anyway, it's a clean code, so it will probably be useful for someone else :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.