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);
Related
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);
This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 5 years ago.
Hi i got this array from my database.
array (size=4)
0 =>
array (size=1)
0 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
1 =>
array (size=1)
0 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
And i need to do get like this
array (size=4)
0 =>
array (size=1)
email_1' => string 'denise#aaa.com' (length=18)
1 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
I tried with array_merge and all. But no idea how to archive this?
Do it like below:-
$final_array = array();
foreach($original_array as $key=>$val){
$final_array[$key][] = $val[0]['email_1'];
}
print_r($final_array);
Output:-https://eval.in/848213
In php it is possible to do like this
foreach ($yourArray as $arr){
$result[] = $arr[0];
}
You can get also your desired output like this:
$result = array_map('array_collapse',$yourArray);
For this just assign like this to its first element.
foreach($yourArray as $array){
$array = $array[0];
}
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 an array like the example below (from var_dump($tagged);:
array (size=1)
0 =>
array (size=7)
0 => string '#raining' (length=8)
1 => string '#raining' (length=8)
2 => string '#driving' (length=8)
3 => string '#hungrySoon' (length=11)
4 => string '#strangeworld' (length=13)
5 => string '#fruitweekFunky' (length=15)
6 => string '#kevins_rainbow_disco' (length=21)
7 => string '#raining' (length=8)
8 => string '#fruitweekFunky' (length=15)
I am simply after displaying the array as follows (From the most frequent first):
#raining
#fruitweekFunky
#driving ...etc
To achieve exactly what you've asked for (a descending ordered array of the highest occurences to the lowest) step by step:
Count the number of occurences:
$occurences = array_count_values($tagged[0]);
Sort the array (by value, because the number of occurrences is the current array value and the tag is the key - arsort() maintains the original keys):
arsort($occurences);
Get array of the keys for output (because the tags are currently keys):
var_dump(array_keys($occurences));
Fact is, $tagged is an array of arrays. So you need to take its first element.
Try :
$result = array_count_values(array_values($tagged[0]));
var_dump($result);
This question already has answers here:
How to filter an associative array comparing keys with values in an indexed array?
(12 answers)
Closed 4 months ago.
I have following arrays:
$keys
array (size=2)
0 => string 'foo' (length=3)
1 => string 'buz' (length=3)
$data
array (size=3)
'foo' => int 1
'bar' => int 2
'buz' => int 3
How to get $data array filtered by $keys values ? Desired output:
array (size=3)
'foo' => int 1
'buz' => int 3
array_intersect_key should be able to help you out here
array_intersect_key($data, array_flip($keys));
The array_flip is needed because array_intersect_key operates on keys, so this makes sure both arrays are in the right format.
DEMO: http://codepad.org/AGpDAZtE