I really should polish up on my regex but for now can anyone help with this...
((2,3,4,11,8),(5,44,67,78,32,22,111,234))
as you can see, each range of numbers is comma separated and, in this example, there are 2 ranges of of numbers.
In a live scenario there could be many numbers and a handful of ranges.
So... how do i extract something like this into a php nested array or something similar?
ANY help appreciated
Use explode() wisely and do it like this:
$ranges = '((2,3,4,11,8),(5,44,67,78,32,22,111,234))';
//break into groups
$array = explode('),(', $ranges);
//trim any parenthesis left and then split by comma ,
foreach($array as &$group)
$group = explode(',',trim($group, '()'));
//display result
var_dump($array);
This outputs:
array
0 =>
array
0 => string '2' (length=1)
1 => string '3' (length=1)
2 => string '4' (length=1)
3 => string '11' (length=2)
4 => string '8' (length=1)
1 => &
array
0 => string '5' (length=1)
1 => string '44' (length=2)
2 => string '67' (length=2)
3 => string '78' (length=2)
4 => string '32' (length=2)
5 => string '22' (length=2)
6 => string '111' (length=3)
7 => string '234' (length=3)
$input = str_replace(array('(', ')'), array('[', ']'), $input);
$output = json_decode($input);
This might work as well
Related
This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 3 years ago.
I have an array whith the following content:
array (size=5)
0 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.facebook.com' (length=24)
2 => string '4' (length=1) // order number
1 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.twiiter.com' (length=23)
2 => string '7' (length=1) // order number
2 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.instagram.com' (length=25)
2 => string '9' (length=1) // order number
3 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.linkedin.com' (length=24)
2 => string '2' (length=1) // order number
4 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.pinterest.com' (length=25)
2 => string '1' (length=1) // order number
I want to sort this array based on the number in the above code. (where is a written comment).
How can I do this?
So far I have written the following code but I do not know how to make it properly.
$arrMerge = array_merge($facebookURL, $twitterURL, $instagramURL, $linkedinURL, $pinterestURL);
$splitArr = array_chunk($arrMerge, 3);
You can use array_multisort with array_column
array_multisort(array_column($arr, 2), SORT_ASC,$arr)
You can use SORT_ASC OR SORT_DESC as you required
Live DEMO
You can use usort.
usort($data, function($a, $b) {
return $a[2] - $b[2];
});
You might also want to type-cast the result to an integer before-hand as it looks like your data is treated as a string.
usort($data, function($a, $b) {
return (int)$a[2] - (int)$b[2];
});
This is also a possible duplicate of this question.
I have $monTab, an array with nested arrays like this in php :
array (size=12)
0 =>
array (size=2)
'mon' => string '2018-01-01 00:00:00' (length=19)
'nb_argus' => string '29' (length=2)
1 =>
array (size=2)
'mon' => string '2018-02-01 00:00:00' (length=19)
'nb_argus' => string '21' (length=2)
2 =>
I am simply trying to add this new pair of key value to each of the nested arrays :
'tx' => int '50' (length=2)
So i've built a for each like that :
foreach($monTab as $item) {
$item["tx"] = 50;
}
It doesnt work at all, var_dump($monTab) shows that nothing has happened !
the tx key is not added at all, the value is not added at all to my arrays !!
Due to the side effect of using pass by reference with foreach(...), using array_walk() or array_map() may be an idea.
array_walk($monTab, function(&$m){
$m['tx'] = 50;
});
I have a string
$style = "font-color:#000;font-weight:bold;background-color:#fff";
I need only
font-color
font-weight
background-color
I have tried
preg_match_all('/(?<names>[a-z\-]+:)/', $style, $matches);
var_dump($matches);
it gives me following output
array
0 =>
array
0 => string 'font-color:' (length=11)
1 => string 'font-weight:' (length=12)
2 => string 'background-color:' (length=17)
'names' =>
array
0 => string 'font-color:' (length=11)
1 => string 'font-weight:' (length=12)
2 => string 'background-color:' (length=17)
1 =>
array
0 => string 'font-color:' (length=11)
1 => string 'font-weight:' (length=12)
2 => string 'background-color:' (length=17)
There are three problems with this output
1. It is two or three dimensional array, I need one dimensional array.
2. It is repeating the information
3. It is appending ":" at the end of each element.
I need a single array like this
array
0 => 'font-color'
1 => 'font-weight'
2 => 'background-color'
Take out the colon:
$style = "font-color:#000;font-weight:bold;background-color:#fff";
preg_match_all('/(?<names>[a-z\-]+):/', $style, $matches);
var_dump($matches['names']);
Then use $matches['names'], since you named it, so you dont have redundant informations
I am trying to turn a URI into key and value pairs in a URI class I have made. The URIs are clean and cannot be parsed into the $_GET variable. The solution has to be within PHP rather than Apache's mod_rewrite.
Consider this URI: /category/music/rows=30/page=12/
The desired key-value pairs I want from this are these:
array('category' <= 'music', 'rows' <= '30', 'page' <= '12')
To try and achieve this I wrote this example code:
$arr = array();
preg_match('/(category\/(?<category>[\w-_]+)|rows=(?<rows>\d+)|page=(?<page>\d+))+/',
"category/music/rows=30/page=12", $arr);
var_dump($arr);
Outputs:
array
0 => string 'category/music' (length=14)
1 => string 'category/music' (length=14)
'category' => string 'music' (length=5)
2 => string 'music' (length=5)
The thinking was that I wanted to match any in a group (/(match this|or this| or this)+/) once or more. I am assuming the problem here is that it matches once and then stops. Also, the parenthesis that group the or statement cause matches that aren't necessary to be stored (the string "category/music" is not required).
I have probably missed something obvious but I can't figure it out. I realize I could run preg_match three times but but it seems like there must be a way to do it in one expression. I also want to keep the code short.
Edit:
preg_match_all works and I have altered the regex to this:
/category/(?[\w-_]+)|rows=(?\d+)|page=(?\d+)/
However, I now get this result:
array
0 =>
array
0 => string 'category/music' (length=14)
1 => string 'rows=30' (length=7)
2 => string 'page=12' (length=7)
'category' =>
array
0 => string 'music' (length=5)
1 => string '' (length=0)
2 => string '' (length=0)
1 =>
array
0 => string 'music' (length=5)
1 => string '' (length=0)
2 => string '' (length=0)
'rows' =>
array
0 => string '' (length=0)
1 => string '30' (length=2)
2 => string '' (length=0)
2 =>
array
0 => string '' (length=0)
1 => string '30' (length=2)
2 => string '' (length=0)
'page' =>
array
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '12' (length=2)
3 =>
array
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '12' (length=2)
It would be ideal if it didn't also store "category/music","rows+30","page=12". It has also matched a lot of unnecessary blank strings.
Try using preg_match_all and see if that helps. preg_match stops after it finds the first match.
To get rid of match 1 ('category/music'), try to use a non-capturing group, so do:
(?:category\/(?<category>[\w-_]+)
instead of
(category\/(?<category>[\w-_]+)
take this string as an example: "will see you in London tomorrow and Kent the day after tomorrow".
How would I convert this to an associative array that contains the keywords as keys, whilst preferably missing out the common words, like this:
Array ( [tomorrow] => 2 [London] => 1 [Kent] => 1)
Any help greatly appreciated.
I would say you could :
split the string into an array of words
with explode
or preg_split
depending on the complexity you'll accept for your words separators
use array_filter to only keep the lines (i.e. words) you want
the callback function will have to return false for all non-valid-words
and, then, use array_count_values on the resulting list of words
which will count how many times each words is present in the array of words
EDIT : and, just for fun, here's a quick example :
First of all, the string, that gets exploded into words :
$str = "will see you in London tomorrow and Kent the day after tomorrow";
$words = preg_split('/\s+/', $str, -1, PREG_SPLIT_NO_EMPTY);
var_dump($words);
Which gets you :
array
0 => string 'will' (length=4)
1 => string 'see' (length=3)
2 => string 'you' (length=3)
3 => string 'in' (length=2)
4 => string 'London' (length=6)
5 => string 'tomorrow' (length=8)
6 => string 'and' (length=3)
7 => string 'Kent' (length=4)
8 => string 'the' (length=3)
9 => string 'day' (length=3)
10 => string 'after' (length=5)
11 => string 'tomorrow' (length=8)
Then, the filteting :
function filter_words($word) {
// a pretty simple filter ^^
if (strlen($word) >= 5) {
return true;
} else {
return false;
}
}
$words_filtered = array_filter($words, 'filter_words');
var_dump($words_filtered);
Which outputs :
array
4 => string 'London' (length=6)
5 => string 'tomorrow' (length=8)
10 => string 'after' (length=5)
11 => string 'tomorrow' (length=8)
And, finally, the counting :
$counts = array_count_values($words_filtered);
var_dump($counts);
And the final result :
array
'London' => int 1
'tomorrow' => int 2
'after' => int 1
Now, up to you to build up from here ;-)
Mainly, you'll have to work on :
A better exploding function, that deals with ponctuation (or deal with that during filtering)
An "intelligent" filtering function, that suits your needs better than mine
Have fun !
You could have a table of common words, then go through your string one word at a time, checking if it exists in the table, if not, then add it to your associative array, or +1 to it if it already exists.
using a blacklist of words not to be included
$str = 'will see you in London tomorrow and Kent the day after tomorrow';
$skip_words = array( 'in', 'the', 'will', 'see', 'and', 'day', 'you', 'after' );
// get words in sentence that aren't to be skipped and count their values
$words = array_count_values( array_diff( explode( ' ', $str ), $skip_words ) );
print_r( $words );