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;
});
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 a multidimensional array like so:
array (size=4)
0 =>
array (size=2)
'term' => string 'news-article' (length=12)
'count' => int 139
1 =>
array (size=2)
'term' => string 'industry-resource' (length=17)
'count' => int 37
2 =>
array (size=2)
'term' => string 'editorial' (length=9)
'count' => int 33
3 =>
array (size=2)
'term' => string 'bulletin' (length=8)
'count' => int 12
and I'm trying create a function that searches for a term and returns it's neighboring value, count.
My inclination was to use array_search(), however using this returns false, I'm guessin because it's only searching the first layer of the array (0,1,2,3).
I'm not so much looking for an exact answer but a nudge in the right direction. I'm guessing it will require looping through the array, but I do not know how to approach getting the neighboring count value once the term value is located. Any help is appreciated!
You can just loop through the array and access them directly.
$search_term = "news-article";
$count = 0;
foreach($array as $element) {
if($element['term'] == $search_term) {
$count = $element['count'];
break;
}
}
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-_]+)
I have a small tricky question with nested arrays. I am getting something like that from my database:
array
0 =>
array
'id' => string '81' (length=2)
'value' => string 'foobar' (length=6)
'created_at' => string '2012-02-18 22:09:57' (length=19)
'updated_at' => string '2012-02-18 22:09:57' (length=19)
1 =>
array
'id' => string '106' (length=3)
'value' => string 'barfoo' (length=6)
'created_at' => string '2012-02-19 15:11:47' (length=19)
'updated_at' => string '2012-02-19 15:11:48' (length=19)
What I want to achieve now is to extract a simple associative array, where one "column" becomes the key and one "column" becomes the value. For the case id / value, the result should then look like that:
array
81 => 'foobar'
106 => 'barfoo'
I know that I could do nested loops to foreach through all of the arrays, but I was wondering if there is a quicker and more native method. I was playing around with array_intersect, but it does not seem to deliver what I need.
Well, this one doesn't involve nested loops:
$result = array();
foreach($queryResult as $row) {
$result[$row['id']] = $row['value'];
}
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