0

I have an array like that:

array(5) {
["code"]=>
int(1)
["messageError"]=>
string(27) "La typologie est incorrecte"
["model"]=>
string(3) "lot"
["grp_regles"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(3) {
["champ"]=>
string(21) "lot_surface_habitable"
["comparaison"]=>
string(7) "between"
["valeurAttendue"]=>
array(2) {
[0]=>
int(16)
[1]=>
int(40)
}
}
}
}
["prerequis"]=>
array(2) {
[0]=>
array(3) {
["champ"]=>
string(6) "typ_id"
["comparaison"]=>
string(1) "="
["valeurAttendue"]=>
int(1)
}
[1]=>
array(3) {
["champ"]=>
string(22) "tranche.fus.fup.fup_id"
["comparaison"]=>
string(1) "="
["valeurAttendue"]=>
int(1)
}
}
}

I want to do a foreach in "prerequis":

$modelRetrieve = $this->retrieveModel($model);
        $modelFind = $modelRetrieve::find($id);
        $arrayError=[];
        $query = '';
        $path = storage_path() . "/json/controle.json";
        $json = file_get_contents($path);
        foreach (json_decode($json,true) as $key => $value) {
            $test = true;
            var_dump($value);
            if($value['model'] === $model ){
            foreach ($value['prerequis'] as $key => $value2) {
                if( $test && $modelFind[$value2['champ']] == (int)$value2["valeurAttendue"] ) 
                {
                    $test = true;
                }
            }
            }
        }

I need in second foreach to use in $value2['champ'] where $value2['champ'] is "tranche.fus.fup_id. So I need to explode that to have ['tranche']['fus']['fup_id']. How to use explode with that ? thanks everyone :)

2
  • currenly you have: $value2['champ'] = "tranche.fus.fup.fup_id" and you need: $value2['champ']['tranche']['fus']['fup']['fup_id'] = "" right? Commented Jun 9, 2021 at 7:34
  • i need to replace ['champ'] with ['tranche']['fus']['fup']['fup_id']. To get the value of $value2['tranche']['fus']['fup']['fup_id'] if champ contains a '.' Commented Jun 9, 2021 at 8:33

2 Answers 2

1

you can use laravel data_get helper:

data_get($value2, $value2['champ'])
Sign up to request clarification or add additional context in comments.

1 Comment

Wow thank you, it's awesome ! i didn't know that !
0

To nest the $segments of the string starting with the innermost item of the result, we have to array_reverse() the $segments so we can loop over it and wrap each iteration's result with another array level in the next iteration until we looped through the whole $segments array.

$exploded = array_reduce(array_reverse(explode('.', $value2['champ'])), function($res, $segment) {
    return [ $segment => $res ];
}, []);

1 Comment

Please add some explanation to your answer such that others can learn from it

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.