php array

系统教程 行业动态 更新时间:2024-06-14 16:52:52
php array_filter可以用作函数,但不能用作方法内部的类(php array_filter works into functions but not inside a class as a method)

我有这个代码,我想从文本中获取最常见的单词。 我使用了几个数组函数,如str_word_count() , array_count_values() ,最后是array_filter() 。 这是代码如下:

<?php /*Get the most frequent words with PHP*/ /*1. Assign the text to a variable*/ $text='NOT long ago, many parents wondered at what age they should give their child full access to the car keys. Nowadays, parents face a trickier question: At what age should a child own a smartphone? The smartphone, after all, is the key to unfettered access to the internet and the many benefits and dangers that come with it. But unlike driving a car, which is legal in some states starting at the age of 16, there is no legal guideline for a parent to determine when a child may be ready for a smartphone. The topic is being increasingly debated as children get smartphones at an ever younger age. On average, children are getting their first smartphones around age 10, according to the research firm Influence Central, down from age 12 in 2012. For some children, smartphone ownership starts even sooner — including second graders as young as 7, according to internet safety experts.'; echo $text; /*2. Set to lowercase*/ $text = strtolower($text); /*3. Separate the text into words, into an array. Accept foreign ascii characters*/ $words = str_word_count($text,1,'áâæàåãäçéêèëíîìïñóôòøõöœúûùüÿ¿¡'); echo '<br><hr><h3>Words:</h3>'; if(is_array($words)){echo '<p>$words is indeed an array</p>';} var_dump($words); /*3. Get each word's occurrences'*/ $words = array_count_values($words); echo '<br><hr><h3>Words occurrences:</h3>'; var_dump($words); /*4. Order according to occurrences*/ echo '<br><hr>'; arsort($words); var_dump($words); echo '<br><hr>'; /*5. Defining the stopwords*/ //Stopwords: $stopwords = ['the','to','it','is','a','at','for','as']; /*6. Filter out the stopwords and those words with a frequence not more than 2 occurrences*/ $palabras = array_filter($words, function($word,$index)use($stopwords){ if(!in_array($index,$stopwords)){ if($word>2){ return true; } } },ARRAY_FILTER_USE_BOTH); echo '<p>Now the most frequent words are (without the stop words):</p>'; var_dump($palabras); ?>

它工作正常。 现在我想将此代码传递给一个类。

这是类和实例的代码:

<?php /*Class to get the most frequent words from a text*/ namespace Models\Helpers\TextMining; class Word { protected $text; protected $words=[]; protected $filtered_words=[]; protected $stopwords = []; protected $lang; protected $active; protected $ascii = 'áâæàåãäçéêèëíîìïñóôòøõöœúûùüÿ¿¡'; public function __construct($text,$lang = 'es',$active = true) { $this->text = strtolower($text); $this->words = str_word_count($this->text,1,$this->ascii); $this->lang = $lang; $this->active = $active; $this->stopwords = ['the','to','it','is','a','at','for','as']; arsort(array_count_values($this->words)); } /*Show stopwords*/ public function getStopwords(){ return $this->stopwords; } /*Show the words from the text*/ public function getWords() { return $this->words; } /*Filter out the stopwords from the text and those words with an occurrence not greater than 2*/ public function applyStopwords2text() { $stopwords = $this->getStopwords(); $words = $this->getWords(); $palabras = array_filter($words, function($word,$index)use($stopwords){ if(!in_array($index,$stopwords)){ if($word>2){ return true; } } },ARRAY_FILTER_USE_BOTH); $this->filtered_words = $palabras; return $palabras; } } /***************/ $text='NOT long ago, many parents wondered at what age they should give their child full access to the car keys. Nowadays, parents face a trickier question: At what age should a child own a smartphone? The smartphone, after all, is the key to unfettered access to the internet and the many benefits and dangers that come with it. But unlike driving a car, which is legal in some states starting at the age of 16, there is no legal guideline for a parent to determine when a child may be ready for a smartphone. The topic is being increasingly debated as children get smartphones at an ever younger age. On average, children are getting their first smartphones around age 10, according to the research firm Influence Central, down from age 12 in 2012. For some children, smartphone ownership starts even sooner — including second graders as young as 7, according to internet safety experts.'; $word = new Word($text,'es',true); $stopwords = $word->getStopwords(); var_dump($stopwords); //var_dump($word); //die(); $words = $word->applyStopwords2text(); var_dump($words); ?>

问题在于array_filter()函数,因为我得到一个空数组。 没有任何内容存储在protected $filtered_words=[];的属性protected $filtered_words=[]; 当使用相同的array_filter()函数时。 为什么? 我该如何解决?

I have this code where I want to get the most frequent words from a text. I use several array functions such as str_word_count(), array_count_values(), and finally array_filter(). Here is the code as follows:

