Set associative array key

Asked

Viewed 137 times

1

Consider this array:

protected $filter = [
    'preco' => 'required;double(1,4)',
    'email' => 'required;email'
];

Step by the foreach:

protected function bootFilterPost() {

    foreach ($this->filter as $key => $value):
        $this->postRules[$key] = preg_split('/(;)/', $value);
    endforeach;

    var_dump($this->postRules);
}

and I have this exit:

array(2) {
  ["preco"]=>
  array(2) {
    [0]=>
    string(8) "required"
    [1]=>
    string(11) "double(1,4)"
  }
  ["email"]=>
  array(2) {
    [0]=>
    string(8) "required"
    [1]=>
    string(5) "email"
  }
}

but I would need it to go out that way:

array(2) {
  ["preco"]=>
  array(2) {
    ["required"]=>
    string(8) "true"
    ["double"]=>
    string(11) "1,4"
  }
  ["email"]=>
  array(2) {
    ["required"]=>
    string(8) "true"
    ["email"]=>
    string(5) "true"
  }
}

that is, I need the values to be inserted as keys and to these the string value ("true") or booleam(true);

  • The pattern is always this: if it’s just a word, stay "palavra" => true, but if there are any parentheses, stay "palavra" => "parenteses"? Is there any other format that should be considered?

  • Your question is not very well defined in terms of output. I remember that Laravel has many validation rules that do not end in true, such as min, max, different, digits, etc....

  • @Andersoncarloswoss exactly "palavra" => true as described by you. In the case of double value(1,4) would be ["double"] => "1,4". This would be the different format to be considered.

  • @Isac, I would create a string and convert it to array, but I couldn’t find a formula for that!

1 answer

1


Although I didn’t set all the rules that mattered, because the validations of the Laravel are quite complete and allow many other things not mentioned, such as, unique, min, max, digits, etc... I present you a possible solution.

This uses an auxiliary value mapping array:

private $regras = [
    "required"=>true, 
    "email"=>true
];

If the value to be considered is in this array the mapping is applied directly, otherwise try to see if the value has some separation by (

double(1,4)

Or separation by :

digits:3

Interpreting and implementing them.

Example:

protected function bootFilterPost() {

    foreach ($this->filter as $key => $value):
        $arr = preg_split('/(;)/', $value);

        foreach ($arr as $a): //ver cada regra de validação do campo

            //ver se é um que faz parte do array de regras 
            if (array_key_exists ($a, $this->regras)): 
                $this->postRules[$key][$a] = $this->regras[$a];

            elseif (strpos($a, "(") !== false): //ver se tem (
                $split = explode("(", $a); //dividir pelo (
                $this->postRules[$key][$split[0]] = "(" . $split[1];

            elseif (strpos($a, ":") !== false): //ver se tem :
                $split = explode(":", $a); //dividir pelo :
                $this->postRules[$key][$split[0]] = $split[1]; 

            endif;


        endforeach;
    endforeach;

    var_dump($this->postRules);
}

Example in Ideone

  • Perfect, this code opened the mind to do many other things.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.