Answer the question
In order to leave comments, you need to log in
How to generate a unique element with respect to an array?
I create keys using $unique = uniqid()
How to additionally check uniqid () with each key of the array and only if no matches are found, save to a variable?
Unsuccessfully trying to do a search through the array iteration
$arr = R::getAll('SELECT `key` FROM `itemscategory` WHERE `id_user` = 100');
$unique = "5e9dfcc49a460"; // существующий key
if (array_key_exists($unique, $arr)) {
$unique = uniqid();
} else {
$reallyUniqueKey = $unique;
echo $reallyUniqueKey; // 5e9dfcc49a460
}
array(4) {
[0]=>
array(1) {
["key"]=>
string(13) "5e9dfcc295981"
}
[1]=>
array(1) {
["key"]=>
string(13) "5e9dfcc49a460"
}
[2]=>
array(1) {
["key"]=>
string(13) "5e9dfcc5b1ff8"
}
[3]=>
array(1) {
["key"]=>
string(13) "5e9dfcc637f75"
}
}
Answer the question
In order to leave comments, you need to log in
if(!in_array($unique, $arr))
$unique = uniqid();
else
$reallyUniqueKey = $unique;
echo $reallyUniqueKey;
You can search in an array like this:
if (0 === count(array_filter($arr, function($el) use ($unique) {return $el['key'] == $unique;}))) {
// йее, уникальное!
} else {
// нашлись с таким значением
}
uniqid('', TRUE)
DISTINCT `key`
- they will not match if there were repetitions. Oh my God.
Well, I wrote you how to do
$unique = "5e9dfcc49a460";
$arr = R::getAll('SELECT `key` FROM `itemscategory` WHERE `id_user` = ? and title = ?',[$user_id, $unique]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question