N
N
NubasLol2018-11-20 11:58:13
Laravel
NubasLol, 2018-11-20 11:58:13

Laravel array validation, how to get next element?

Using the star array.* = ''rules'' everything works great, but now there is a need to compare the previous element of the array with the next one, is there such a way? Perhaps using Rule

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mykola Ivashchuk, 2018-11-20
@NubasLol

Make a custom rule and apply it to the array field, i.e.: and inside the rule, check the elements according to the desired algorithm, because you will have access to the entire array.

A
atawerrus, 2018-11-20
@atawerrus

array.0 => 'same:array.1'

A
ARX2, 2018-11-22
@ARX2

There is an easy option via array_search . I once used it, but there is no longer a need, arrays are not good to sort through.

$arrayOne = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$arrayTwo = array(0 => 'yellow', 1 => 'blue', 2 => 'green', 3 => 'red');

Примерно будет выглядеть так:
foreach($arrayOne as $array => $value){

$key = array_search($value $arrayTwo); //Первым $key = 2 

if($key ...)
 ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question