Question

I'm having a terrible time trying to massage an array of data into the format a third-party service requires. I can't change the way the data comes into the function so I need to manipulate it then send it off. I've managed to hack it into a state where its almost correct with the exception that it only takes the first value of the arrays and ignores the rest. I think this is because I'm overwriting the values but I don't know how to solve it.

I'm pretty new to PHP and think I'm missing some core array methods or something here so any input would be greatly appreciated

Data going into the function ($data)

Array (
    [checkbox_customfield_11363] => Array
        (
            [random unknown string] => Apple
            [random unknown string] => Orange
            [random unknown string] => Banana
        )

    [checkbox_customfield_11339] => Array
        (
            [random unknown string] => Peas
            [random unknown string] => Potatos

        )
)

The function that should be massaging the data into the required format. The first foreach loop just removes the first part of the name of each array, It works as intended - might not be the cleanest though. Its the second foreach and below that I'm struggling with.

public function createCheckBoxField($data)
{
    $checkbox_matches = array();
    foreach ($data as $key => $value) {
        if (preg_match("/checkbox/", $key)) {
            $newKey = explode('_', $key, 2);
            $checkbox_matches[$newKey[1]] = $value;
        }
    }

    $checkboxes = array();
    foreach ($checkbox_matches as $key1 => $value1) {

        foreach ($value1 as $test) {
            $checkboxes += [$key1 => [["value" => $test]]];
        }
    }
    return $checkboxes;
}

Code currently returns

Array
(
    [customfield_11363] => Array
        (
            [0] => Array
                (
                    [value] => Apple
                )

        )

    [customfield_11339] => Array
        (
            [0] => Array
                (
                    [value] => Peas
                )

        )

)

I need it to be returning

Array
(
    [customfield_11363] => Array
        (
            [0] => Array
                (
                    [value] => Apple
                )
            [1] => Array
                (
                    [value] => Orange
                )
            [2] => Array
                (
                    [value] => Banana
                )

        )

    [customfield_11339] => Array
        (
            [0] => Array
                (
                    [value] => Peas
                )
            [1] => Array
                (
                    [value] => Potato
                )

        )

)
Was it helpful?

Solution

Not sure about the preg_match etc (because you haven't shown what it is meant to do), but given your input and desired output this should work:

foreach($data as $key => $array) {
    foreach($array as $value) {
        $result[$key][] = array('value' => $value);
    }
}

OTHER TIPS

The problem why you see only the first element is because of the array union operator you're using in this line:

$checkboxes += [$key1 => [["value" => $test]]];

Because $checkboxes contains the key $key1 already in every iteration but the first one, it's not overwritten. With array union, the key on the left wins.

Instead push into the array:

$checkboxes[$key1][] = ["value" => $test];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top