<?php /*Get the most frequent words with PHP*/ /*1. Assign the text to a variable*/ $text='NOT long ago, many parents wondered at what age they should give their child full access to the car keys. Nowadays, parents face a trickier question: At what age should a child own a smartphone? The smartphone, after all, is the key to unfettered access to the internet and the many benefits and dangers that come with it. But unlike driving a car, which is legal in some states starting at the age of 16, there is no legal guideline for a parent to determine when a child may be ready for a smartphone. The topic is being increasingly debated as children get smartphones at an ever younger age. On average, children are getting their first smartphones around age 10, according to the research firm Influence Central, down from age 12 in 2012. For some children, smartphone ownership starts even sooner — including second graders as young as 7, according to internet safety experts.'; echo $text; /*2. Set to lowercase*/ $text = strtolower($text); /*3. Separate the text into words, into an array. Accept foreign ascii characters*/ $words = str_word_count($text,1,'áâæàåãäçéêèëíîìïñóôòøõöœúûùüÿ¿¡'); echo '<br><hr><h3>Words:</h3>'; if(is_array($words)){echo '<p>$words is indeed an array</p>';} var_dump($words); /*3. Get each word's occurrences'*/ $words = array_count_values($words); echo '<br><hr><h3>Words occurrences:</h3>'; var_dump($words); /*4. Order according to occurrences*/ echo '<br><hr>'; arsort($words); var_dump($words); echo '<br><hr>'; /*5. Defining the stopwords*/ //Stopwords: $stopwords = ['the','to','it','is','a','at','for','as']; /*6. Filter out the stopwords and those words with a frequence not more than 2 occurrences*/ $palabras = array_filter($words, function($word,$index)use($stopwords){ if(!in_array($index,$stopwords)){ if($word>2){ return true; } } },ARRAY_FILTER_USE_BOTH); echo '<p>Now the most frequent words are (without the stop words):</p>'; var_dump($palabras); ?>

It's working fine. Now I want to pass this code into a class.

Here is the code of the class and instantiation:

<?php /*Class to get the most frequent words from a text*/ namespace Models\Helpers\TextMining; class Word { protected $text; protected $words=[]; protected $filtered_words=[]; protected $stopwords = []; protected $lang; protected $active; protected $ascii = 'áâæàåãäçéêèëíîìïñóôòøõöœúûùüÿ¿¡'; public function __construct($text,$lang = 'es',$active = true) { $this->text = strtolower($text); $this->words = str_word_count($this->text,1,$this->ascii); $this->lang = $lang; $this->active = $active; $this->stopwords = ['the','to','it','is','a','at','for','as']; arsort(array_count_values($this->words)); } /*Show stopwords*/ public function getStopwords(){ return $this->stopwords; } /*Show the words from the text*/ public function getWords() { return $this->words; } /*Filter out the stopwords from the text and those words with an occurrence not greater than 2*/ public function applyStopwords2text() { $stopwords = $this->getStopwords(); $words = $this->getWords(); $palabras = array_filter($words, function($word,$index)use($stopwords){ if(!in_array($index,$stopwords)){ if($word>2){ return true; } } },ARRAY_FILTER_USE_BOTH); $this->filtered_words = $palabras; return $palabras; } } /***************/ $text='NOT long ago, many parents wondered at what age they should give their child full access to the car keys. Nowadays, parents face a trickier question: At what age should a child own a smartphone? The smartphone, after all, is the key to unfettered access to the internet and the many benefits and dangers that come with it. But unlike driving a car, which is legal in some states starting at the age of 16, there is no legal guideline for a parent to determine when a child may be ready for a smartphone. The topic is being increasingly debated as children get smartphones at an ever younger age. On average, children are getting their first smartphones around age 10, according to the research firm Influence Central, down from age 12 in 2012. For some children, smartphone ownership starts even sooner — including second graders as young as 7, according to internet safety experts.'; $word = new Word($text,'es',true); $stopwords = $word->getStopwords(); var_dump($stopwords); //var_dump($word); //die(); $words = $word->applyStopwords2text(); var_dump($words); ?>

The problem is with the array_filter() function, because I get an empty array. Nothing is stored inside the property protected $filtered_words=[]; when using the same array_filter() function. Why? How do I fix this?

最满意答案

你的问题在你的构造函数中 - 你没有使代码和独立版本一样。

最后一行:

arsort(array_count_values($this->words));

array_count_values的结果没有分配给任何东西,所以arsort对临时数组进行排序,然后不保存它。

将您的构造函数更改为:

public function __construct($text,$lang = 'es',$active = true) { $this->text = strtolower($text); $this->words = str_word_count($this->text,1,$this->ascii); $this->lang = $lang; $this->active = $active; $this->stopwords = ['the','to','it','is','a','at','for','as']; $this->words = array_count_values($this->words); arsort($this->words); }

然后它会匹配非代码版本的代码。

Your problem is in your constructor - you haven't made the code do the same as in the stand-alone version.

In the last line:

arsort(array_count_values($this->words));

The result of array_count_values isn't assigned to anything, so arsort sorts the temporary array, and then doesn't save it.

Change your constructor to:

public function __construct($text,$lang = 'es',$active = true) { $this->text = strtolower($text); $this->words = str_word_count($this->text,1,$this->ascii); $this->lang = $lang; $this->active = $active; $this->stopwords = ['the','to','it','is','a','at','for','as']; $this->words = array_count_values($this->words); arsort($this->words); }

Then it will match the non-oop version of the code.

更多推荐

本文发布于:2023-04-05 12:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/a10506bcd8f29576f1df5d65bbc73e04.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:php   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!