0

I looked in the forum and found a lot of almost same topics but not what I am exactly looking for:

I have 1 array like:

$filterNames : Array
(
    [0] => 11424205969default-img.jpg
    [1] => myimage.png
    [2] => media/14-15/11235231video1.flv
    [3] => likemedia/10-12/233569video2.mp4
)

Another like:

$files : Array
(
    [0] => /mypath/materiales/media/14-15/video1.flv
    [1] => /mypath/materiales/likemedia/10-12/video2.mp4
)

I need to get the array of the values wich are not already existing in 1st array. But as the values are not identical I can't get it work.

Something like:

function array_check($files, $keyword) {
    $out = array();
    foreach($files as $index => $string) {
        if (strpos($string, $keyword) !== FALSE) {
            foreach ($filterNames as $namesF) {
                $out[] = array_check(array($files,'stackoverflow'),$namesF);
            }
        } 
    }
}

My question is diferent on the one presented by AbraCadaver because the values of arrays are not exactly same, in the example, they are all same (numbers)

I need an output array with only the single values like they are in the $files array, with exact path. But not if present with another path in the 1st array. Do I explain myself ? thanks for helping guys cheers

  • You want the ones from $filterNames that are not in $files or what? What is the expected output in this example? – AbraCadaver Apr 10 '15 at 15:47
  • 2
    Please [edit] your question, to include what you expect the output should be and how the current code behaves. – Artjom B. Apr 10 '15 at 15:50
  • What you are describing is an array intersection. I believe [this question](http://stackoverflow.com/questions/5299608/find-common-values-in-multiple-arrays-with-php) is what you are looking for. – Rick Smith Apr 10 '15 at 16:55
  • Just to be sure; in `$filterNames` above, is it `media/14-15/11235231video1.flv` or `media/14-15/11235231/video1.flv` (with a `/` before `video1.flv`)? – mhall Apr 13 '15 at 18:04
  • Hi, $filternames contain this array with paths & files. in fact the compare array doesn't need to get importance of the paths. Like I said in the post below, I need to have an array with the files existing in $files array without files (with or not another path) already existing in $filternames. the / is not relevant . thanks for asking – Ben-Milkshake Apr 15 '15 at 13:09

2 Answers2

0

I'm not sure what the expected output of this would be, but here is an example. In the example the 'match' array would be a key value array with these values :

[
'thing-one.jpg'] => 0
]

Where the value (0) is the key (Belonging to arr2) where the match was found. You could use a simple "in_array()" search using the key ('thing-one.jpg') to get the key where it appears in arr1.

$arr1 = [
[0] => 'folder/other-thing/thing-one.jpg'
[1] => 'folder/other-thing/thing-two.prj'
[2] => 'folder/other-thing/thing-three.png'
];

$arr2 = [
    [0] => 'omg/lol/thing-one.jpg',
    [1] => 'omg/lol/thing-eight.wtf'
];

function findAllTheThings($arr1, $arr2) {
    $match = [];
foreach ($arr1 as $path) {

    $sub_strings = explode('/', $path);

    foreach ($sub_strings as $piece) {
        if (in_array($piece, $arr2)) {
            // we have a match in the path
            $match[$piece] = in_array($piece, $arr2);
        }
    }   

}
}
  • Hi guys, Still didn't find out how to do that. Had a look @ your sugestions but can't get it done. Any other help ? :) thanks – Ben-Milkshake Apr 13 '15 at 14:50
0

Thanks for your time and answer.

First, php give me an error with the $match = []; so I put array(); Then I put the code adding a return line, but $out array is empty.

function findAllTheThings($arr1, $arr2) {
     $match = array();
    foreach ($arr1 as $path) {
    
     $sub_strings = explode('/', $path);
    
     foreach ($sub_strings as $piece) {
      if (in_array($piece, $arr2)) {
       // we have a match in the path
       $match[$piece] = in_array($piece, $arr2);
      }
     }   
    
    }
    return $match;
   }
   $out = findAllTheThings($filterNames, $files);

Still digging ;) thanks for help

EDIT : THanks guys. I need an array wich gives

$files : Array
(
    [0] => /mypath/materiales/media/14-15/video1.flv
    [1] => /mypath/materiales/likemedia/10-12/video2.mp4
)

In fact I have an array with these values and I need to filter to exclude all files already in the DB. This array is to insert in db. but don't want to have twice the files even if already exist in DB do I explain myself ? thanks for help