1

I am having one file which contains some stuff. I want to use first column of the file and than make an array of that column. Now when i use grep in PHP to find the associated row from main file as per made array value the script just hangs produce no output. Below is my code-:

<?php
$temp=exec("wc -l /root/live/lastDateTemplate-28-11-2013.csv");
$removeHeading=$temp -1;
$getNewsletterNameFile=exec("awk 'BEGIN { FS = \",\" } ; { print $1 }'    /root/live/lastDateTemplate-28-11-2013.csv | tail -$removeHeading  >  /root/live/demoFile.txt");
$fp=fopen('/root/live/demoFile.txt', 'r');
while (!feof($fp))
 {
  $getNewsletter=fgets($fp);
  $getNewsletter=trim($getNewsletter);
  $newsLetterName[]=$getNewsletter;
  }
  fclose($fp);
  $lenOfNewsletterFile=count($newsLetterName);
  $requiredLength=$lenOfNewsletterFile - 1;
  $current_day_csvFile ="/root/live/lastDateTemplate-28-11-2013.csv";
  for($i=0;$i<=$requiredLength;$i++)
   {
  $getRow=exec("grep ".$newsLetterName[$i]." ".$current_day_csvFile);
   }
  ?>
5
  • 1
    What is it that you're actually trying to achieve? You're basically using PHP as a shell. You should probably either use PHP to do PHP things, or use the shell, but not both. Commented Nov 30, 2013 at 22:15
  • what do you want to say i am doing wrong, there is no syntax error PHP provide functionality to execute shell commands also. So why you are saying this that I have to fully use shell or PHP. Commented Nov 30, 2013 at 22:21
  • Can you explain in English what it is that you're trying to do as the result of this program? You are undoubtedly doing more work than you actually need to. Commented Nov 30, 2013 at 22:23
  • I'm with Andy Lester. As far as I can see, all that work can be done by awk. You are mixing many tools while not using one of them in the correct way.. that's why -1 Commented Nov 30, 2013 at 22:23
  • I am the file and than use stats to show on web can it been done than please help. There is one .csv file which have 20 rows and 15 column column 1 contains newsletter name . Which i just separated using AWK and made new file, now i read this file and make array of newsletter name . And than i want to grep newsletter name and make array of each column . Which i use to show on web. If you can suggest me another way I will happy and thankfull to you friends. Commented Nov 30, 2013 at 22:35

1 Answer 1

1

If I understand the question correctly, you'd like to create an assoc array from a csv where the key is the first column and the values are arrays containing the other columns. If that's so I'd reckon you should remove the awk/grep subshells as they're doing redundant work, a single pass of php alone would suffice. Here is a what I'd do:

$file = '/root/live/lastDateTemplate-28-11-2013.csv';

$rows = array();
# read the file line by line:
foreach (file($file) as $line) {
    # split the first column from the others:
    list($title, $csv_attributes) = explode(',', $line, 2);
    # split the attributes into an array:
    $attributes = explode(',', $csv_attributes);
    $rows[$title] = $attributes;
}
# remove the first row, the header line:
array_shift($rows);

PHP has many powerful string/array functions that make these sorts of problems a breeze, I'd suggest you check them out, they'll make your life easier!

http://www.php.net/manual/en/ref.strings.php https://www.php.net/manual/en/ref.array.php

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you smassey for undersatnding and resolving the isse . Thanks Alot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.