How to find common elements in two arrays? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
There are two arrays. It is necessary to display a list from the first array on the page, in which the elements of the second array will be highlighted, for example with a color. Probably it is necessary to read one array in a loop and compare, for example preg_match with the second array and if there is a coincidence - to allocate. Does anyone have a more beautiful solution?

There is an array_intersect function that does pretty much what you need. If you want to compare the elements using regular expressions, try preg_grep, but you will need to prepare the pattern first.

Related

Is there a way to combine more than two arrays just like array_combine does? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am passing two variables from an HMTL form as arrays and i used array combine and for each loop to loop through all the two variables but now i need to pass through 3 variables so it is possible? my current code is below and am getting an error when i add a third variable.
array_combine($firstnumber, $secondnumber) as $firstnum => $secondnum)
{ //do something with the variables }
Use array_merge, to join multiple arrays in PHP.
PHP.net
array_combine — Creates an array by using one array for keys and another for its values
array_merge — Merge one or more arrays
Be aware! The behaviour of both is different.

How do i assign the successive duplicate values of a string? PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How do i assign the successive duplicate values of a string in a single offset of an array In PHP. For example FFF2AA
In array it should be
0=>FFF
1=>2
2=AA
In array format
This post Count consecutive occurence of specific, identical characters in a string - PHP gives you almost your perfect answer. you just have tu use '*' instead of '+' in the regex if you want to take into accounts 1 character strings (like the '2' above).

Generate all combinations of string in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to generate all possible combinations of a string using PHP. I've searched this site for a while trying to find the right algorithm but I can't seem to find the correct one. To be more specific if i input the string "hi", I want it to return "hi", "ih", "h", "i". But I don't want it to reuse the same characters multiple times. So I wouldn't want "hh" and "ii". Is there an algorithm for this? Thanks!
Dont take this for production:
$string = implode('',array_unique(str_split('helpa')));
$i=0;
while($i++<50000){
$coll[substr(str_shuffle($string),0,mt_rand(1,strlen($string)))]=true;
}
ksort($coll);
print '<pre>';
print_r(array_keys($coll));
Here you can test what you want to get.
In 10000 iteration i was getting 325 combinations from a 5 length string
(what seems all possible combinations)

Cleanest way to round down numbers in a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a string such as this:
10,14.5,3.6,10.4,2
I'd like to round down each number so the result would be this:
10,14,3,10,2
I'm looking for the cleanest and fastest solution? My first thought was to explode them into an array and run a loop and floor each value, but that seems a little clunky to me.
I also thought that maybe using regex to just remove everything after a decimal for each value, but I don't know how to do that or if it's more efficient?
Anyone have any suggestions?
$array = explode(",",'10,14.5,3.6,10.4,2');
$string = implode(",",array_map('floor',$array));
Example

Recreating a dynamic php array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a php array which is created from data in an sql database.
All I want to do is recreate that identical array on another site.
I can print_r it no worries. But how would I output the structure of the array so that I can go as follows on the new site.
$newvar = array(.....);
I'm sure it's a very simply question, But I can't find an answer.
serialize Try this function. I think this is what you need
you can use json_encode to send your array and keep the structure of your array.

Categories