How can I write a php program to find all arrays which share at least a single element in their prefixes. Let the prefixes are one fourth of the total elements in each array. Can anyone help me to code for that? I am a fresher in php. I need this to do a project regarding near duplicate detection.
You can use PHP's built in array function array_intersect
if(array_intersect($firstArray, $secondArray) == null)
{
//do not have any element common
}
else
{
//have at least one element common
}
you can create function of this code and pass all array's pairs to get result.
Related
I have generator:
function f() {
foreach(range(1, 100) as $i) {
yield i;
}
}
Is there elegant way to make another function when I pass the generator to reverse it.
I dont want to use array since I want to be able to slice the new generator(reverse) without looping all elements.
For example:
function reverseGen(gen) {...}
There is no intrinsic “end” to a generator, e.g. while (true) yield $i++;. There’s also no way to access a specific element of a generator, nor to ask a generator for its previous value. The only thing you can do with a generator is to ask for its next value, and once you’ve done that, that value is gone and you can’t access it again.
So, no, there’s no way to iterate a generator in reverse; all you can do is store its values in an array and iterate that in reverse.
Within the first column of every row lies a position, i.e. Software Engineer, and if the user's position matches the one in that row, the nested for loop should check in each column of that row if the given id exists in the csv string for that column. I haven't worked much with arrays in PHP and most of my experience with multidimensional arrays lies within Java so pardon my java-themed PHP code:
$csv = read_csv('/filepath/tct.csv');
$csv_rows = 75;
for($i=1;$i<$csv_rows;$i++)
{
if(strtolower($user['user_pos']) == strtolower($csv[$i][0]))
{
for($j=1;j<sizeof($csv[$i]);j++)
{
if(array_search($user['id'], explode(",",$csv[$i][$j])))
{
return true;
}
}
}
}
Unfortunately, my java doesn't seem to work and all of the existing questions concerning PHP multidimensional arrays only confuse me with the $key->$value talk. Can anyone explain to me what I need to do adjust to get this to work?
So what I was doing is a perfectly acceptable (in terms of functionality) when processing multidimensional arrays, I just neglected to place the $ before j twice in the inner loop.
The most efficient and understandable way I've found thus far is:
foreach($csv as $csv_row) {
for($i=1;$i<count($csv_row);$i++) {
//do something with $csv_row[$i]
}
}
The standard-issue nested for loop is to ensure the row names aren't iterated over since I always had row names at column[0] (despite the fact that the outer foreach will loop over the column names in row 0)
Of course, this is heavily reliant on someone using the same spreadsheet/csv layout. If it's a csv of just data, I'm sure the inner loop could be changed to:
foreach($csv_row as $csv_col {
//do something with $csv_col
}
I have an array called $itemArray[] that contains a set of strings, such as:
myItem_1,
myItem_2,
myItem_3,
myItem_4,
anotherName_1,
anotherName_2,
anotherName_3,
anotherName_4
I also have the following code that goes through the array and filters out the ones that contain 'myItem'.
The loop works, however, it returns results in varied order. Is there a way to sort the result of the array so
that it lists them alphanumerically?
foreach($itemArray as $item) {
if (preg_match('/myItem/',$item))
echo '$item';
}
I don't have much experience with php and would definitely appreciate the help! Thank you!
PHP supports lots of ways to sort arrays, see how they work here. You could run it as asort($itemArray).
The right way to solve this is by sorting the array prior to outputting it. Something along the lines of:
asort($itemArray);
foreach($itemArray as $item) {
if (preg_match('/myItem/',$item))
array[index++] = $item;
}
I have this situation in PHP. I have an array that has these keys for example, wires-1, wires-2, wires-3. I need a function or way for my program to read these keys, and find that the common word is wires? How would that be accomplished in PHP? Thanks for your help.
Take a look at how an autocomplete's functionality works, this is similar to your approach.
I'm sure there's plenty of source codes for autocomplete on google
For the string value of every key in your array:
Throw away all non-alpha characters, i.e. leave only letters such that ctype_alpha($remaining_text) should return true.
Keep an array with the found words as keys, and their frequencies as values, as such:
$array = new array();
function found_word($word)
{ global $array;
if(!isset($array[$word])) { $array[$word] = 1; }
else { $array[$word]++; }
}
Only nicer ;)
Sort the array in reverse by using arsort($array);
$array now contains the most found words as its first elements.
you would have to create every possible suffix of every string you have.
create a map for every suffix you found
count the occurence of every suffix in your string array
you can modify the performance with f.ex. limiting the suffix length
I am creating a grid in GD and was wondering if this is the right way to do it.
I have 2 arrays. One contains all X values, the other contains all Y values.
foreach ($xpointsArray as $xvalue) {
foreach ($ypointsArray as $yvalue) {
// Draw point at coordinates $xvalue, $yvalue
}
}
I just think there must be a more elegant way to set this up, and I would like to further
access the points values without doing this every time.
What you're doing is correct. There's possibly some more elegant solutions using a single two dimensional array, but either way you have to iterate through two loops.
Additionally, using a two dimensional array you could reference specific points by $Array[$x][$y] to get a specific point.
foreach, while, for are nice and I personally would prefer them, but since you are asking, what about using array_walk()?
array_walk($array1, "print_sudoku_field", $array2);