3

I have a set of JSON rules that I am attempting to convert to a PHP array.

This is the set of rules:

{
"rules" : {
    "full_name" : "required",
    "email" : {
        "required" : true,
        "email" : true
    },
    "phone" : {
        "required" : true,
        "maxlength" : 10
    },
    "comments" : {
        "minlength" : 10
    }
}
}

This is the final output I am working towards:

        $rules = [
        'full_name' =>  'required',
        'email'     =>  [ 'required', 'email'],
        'phone'     =>  [ 'max_length' => 10 ],
        'message'   =>  [ 'min_length' => 10 ]
    ];

I've created this translation method that reads the json data after json_decode:

 private function convertRules($rules)
{
    $convertedRules = [];

    foreach( $rules as $key => $value ) {

        if( is_array($value)) {

            $value = $this->convertRules($value);
        }

        switch( $key ) {

            case '1':

                $value = $key;

                break;

            case 'minlength':

                $key = 'min_length';

                break;

            case 'maxlength':

                $key = 'max_length';

                break;
        }

        switch( $value ) {

            case '1':

                $value = $key;

                break;
        }

        $convertedRules[$key] = $value;
    }


    return $convertedRules;
}

But it creates an array with keys that duplicate the values.

    Array
(
    [full_name] => required
    [email] => Array
        (
            [required] => required
            [email] => email
        )

    [phone] => Array
        (
            [required] => required
            [max_length] => 10
        )

    [comments] => Array
        (
            [min_length] => 10
        )

)

How can I get an array without keys similar to the PHP rules array above?

Any PHP7 optimizations are also appreciated!

2
  • Was the final output for the phone key supposed to be 'phone' => [ 'required', 'max_length' => 10 ]? Commented Apr 9, 2016 at 6:06
  • Good catch, yes you are right. Commented Apr 9, 2016 at 6:44

1 Answer 1

1

You can tweak your convert_rules function to produce the desired result like so:

function convert_rules($rules) {
    $result = [];

    foreach ($rules as $name => $ruleSet) {
        if (!is_array($ruleSet)) {
            $result[$name] = $ruleSet;
        } else {
            $result[$name] = [];

            foreach ($ruleSet as $rule => $value) {
                switch ($rule) {
                    case 'maxlength':
                        $result[$name]['max_length'] = $value;
                        break;

                    case 'minlength':
                        $result[$name]['min_length'] = $value;
                        break;

                    default:
                        $result[$name][] = $rule;
                }
            }
        }
    }

    return $result;
}

As you can see, the default behaviour is to insert the rules without keys. For anything else, it's enough to create a separate case.

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

1 Comment

Wow, I totally over thought that.Thank you.

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.