array_diff doesn't work (PHP) - php

I have 2 array in my code, like the shown below:
<?php
$kalimat = "I just want to search something like visual odometry, dude";
$kata = array();
$eliminasi = " \n . ,;:-()?!";
$tokenizing = strtok($kalimat, $eliminasi);
while ($tokenizing !== false) {
$kata[] = $tokenizing;
$tokenizing = strtok($eliminasi);
}
$sumkata = count($kata);
print "<pre>";
print_r($kata);
print "</pre>";
//stop list
$file = fopen("stoplist.txt","r") or die("fail to open file");
$stoplist;
$i = 0;
while($row = fgets($file)){
$data = explode(",", $row);
$stoplist[$i] = $data;
$i++;
}
fclose($file);
$count = count($stoplist);
//Cange 2 dimention array become 1 dimention
for($i=0;$i<$count;$i++){
for($j=0; $j<1; $j++){
$stopword[$i] = $stoplist[$i][$j];
}
}
//Filtering process
$hasilfilter = array_diff($kata,$stopword);
var_dump($hasilfilter);
?>
$stopword contain of some stop word like attached in http://xpo6.com/list-of-english-stop-words/
All I wanna do is: I want to check if save the element that exist in array $kata and it is not exist in array $stopword
So I want to delete all the element that exist in both array $kata and $stopword .
I read some suggestion to use array_diff , but somehow it doesn't work to me. Really need your help :( Thanks.

array_diff is what you need, you are right. Here is a simplified version of what you try to do:
<?php
// Your string $kalimat as an array of words, this already works in your example.
$kata = ['I', 'just', 'want', 'to', '...'];
// I can't test $stopword code, because I don't have your file.
// So let's say it's a array with the word 'just'
$stopword = ['just'];
// array_diff gives you what you want
var_dump(array_diff($kata,$stopword));
// It will display your array minus "just": ['I', 'want', 'to', '...']
You should also double check the value of $stopword, I can't test this part (don't have your file). If it does not work for you, I guess the problem is with this variable ($stopword)

There is a problem in your $stopword array. var_dump it to see the issue.array_diff is working correct.
Try following code I wrote to make your $stopword array right:
<?php
$kalimat = "I just want to search something like visual odometry, dude";
$kata = array();
$eliminasi = " \n . ,;:-()?!";
$tokenizing = strtok($kalimat, $eliminasi);
while ($tokenizing !== false) {
$kata[] = $tokenizing;
$tokenizing = strtok($eliminasi);
}
$sumkata = count($kata);
print "<pre>";
print_r($kata);
print "</pre>";
//stop list
$file = fopen("stoplist.txt","r") or die("fail to open file");
$stoplist;
$i = 0;
while($row = fgets($file)){
$data = explode(",", $row);
$stoplist[$i] = $data;
$i++;
}
fclose($file);
$count = count($stoplist);
//Cange 2 dimention array become 1 dimention
$stopword= call_user_func_array('array_merge', $stoplist);
$new = array();
foreach($stopword as $st){
$new[] = explode(' ', $st);
}
$new2= call_user_func_array('array_merge', $new);
foreach($new2 as &$n){
$n = trim($n);
}
$new3 = array_unique($new2);
unset($stopword,$new,$new2);
$stopword = $new3;
unset($new3);
//Filtering process
$hasilfilter = array_diff($kata,$stopword);
print "<pre>";
var_dump($hasilfilter);
print "</pre>";
?>
I hope it helps

Related

Comparing two diffrent arrays returns empty array problem PHP

