0

Hope this post is not a duplicate, I've looking for a solution but I'm really stuck, maybe somebody pass trough this and has a solution.

I have a set of array that pass into a graph, the desire graph need to show all the data even if in the desired day doesn't exist value for one sub array data.

This is my array:

$setData = array(
            '2017-06-21' => array(
                'gender1' => array(
                    'value1' => 100,
                    'value2' => 100,
                    'value3' => 100,
                ),
                'gender2' => array(
                    'value1' => 200,
                    'value2' => 200,
                    'value3' => 200,
                ),
                'gender3' => array(
                    'value1' => 300,
                    'value2' => 300,
                    'value3' => 300,
                )
            ),
            '2017-06-22' => array(
                'gender1' => array(
                    'value1' => 90,
                    'value2' => 90,
                    'value3' => 90,
                ),
                'gender3' => array(
                    'value1' => 200,
                    'value2' => 200,
                    'value3' => 200,
                )
            ),
            '2017-06-23' => array(
                'gender2' => array(
                    'value1' => 150,
                    'value2' => 150,
                    'value3' => 150,
                ),
                'gender3' => array(
                    'value1' => 150,
                    'value2' => 150,
                    'value3' => 150,
                )
            ),
        );

So, at this way on date 22 in the graph will dissapear the line or bar stack for gender 2 and on 23 for gender 1, what i need is to keep the line or bar stack even if in 22 i don't have values for gender 2 and on 23 values for gender 1

The desired result should be:

$setDataNeed = array(
            '2017-06-21' => array(
                'gender1' => array(
                    'value1' => 100,
                    'value2' => 100,
                    'value3' => 100,
                ),
                'gender2' => array(
                    'value1' => 200,
                    'value2' => 200,
                    'value3' => 200,
                ),
                'gender3' => array(
                    'value1' => 300,
                    'value2' => 300,
                    'value3' => 300,
                )
            ),
            '2017-06-22' => array(
                'gender1' => array(
                    'value1' => 90,
                    'value2' => 90,
                    'value3' => 90,
                ), 
                /* Keep values from gender2 from 21 date*/
                'gender2' => array(
                    'value1' => 200,
                    'value2' => 200,
                    'value3' => 200,
                ),
                'gender3' => array(
                    'value1' => 200,
                    'value2' => 200,
                    'value3' => 200,
                )
            ),
            '2017-06-23' => array(
                /* Keep values for gender1 from 22 date*/
                'gender1' => array(
                    'value1' => 90,
                    'value2' => 90,
                    'value3' => 90,
                ),
                'gender2' => array(
                    'value1' => 150,
                    'value2' => 150,
                    'value3' => 150,
                ),
                'gender3' => array(
                    'value1' => 150,
                    'value2' => 150,
                    'value3' => 150,
                )
            ),
        );

2 Answers 2

1

Simply remember previous row and insert it if needed.

function prepare($aIn) {
    $aRes = array();
    $prev = array();
    $keys = array('gender1', 'gender2', 'gender3',);

    foreach ($aIn as $row) {
        foreach ($keys as $k) {
            if (!isset($row[$k]) && isset($prev[$k])) {
                $row[$k] = $prev[$k];
            }
        }

        $aRes[] = $row;
        $prev = $row;
    }

    return $aRes;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Ivan, thanks for you share, is there a way to keep the keys ?, i tried the function and added the keys and the result is the same from the first array.
If you need store keys from array use array_keys(). Or you need something different? I do not clear understand what you want.
0

Other way of doing this. It keeps the keys.

 $previousValue = null;
 foreach($setData as $key=>$value){;
     if($previousValue) {
        $setData[$key] = array_replace_recursive($previousValue,$value);
     }
     $previousValue = $value;    
 }
print_r($setData);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.