-1

I Have Two Arrays.

$uploaded_array = array("image/jpeg", "image/jpeg", "image/gif", "image/png", "text/html");
$allowed_array = array("image/jpeg", "image/gif", "image/png");

I need a function to achieve a specific goal. The Conditions are, 1. uploaded_array can only contain elements from allowed_array. 2. it is okay for uploaded_array to not contain all the elements present in allowed_array but the uploaded_array should not contain any element which is not present in the allowed_array. 3. it is also okay for uploaded_array to have duplicates element as long as the element is present in allowed_array too.

I am just storing the uploaded images file type into uploaded_array and then i have defined allowed_array to set acceptable file type formats.

Thanks for the answers.

Found one solution by myself. just eliminated duplicates from the first array and compared the two arrays for the difference and then count the difference. if the count is more then 0 it means the first array had an element which is not present in the second array.

$uploaded_array = array_unique(array("image/jpeg", "image/jpeg", "image/gif", "image/png", "text/html"));
$allowed_array = array("image/jpeg", "image/gif", "image/png");
$acceptable_format=count(array_diff($uploaded_array,$allowed_array));


if($acceptable_format==0) {
//all files are acceptable
} else {
//some files are not in valid format
}
Satizh J
  • 307
  • 4
  • 14
  • 3
    So that is all the code you can think of yourself? – trincot Feb 17 '20 at 14:20
  • 1
    _“I tried various conditions but none worked”_ - show us what you actually tried then. Don’t pretend something like `if(condition){` was an actual attempt - it isn’t, just writing “condition” in some place, can hardly be considered as such here. – misorude Feb 17 '20 at 14:22
  • 1
    It is not about what I need "to solve this problem". It is about the evident lack of research on your side. When I do an internet search, there are a number of useful array functions coming back all over the place... – trincot Feb 17 '20 at 14:32
  • [1](https://stackoverflow.com/questions/17185728/to-get-all-value-from-one-array-which-is-not-exists-in-another-array), [2](https://stackoverflow.com/questions/18770342/in-php-how-can-i-find-elements-in-one-array-that-are-not-in-another), [3](https://stackoverflow.com/questions/10650918/how-to-check-if-one-array-elements-entirely-exists-in-another-array-in-php), [4](https://stackoverflow.com/questions/523796/checking-to-see-if-one-arrays-elements-are-in-another-array-in-php), [5](https://stackoverflow.com/questions/49879188/check-if-all-array-value-exist-in-another-array-value),... enough? – trincot Feb 17 '20 at 14:33
  • @trincot those didnt came up on my search engine, sorry for that. – Satizh J Feb 17 '20 at 14:37

5 Answers5

2

Use array_diff. If result is empty, it is OK, else not.

if (array_diff($uploaded_array, $allowed_array)) {
    echo "not ok";
}
trincot
  • 317,000
  • 35
  • 244
  • 286
1

Thats it:

$uploaded_array = array("image/jpeg", "image/jpeg", "image/gif", "image/png", "text/html");
$allowed_array = array("image/jpeg", "image/gif", "image/png");

foreach( $uploaded_array as $key => $mime ) {
  if( !in_array( $mime, $allowed_array ) )  {
    unset( $uploaded_array[$key] );
  }
}

print_r( $uploaded_array );
Robin Gillitzer
  • 1,603
  • 1
  • 6
  • 17
1

You can use array_intersect:

$uploaded_array = array("image/jpeg", "image/jpeg", "image/gif", "image/png", "text/html");
$allowed_array = array("image/jpeg", "image/gif", "image/png","text/php");

print_r(array_intersect($uploaded_array,$allowed_array));

Output:

Array
(
    [0] => image/jpeg
    [1] => image/jpeg
    [2] => image/gif
    [3] => image/png
)

Demo

Aksen P
  • 4,564
  • 3
  • 14
  • 27
1
$uploaded_array = array_unique(array("image/jpeg", "image/jpeg", "image/gif", "image/png", "text/html"));
$allowed_array = array("image/jpeg", "image/gif", "image/png");
$acceptable_format=count(array_diff($uploaded_array,$allowed_array));


if($acceptable_format==0) {
//all files are acceptable
} else {
//some files are not in valid format
}

Somehow made it work myself. just eliminated duplicates from first array and compared the two arrays for the difference and then count the difference. if the count is more then 0 it means the first array had element which is not present in second array.

Satizh J
  • 307
  • 4
  • 14
  • This is what I [answered](https://stackoverflow.com/a/60264538/5459839)... except that you actually don't need to count. – trincot Feb 17 '20 at 14:41
1

what about in_array+foreach?

$index = 0;
foreach($uploaded_array as $key => $val) {
    if(in_array($val, $allowed_array)) {
        $aa[$index++] = $val;
    }
}
emololftw
  • 31
  • 6