Entirely remove empty array [duplicate] - php

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));

Related

How can i select array elements what name contains numbers? [duplicate]

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);

How can I sort an array on based number? [duplicate]

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.

Remove duplicate values from an array using PHP [duplicate]

This question already has answers here:
How to remove duplicate values from an array in PHP
(27 answers)
Closed 9 years ago.
I want to show data in array all but except the ones that are duplicates.
My array looks like this:
array (size=4)
0 => string 'Eclairage Public' (length=16)
1 => string 'Fonte de Voirie' (length=15)
2 => string 'Aire de jeux' (length=12)
3 => string 'Aire de jeux' (length=12)
4 => string 'Fonte de Voirie' (length=15)
I want it to show only:
array (size=4)
0 => string 'Eclairage Public' (length=16)
1 => string 'Fonte de Voirie' (length=15)
2 => string 'Aire de jeux' (length=12)
As you can see, the duplicate elements were removed. How can I accomplish this using PHP?
use array_unique(). It removes duplicate values from an array.
$arr = array_unique($arr);
array_unique is what you looking for.
$array = array_unique($array);

How to get colon delimited values from a string in php

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

Matching and storing multiple strings in one string with regular expressions and PHP

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-_]+)

Categories