-1

Hello. im having this code:

public function parse_reports($filename)
{    
    $result = array();
    $fp = fopen($filename, "r");
    if (($headers = fgetcsv($fp, 0, "\t")) !== FALSE)
      if ($headers)
        while (($line = fgetcsv($fp, 0, "\t")) !== FALSE) 
          if ($line)
            if (sizeof($line)==sizeof($headers))
              $result[] = array_combine($headers,$line);
    fclose($fp);
    return $result; //return result
}

and my array list its like:

Array
( //test array1
    [0] => Array
        ( //array 0
            [VNUM] => 1
            [LOCALE_NAME] => Yang
        )

    [1] => Array
        ( //array 1
            [VNUM] => 2
            [LOCALE_NAME] => Ελληνική Έκδοση
        )

    [2] => Array
        ( //array 2
            [VNUM] => 10
            [LOCALE_NAME] => Σπαθί+0
        )

    [3] => Array
        ( //array 3
            [VNUM] => 11
            [LOCALE_NAME] => Σπαθί+1
        )

what i need is for array numbers to take VNUM Number for example looks like this:

Array
(
    [1] => Array
        (
            [VNUM] => 1 //this take the value of VNUM
            [LOCALE_NAME] => Yang
        )

    [2] => Array
        (
            [VNUM] => 2 //this take the value of VNUM
            [LOCALE_NAME] => Ελληνική Έκδοση
        )

    [10] => Array //this take the value of VNUM
        (
            [VNUM] => 10 //this take the value of VNUM
            [LOCALE_NAME] => Σπαθί+0
        )

    [11] => Array
        (
            [VNUM] => 11 //this take the value of VNUM
            [LOCALE_NAME] => Σπαθί+1
        )

as you can see what i need is to change arrays id with the VNUM that my .txt header have...if someone could help me with this i would be very happy! thank you! if you need any more details feel free to ask me

Fixed thanks to Eddie

zekephp
  • 33
  • 5
  • I would just save the proper key to start with from the vnum, as array is practically just a map(key+value) and you don't have to use number indexes. But if you have to change the key for your array, check here: https://stackoverflow.com/questions/240660/in-php-how-do-you-change-the-key-of-an-array-element – Stacking For Heap Mar 02 '18 at 08:09
  • Welcome to Stack Overflow! Stack Overflow is not a discussion forum, it is a Question and Answer site where you can ask a **specific** programming question that **can be answered** rather than discussed. Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) and then edit your question to conform with the site guidelines. Off-topic questions such as this one are routinely closed, but if edited to ask an *answerable* question, can be re-opened again. Thanks. – NightOwl888 Mar 02 '18 at 08:43

2 Answers2

0

You can use array_reduce

Note: I have to use implode and explode for this since there are hidden/special characters on text file.

Like:

public function parse_reports($filename)
{    
    $result = array();
    $fp = fopen($filename, "r");
    if (($headers = fgetcsv($fp, 0, "\t")) !== FALSE)
      if ($headers)
        while (($line = fgetcsv($fp, 0, "\t")) !== FALSE) 
          if ($line)
            if (sizeof($line)==sizeof($headers))
              $result[] = array_combine($headers,$line);
    fclose($fp);

    //To use the VNUM as the key
    $result = array_reduce($result,function($c,$v){
        $val = explode( ",", implode( ",", $v ));
        $c[ $val[0] ] = $v;
        return $c;
    }, array());

    return $result;
}

Getting this result

Array
(
    [1] => Array
        (
            [VNUM] => 1
            [LOCALE_NAME] => Yang
        )

    [2] => Array
        (
            [VNUM] => 2
            [LOCALE_NAME] => Ελληνική Έκδοση
        )

    [10] => Array
        (
            [VNUM] => 10
            [LOCALE_NAME] => Σπαθί+0
        )

    [11] => Array
        (
            [VNUM] => 11
            [LOCALE_NAME] => Σπαθί+1
        )

    [12] => Array
        (
            [VNUM] => 12
            [LOCALE_NAME] => Σπαθί+2
        )

    [13] => Array
        (
            [VNUM] => 13
            [LOCALE_NAME] => Σπαθί+3
        )
......

Doc: http://php.net/manual/en/function.array-reduce.php

Eddie
  • 26,593
  • 6
  • 36
  • 58
  • Notice: Undefined index: VNUM in C:\xampp\htdocs\Wiki\include\class\class.equipment.php on line 54 :/ – zekephp Mar 02 '18 at 08:40
  • Can you post a sample text file? Ill try to recreate it – Eddie Mar 02 '18 at 09:05
  • https://drive.google.com/file/d/1aAZCD1KdGaZ7HB0QgGZ2A4WU2XdgynMQ/view?usp=sharing thanks for your time! – zekephp Mar 02 '18 at 09:07
  • same error :/ Notice: Undefined index: VNUM in C:\xampp\htdocs\Wiki\include\class\class.equipment.php on line 54 – zekephp Mar 02 '18 at 09:21
  • OMG! Happy to help. – Eddie Mar 02 '18 at 09:37
  • Not sure why the name bit didn't work, but just a suggestion - the `array_reduce()` could be made redundant by changing the initial point where the array is loaded... `$result[] = array_combine($headers,$line);` could be `$result[$line[0]] = array_combine($headers,$line);` – Nigel Ren Mar 02 '18 at 09:48
  • I sended you email for something else on this fuction if you could help me! thanks a lot <3 – zekephp Mar 02 '18 at 17:32
0

You could use array_column() like...

return array_column ($result, null, 'VNUM'); //return result

This uses the VNUM column as an index to the array.

Update: It's possible that your column headers may include whitespace and therefore array_column() won't work as it is, but if you try...

public function parse_reports($filename)
{    
    $result = array();
    $fp = fopen($filename, "r");
    if (($headers = fgetcsv($fp, 0, "\t")) !== FALSE)
      if ($headers)
        $headers = array_map("trim", $headers );
        while (($line = fgetcsv($fp, 0, "\t")) !== FALSE) 
          if ($line)
            if (sizeof($line)==sizeof($headers))
              $result[] = array_combine($headers,$line);
    fclose($fp);
    return array_column ($result, null, 'VNUM'); //return result
}

This will clean up any header value in the array_map() call.

Update (2):

Looks as though there is something odd with the headers - one last attempt...

return array_column($result, null, $headers[0]);

Sample data...

VNUM    LOCALE_NAME
1   Yang
2   Ελληνική Έκδοση
10  Σπαθί+0
11  Σπαθί+1
12  Σπαθί+2
13  Σπαθί+3
14  Σπαθί+4
15  Σπαθί+5
16  Σπαθί+6
17  Σπαθί+7
18  Σπαθί+8
19  Σπαθί+9
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • return array_column ($result, 'LOCALE_NAME', 'VNUM'); if i do this like this its working perfectly but this will be not good cause i want to add more fields like: [VNUM] => 11 //this take the value of VNUM [LOCALE_NAME] => Σπαθί+1 [phone] => 69000000 how will i do it; :/ – zekephp Mar 02 '18 at 08:38
  • Thats why you pass null as the second paramter. This indexes the whole array and not a particular column. – Nigel Ren Mar 02 '18 at 08:40
  • with null its exacly the same code as it was: [0] => Array ( [VNUM] => 1 [LOCALE_NAME] => Yang ) – zekephp Mar 02 '18 at 08:42
  • If using ` 'LOCALE_NAME'` works, then using `null` should give the full array - that is nothing to do with how it's indexed. – Nigel Ren Mar 02 '18 at 08:46
  • With null: https://prnt.sc/ilr3bc with LOCALE_NAME: https://prnt.sc/ilr3iw i want it like the LOCALE_NAME one but to print and other columns if i add not only those two :/ – zekephp Mar 02 '18 at 08:51
  • Neither of those calls has worked properly, I've updated the answer with some code to try and clean up the headers, can you try the updated code. – Nigel Ren Mar 02 '18 at 09:01
  • One last try - change return to `return array_column($result, null, $headers[0]);` – Nigel Ren Mar 02 '18 at 09:17
  • I've tried with your sample data and the above code works, so no idea what problem your having. – Nigel Ren Mar 02 '18 at 09:21
  • but i tried with the same :/ can you upload the files to check them?? – zekephp Mar 02 '18 at 09:22
  • I've added a segment of the file you uploaded, can you try that to test. – Nigel Ren Mar 02 '18 at 09:26
  • it works on you cause VNUM LOCALE_NAME its not tab delimited but with spaces... so this is count as 1 value and it take it :/ – zekephp Mar 02 '18 at 09:32
  • Can you edit the file (just do the first couple of lines and delete the rest), it may be to do with the copy/paste at my end. – Nigel Ren Mar 02 '18 at 09:34