1

I have trying to generate specific json for Rubrics which require this below format only .

Why I need this ? https://surveyjs.io/Examples/Library?id=questiontype-matrix-rubric&platform=jQuery&theme=bootstrap#content-js

For surveyJS it i am creating cells json using PHP

PHP output

Array
(
    [Attendance] => Array
        (
            [0] => Array
                (
                    [OUTSTANDING] => A
                )

            [1] => Array
                (
                    [Satisfactory] => B
                )

            [2] => Array
                (
                    [Needs Improvement] => C
                )

            [3] => Array
                (
                    [Unacceptable] => D
                )

        )

    [Punctuality] => Array
        (
            [0] => Array
                (
                    [OUTSTANDING] => A
                )

            [1] => Array
                (
                    [Satisfactory] => B
                )

            [2] => Array
                (
                    [Needs Improvement] => C
                )

            [3] => Array
                (
                    [Unacceptable] => D
                )

        )

)

Output I have been trying to achieve and required

               {
                    "Attendance": {
                        "OUTSTANDING": "A",
                        "Satisfactory": "B",
                        "Needs Improvement": "C",
                        "Unacceptable": "D"
                    },
                    "Punctuality": {
                        "OUTSTANDING": "A",
                        "Satisfactory": "B",
                        "Needs Improvement": "C",
                        "Unacceptable": "D"
                    }
                }

This is what I have tried so far :-

 $finalArr = [];
            $columns = [];
            $rows = [];
            $cells = [];
            $rubics = json_decode($model->rubric_json);
            $points = json_decode($model->points_json);
            $rubicsCols = json_decode($model->rubric_json);

            if (!empty($rubicsCols[0])) {
                $columns = array_filter($rubicsCols[0], 'strlen');
            }

            if (!empty($points[0])) {
                $points = array_filter($points[0], 'strlen');
            }
            unset($rubics[0]);
            if (!empty($rubics)) {
                foreach ($rubics as $key => $data) {
                    $rows[] = [
                        'value' => $data[0],
                        'text' => $data[0]
                    ];
                }
            }

            if (!empty($rows)) {
                foreach ($rows as $rowKey => $row) {
                    foreach ($columns as $key => $column) {
                        $cells[$row['text']][] =[
                            $column =>$rubics[$rowKey + 1][$key]
                        ] ;
                    }
                }
            }


            echo "<pre>";
            print_r(json_encode($cells,JSON_PRETTY_PRINT));exit;

after json_encode($cells,JSON_PRETTY_PRINT) output is :-

{
    "Attendance": [
        {
            "OUTSTANDING": "Attends all of class.Never arrives late, leaves early, or is in and out of the class while it is in progress."
        },
        {
            "Satisfactory": "Attends most of class.Rarely arrives late, leaves early, or is in and out of the class while it is in progress."
        },
        {
            "Needs Improvement": "Sometimes absent.Sometimes arrives late, leaves early, or is in and out of the class while it is in progress."
        },
        {
            "Unacceptable": "Often absent.Often arrives late, leaves early, or is in and out of the class while it is in progress."
        }
    ],
    "Punctuality": [
        {
            "OUTSTANDING": "Always ready to start when class starts at the beginning of the day and when class resumes after breaks."
        },
        {
            "Satisfactory": "Usually ready to start when class starts at the beginning of the day and when class resumes after breaks."
        },
        {
            "Needs Improvement": "Rarely ready to start when class starts at the beginning of the day and when class resumes after breaks."
        },
        {
            "Unacceptable": "Almost never ready to start when class starts at the beginning of the day and when class resumes after breaks"
        }
    ]
}
4
  • What have you tried so far? Commented Jul 13, 2020 at 18:13
  • @xtraorange I have added an edit Sorry Commented Jul 13, 2020 at 18:20
  • 1
    no worries, it just helps provide better answers when you include that Commented Jul 13, 2020 at 18:24
  • Sure thanks mate its my first time @xtraorange Commented Jul 13, 2020 at 18:28

2 Answers 2

2

You need to flatten each sub-array. Given that structure this should work:

foreach($array as $key => $val) {
    $array[$key] = array_merge(...$val);
}

If you have an old PHP version then:

    $array[$key] = call_user_func_array('array_merge', $val);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much mate I was banging my head for last 4-5 hours You saved my time ,I learned something new .God bless
It would be easier just to build it properly first, but you didn't show that earlier. If you give an example of $columns , $rubrics and $points it's probably much easier.
You are right actually and I have just built my own thing now . Thanks for the help mate
2

According to json.org specification an associative array will be converted to an object and flat one will be an array, so you need something like:

<?php
$arr = [
    'Attendance' => [
        'OUTSTANDING' => 'A',
        'Satisfactory' => 'B',
        'Needs Improvement' => 'C',
        'Unacceptable' => 'D'
    ],
    'Punctuality' => [
        'OUTSTANDING' => 'A',
        'Satisfactory' => 'B',
        'Needs Improvement' => 'C',
        'Unacceptable' => 'D'
    ]
];

echo '<pre>';
echo json_encode($arr, JSON_PRETTY_PRINT);

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.