Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7572471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:56:01+00:00 2026-05-30T15:56:01+00:00

These are my two methods, I need to include them in my class but

  • 0

These are my two methods, I need to include them in my class but am unsure how to do it since I’m not an OO programmer

function gcd($x,$y) 
{
    do {
        $rest=$x%$y;
    $x=$y;
    $y=$rest;
    } while($rest!==0);
    return $x;
}

function testCommonality($a) 
{
  $keys = array_keys($a[1]);
  $common = array();
  foreach ($keys as $key) {
    $v1 = $a[0][$key];
    $v2 = $a[1][$key];
    if ((gcd($v1, $v2)) != 1) $a[0]['m'] *= 1.5;
  }
  return $a;
}

print_r($parser->algorithm->testCommonality());

I need to include those in this class, and have them operate on the output of $parser->algorithm Help is greatly GREATLY appreciated

class CSVParser
{

    public $output = NULL;
    public $digits = NULL;

    public function __construct($file)
    {


        if (!file_exists($file)) {
            throw new Exception("$file does not exist");
        }

        $this->contents = file_get_contents($file);
        $this->output = array();
        $this->digits = array();
    }

    public function parse($separatorChar1 = ',', $separatorChar2 = ';', $enclosureChar = '"', $newlineChar = "\n")
    {

        $lines = explode($newlineChar, $this->contents);
        foreach ($lines as $line) {
            if (strlen($line) == 0) continue;
            $group = array();
            list($part1, $part2) = explode($separatorChar2, $line);
            $group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part1), array("$enclosureChar \t"));
            $group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part2), array("$enclosureChar \t"));
            $this->output[] = $group;
        }
    }

    private function trim_value($value, $chars)
    {
        return preg_replace("#^( |" . $chars . ")+#", '', $value);
    }

    public function algorithm()
    {
        $alpha = array(
            'c' => str_split('bcdfghjklmnpqrstvwxz'),
            'v' => str_split('aeiouy')
        );
        $i = 0;
        $k = 0;
        foreach ($this->output as $item) {
            $cnt = 0;
            $this->digits[$i] = array();
            foreach ($item as $part) {
                $this->digits[$i][$cnt] = array();
                $new = array();
                foreach ($part as $str) {
                    $v = count(array_intersect(str_split($str), $alpha['v']));
                    $c = count(array_intersect(str_split($str), $alpha['c']));
                    $t = strlen(str_replace(' ', '', $str));
                    $new = array('v' => $v, 'c' => $c, 't' => $t);
                    $this->digits[$i][$cnt][] = $new;
                }
                $cnt++;
            }
            $i++;
        }
    }
}


$parser = new CSVParser("file.txt");
$parser->parse();
print_r($parser->output);
$parser->algorithm();
print_r($parser->digits);

Thanks for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:56:03+00:00Added an answer on May 30, 2026 at 3:56 pm

    I know very little of PHP, but, if I understand correctly, what you are asking appears to be straight forward enough. I would have thought somebody would have already answered this.

    The following may be something like what you are after:

    class CSVParser
    {
    
    public $output = NULL;
    public $digits = NULL;
    
    public function __construct($file)
    {
    
    
        if (!file_exists($file)) {
            throw new Exception("$file does not exist");
        }
    
        $this->contents = file_get_contents($file);
        $this->output = array();
        $this->digits = array();
    }
    
    public function parse($separatorChar1 = ',', $separatorChar2 = ';', $enclosureChar = '"', $newlineChar = "\n")
    {
    
        $lines = explode($newlineChar, $this->contents);
        foreach ($lines as $line) {
            if (strlen($line) == 0) continue;
            $group = array();
            list($part1, $part2) = explode($separatorChar2, $line);
            $group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part1), array("$enclosureChar \t"));
            $group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part2), array("$enclosureChar \t"));
            $this->output[] = $group;
        }
    }
    
    private function trim_value($value, $chars)
    {
        return preg_replace("#^( |" . $chars . ")+#", '', $value);
    }
    
    public function algorithm()
    {
        $alpha = array(
            'c' => str_split('bcdfghjklmnpqrstvwxz'),
            'v' => str_split('aeiouy')
        );
        $i = 0;
        $k = 0;
        foreach ($this->output as $item) {
            $cnt = 0;
            $this->digits[$i] = array();
            foreach ($item as $part) {
                $this->digits[$i][$cnt] = array();
                $new = array();
                foreach ($part as $str) {
                    $v = count(array_intersect(str_split($str), $alpha['v']));
                    $c = count(array_intersect(str_split($str), $alpha['c']));
                    $t = strlen(str_replace(' ', '', $str));
                    $new = array('v' => $v, 'c' => $c, 't' => $t);
                    $this->digits[$i][$cnt][] = $new;
                }
                $cnt++;
            }
            $i++;
        }
        return $this->digits;
    }
    
    private function gcd($x,$y) 
    {
        do {
            $rest=$x%$y;
        $x=$y;
        $y=$rest;
        } while($rest!==0);
        return $x;
    }
    
    public function testCommonality($a) 
    {
        $keys = array_keys($a[1]);
        $common = array();
        foreach ($keys as $key) {
        $v1 = $a[0][$key];
        $v2 = $a[1][$key];
        if ((gcd($v1, $v2)) != 1) $a[0]['m'] *= 1.5;
      }
       return $a;
    }
    }
    
    
    $parser = new CSVParser("file.txt");
    $parser->parse();
    print_r($parser->output);
    $parser->algorithm();
    print_r($parser->digits);
    print_r( $parser->testCommonality( $parser->digits() ) );
    

    `

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two methods on a class that differ only in one method
I'm not sure if there is a difference in these two methods. If so,
These two methods exhibit repetition: public static Expression<Func<Foo, FooEditDto>> EditDtoSelector() { return f =>
These two methods appear to behave the same to me public IEnumerable<string> GetNothing() {
If these two methods are simply synonyms, why do people go to the trouble
Say you have these two methods: Number 1: void AddPerson(Person person) { // Validate
I came across these two methods to concatenate strings: Common part: char* first= First;
I thought these two methods were (memory allocation-wise) equivalent, however, I was seeing out
I wish to know all the pros and cons about using these two methods.
I've recently created these two (unrelated) methods to replace lots of boiler-plate code in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.