0

sitting here for hours, but i can't see what the problem is.

I've got 1 file: Gross_matching.csv:

Actionspiele,77
Strategiespiele,112

And the keywords are "Actionspiele, Strategiespiele".

And would like to get this file, and search after "Actionspiele" and would like to get "77|112". But I'm getting "77|77". Does anybody know why?

<?php
if (!empty($articleData['keywords'])) {
    $temp_dir = "...";
    if (file_exists($temp_dir."gross_matching.csv")) {
        $csv = array();
        $file_gross = fopen($temp_dir."gross_matching.csv", 'r');
        while (($result = fgetcsv($file_gross, ",")) !== false) {
            $csv[] = $result;
        }
        fclose($file);
    } else {
        $articleData['attr_attr18'] .= " | File not found";
    }
    if (!empty($csv)) {
        $string = '';
        $keywords = explode(",", $articleData['keywords']);
        if(is_array($keywords)) {
            foreach($keywords as $key => $value) {
                $pos = array_search($value, $csv);
                $string .= $csv[$pos][1]."|";
            } 
            if (!empty($string)) {
                $articleData['attr_attr18'] = $string;
            } else {
                $articleData['attr_attr18'] .= " - String empty";
            } 
        }
    } else {
        $articleData['attr_attr18'] .= " - csv empty";
    }
} else {
    $articleData['attr_attr18'] .= " - not Gross";
}
?>
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
CasualBen
  • 829
  • 8
  • 22
  • Also put the search into a function of it's own. All this tree of if and else and what not and temp here, keywords there whatever only stand in the way. – hakre Jan 06 '13 at 20:25
  • It would be nice for you add more CSV data rather then just the two search information because so many things still needs to be considered like duplicate keywords etc. you should also add expected output for your data – Baba Jan 06 '13 at 21:50

1 Answers1

4

You trying to use array_search() to look for the string "Actionspiele" in the array $csv, but all the values of that array are themselves arrays, not strings, so the search fails and returns false. When you use false as an array index, it is cast to 0, so it always gives you the first row of the array.

I would rewrite your array building code too look something like this:

$csv = array();
while ( $result = fgetcsv( $file_gross, "," ) ) {
    $csv[ $result[0] ] = $result[1];
}

This will use the first column of the CSV file as the keys and the second column as the values of the $csv array, so it'll look like this:

$csv = array(
    'Actionspiele' => 77,
    'Strategiespiele' => 112,
);

Then you can simply look up the keywords in the array directly, like this:

$ids = array();
foreach ( $keywords as $keyword ) {
    $ids[] = $csv[ $keyword ];
}
$string = implode( '|', $ids );
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
  • Ps. I was expecting there to be some built-in array function that could replace the `foreach` loop in the last example, but I looked and couldn't find one, which surprises me a bit. In Perl, for example, one could do that easily with an array (well, hash) slice, but PHP's `array_slice()` doesn't seem as versatile. I suppose `$ids = array_intersect_key( $csv, array_flip( $keywords ) );` would do it, but it seems a bit awkward. – Ilmari Karonen Jan 06 '13 at 20:41
  • Why do you say its awkward ? – Baba Jan 06 '13 at 20:58
  • @Baba: Well, it's using two relatively obscure functions (three if you insist on numeric keys in the result) for what should be a simple and common operation. Even then, it will not respect the order of the keywords, nor will it allow keywords to be duplicated. (Of course, neither of those are necessarily problems in this particular case.) It just seems to me like there ought to be a better way. – Ilmari Karonen Jan 06 '13 at 21:03
  • valid point .. the only function i can think of now that can give the same result is `array_reduce` but you need a `closure` then why not create a function with foreach in the first place ... which is faster ....... + nice answer – Baba Jan 06 '13 at 21:22