Hello I made a code which I don't understand why doesn't work, please share a tips with me:
In the end, arrays $recordHited and $logArray I want to compare and count the amount of different elements.
Unfortunately $result = array_diff($recordHited, $logArray); is giving a empty array :-(.
Than you in advance for any tips.
fgets(STIDIN) :
4 2
6
1 1 3 4 1 2
$input_line = trim(fgets(STDIN));
$firstLine = explode(" ",$input_line);
$keyboard = $firstLine[0];
$keyboardArray = array();
$mistakes = $firstLine[1];
$collectArray = array();
$adder = 0;
$recordHited = array();
$endOfArrayValue = 0;
//getting second and third line of innput
for($ix=0;$ix<2;$ix++){
$collectArray[]= trim(fgets(STDIN));
}
//getting line of buttons
for($iy=0;$iy<$keyboard;$iy++){
$adder++;
array_push($keyboardArray,$adder);
}
//declaring the from loop values
$times = $collectArray[0];
$log = $collectArray[1];
$logArray = explode(" ",$log);
//new array of hit
for($k=0;$k<$times;$k++){
if($endOfArrayValue<$keyboard){
$recordHited[]=$keyboardArray[$endOfArrayValue];
$endOfArrayValue++;
}else{
$endOfArrayValue = 0;
$recordHited[]=$keyboardArray[$endOfArrayValue];
$endOfArrayValue++;
};
};
//change array values
$recordHited = array_map('strval', $recordHited);
//print outcome
$result = array_diff($recordHited, $logArray);
echo var_dump($result);
echo var_dump($recordHited);
echo var_dump($logArray);
$sa = count($result);
if($sa>$mistakes){
echo "-1";
}else{
echo (($times-$sa)*1000);
}
?>

How can I open a file with a variable (array) name on php?

I tried write a php script for taking 1 main file from server and read this file, explode it(with ":" character) and keep it a array then I write this variables in array to a txt file each new line. Then I can read this file line by line but I can't open any file with fopen($variable, 'r');. My variable is; $variable = $array[1]."txt";.
My codes;
<?php
$file = file("toplist.txt");
$countLine = count($file);
$userMain = array();
$userMain[0] = "Top List";
$userNames = array();
$userNames[0] = "SampleName";
for ($i=1;$i<$countLine;$i++){
$user = explode (":",$file[$i],-1);
$userMain[$i] = $user[0];
echo $userMain[$i]."<br>"; //Test echo
}
$totalLn = count($userMain);
echo $totalLn; //Echo total line.
$myFile = $userMain[1].".txt";
$fileAA = fopen($myFile,'r');
while($line = fgets($fileAA))
$data[] = $line;
fclose($fileAA);
for ($counter = 0; $counter <= 5 ; $counter++ )
{
echo "<i>".$data[$counter]."</i><br />";
}
?>
My toplist.txt file;
toplist:
54df3a11-3ea0-37c4-8ec4-0fdd45f2e069: 211
and I have a file with a 54df3a11-3ea0-37c4-8ec4-0fdd45f2e069.txt named.
And 54df3a11-3ea0-37c4-8ec4-0fdd45f2e069.txt file contents;
name : SampleName123
destination : SampleDestination
SampleContent : SampleContent
I need the name line and just SampleName123.
$contents = file_get_contents($array[1]."txt");
$rows = explode("\n",$contents);
$user = [];
foreach($rows as $row){
$parts = explode(" : ",$row);
$user[$parts[0]] = $parts[1];
}
After this parsing you can access user as an array.
$user['name']
And you can do the listing as well:
$file = file($user['name'].".txt"); //Reads entire file into an array
foreach($file as $row){
echo "<i>".$row."</i><br />";
}
PS: It's working but need to use trim() function for taken file names from .txt file
If the file content is always the same take the first row and use substr($str, 0, 7); // Outputs: SampleName123

Select one item from array

So, I wanna select item per item from this array ["A185","A740","A540"]
Like for example I wanna select
$cie[0] = A185 or A740
with something like $cie[0] = A185
This is my code so far, since I fetch that code from a row in a MySQL table.
while ($row = pg_fetch_array($resul)) {
$cie10 = array($row["cie"]);
}
$cie = ["A185","A740"];
$values = array_count_values($cie);
$top = array_slice($values, 0, 1);
print_r($top);
What I get:
Array ( [["A185","A740","A540"]] => 1 )
It just won't work.
I'm Sure you are looking to display the data that is in the array variable
$var = ["A185","A740","A540"]; // Asume as you stored the values in the array called var
foreach($var as $x){
print_r($x);
echo "<br/>";
}
EDIT: Highlighted the code
If i understand your problem. You are looking for this:-
$Fullarray = ["A185","A740","A540"];
$cie = array_slice($Fullarray,0,2);
foreach ($cie as $name) {
$d[] = '"' . $name . '"';
}
$implodekeys = "[".implode(',',$d)."]";
$newarray[$implodekeys] =1;
echo "<pre>"; print_r($newarray);
Hope it helps!

count words of regex pattern in php?

I'm trying to match pattern 'lly' from '/usr/share/dict/words' in linux and I can display them in the browser. I want to count how many words that matches the pattern and display the total at the end of output. This is my php script.
<?php
$dfile = fopen("/usr/share/dict/words", "r");
while(!feof($dfile)) {
$mynextline = fgets($dfile);
if (preg_match("/lly/", $mynextline)) echo "$mynextline<br>";
}
?>
You can use the count function to count how many elements of an array they are. So you simply just add to this array each time, and then count it.
<?php
$dfile = fopen("/usr/share/dict/words", "r");
//Create an empty array
$array_to_count = array();
while(!feof($dfile)) {
$mynextline = fgets($dfile);
if (preg_match("/lly/", $mynextline)){
echo "$mynextline<br>";
//Add it to the array
$array_to_count[] = $mynextline;
}
}
//Now we're at the end so show the amount
echo count($array_to_count);
?>
A simpler way if you don't want to store all of the values (which might come in handy, but anyway) is to just increment to an integer variable like so:
<?php
$dfile = fopen("/usr/share/dict/words", "r");
//Create an integer variable
$count = 0;
while(!feof($dfile)) {
$mynextline = fgets($dfile);
if (preg_match("/lly/", $mynextline)){
echo "$mynextline<br>";
//Add it to the var
$count++;
}
}
//Show the number here
echo $count;
?>
PHP: Glob - Manual
sizeof(glob("/lly/*"));
#edit
Also, you can do like this:
$array = glob("/usr/share/dict/words/lly/*")
foreach ($array as $row)
{
echo $row.'<br>';
}
echo count($array);

how to get individual element from array in php?

I have dateValues as follows,
[dateVals] = 031012,041012;
it is comma seperated values. I want to make this as array and to get individual values . As i am new to PHP , i want some one's help .
$val = array[dataVals];
for($i=0;$i<sizeof($val);$i++) {
echo "val is".$val[$i]."\n";
}
is not working
use this code
$dateVals = '031012,041012';
$pieces = explode(",", $dateVals);
for($i=0;$i<sizeof($pieces);$i++) {
echo "val is".$pieces[$i]."\n";
}
it will give you proper output.
working example http://codepad.viper-7.com/PQBiZ3
$dateVals = '031012,041012';
$dateValsArr = explode(',', $dateVals);
foreach( $dateValsArr as $date) {
}
Try this code.
Try this One
$dateVals = '031012,041012';
$date_arr[] = explode(',',$dateVals);
for($i = 0 ; $i < count($date_arr) ; $i++)
{
print $date_arr[$i].'<br />';
}

Categories