This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 2 years ago.
At first, im beginner.
I want to push elements from array to another array what does not contain numbers
I have an array1:
0 => string '142221A' (length=7)
1 => string 'hOUSES' (length=6)
2 => string 'bOOKS' (length=5)
3 => string 'sHOES' (length=5)
4 => string '92921' (length=5)
5 => string '12231' (length=5)
6 => string 'cARS' (length=4)
7 => string 'tOYS' (length=4)
The output i want like this, array2:
0 => string 'hOUSES' (length=6)
1 => string 'bOOKS' (length=5)
2 => string 'sHOES' (length=5)
3 => string 'cARS' (length=4)
4 => string 'tOYS' (length=4)
I dont want a solution, i want the way for it.
in PHP you can use is_numeric() method to check the string is a just a numeric or not as the following way:
$elements = ['142221A','hOUSES','bOOKS','sHOES','92921','12231','cARS','tOYS'];
$string_array = [];
foreach ($elements as $element) {
if(!is_numeric($element)) {
array_push($string_array, $element);
}
}
print_r($string_array);
but if you want to filter elements of an array to just have the elements that don't have any numeric value inside of it use the following way:
$elements = ['142221A','hOUSES','bOOKS','sHOES','92921','12231','cARS','tOYS'];
$just_string = [];
foreach ($elements as $element) {
//it will check for the element which has a digit number inside of it or not
//if it doesn't contain any number then it will be added to new array
if(preg_match('~[0-9]~', $element) != 1){
array_push($just_string, $element);
}
}
print_r($just_string);
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'm using preg_match_all() to get matches of the parameters of my url. Problem is matches function send me back an array of array. So it became difficult to explore it with a foreach function (to replace parameters by default parameters for example).
Is there a way to explore straight an array of array and send back the value of the second array, not the first, without passing by the first ?
I put you a simple example of my problem:
$var = "abababa";
preg_match_all("#(a)#", $var, $matches);
$args = array_slice ($matches, 1);
var_dump($args)
arg return me :
array (size=1)
0 =>
array (size=5)
0 => string 'a' (length=1)
1 => string 'a' (length=1)
2 => string 'a' (length=1)
3 => string 'a' (length=1)
And I wish it could return just
array (size=5)
0 => string 'a' (length=1)
1 => string 'a' (length=1)
2 => string 'a' (length=1)
3 => string 'a' (length=1)
Therefore to be able to replace args elements by othes (default elements for example).
Thanks if you can help me.
You can simply do this:
$var = "abababa";
preg_match_all("#(a)#", $var, $matches);
$args = array_slice ($matches[0], 1); // notice we are accessing the first element of $matches
var_dump($args);
This should produce your desired output.
This question already has answers here:
Remove empty array elements
(27 answers)
Closed 7 years ago.
I'm using a foreach to build this :
array (size=3)
'trainid' => string '76795' (length=5)
'traintype' => string ' -X' (length=3)
'userid' => string 'CPN' (length=3)
array (size=3)
'trainid' => string '27725' (length=5)
'traintype' => string ' -Z' (length=3)
'userid' => string 'CPN' (length=3)
array (size=0)
empty
array (size=3)
'trainid' => string '00000' (length=5)
'traintype' => string ' -X' (length=3)
'userid' => string 'CPN' (length=3)
array (size=3)
'trainid' => string '27921' (length=5)
'traintype' => string ' -Z' (length=3)
'userid' => string 'CPN' (length=3)
And as you see, there is an empty array and I would like to entirely remove this array. In fact, it crashs my sql script if there is an empty array.
Do you know how to remove it?
Thank you!
You can use array_filter, if no callback is provided, all entries equal to FALSE will be removed.
$array2 = array_filter($array);
Simply use array_filter(), It will automatically remove empty variable in array..
print_r(array_filter($arrayvariable));
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 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