Sort array not working - php
I am trying to sort array alphabetically using php sort function.
Array ( [0] => Open Sans [1] => Wellfleet [2] => Rambla [3] => Dosis [4] => Noto Sans [5] => Domine [6] => Signika Negative [7] => Arvo [8] => Neuton [9] => Rufina [10] => Tinos [11] => Podkova [12] => Magra [13] => Bitter [14] => Anton [15] => Libre Baskerville [16] => Tienne [17] => Roboto [18] => Ruda [19] => Merriweather [20] => Amaranth [21] => Playfair Display SC [22] => Cinzel Decorative [23] => Nobile [24] => Volkhov [25] => Nunito [26] => Merriweather Sans [27] => Stardos Stencil [28] => Bree Serif )
I have tried this one
$heading_fonts = sort($heading_fonts);
Eventually I am combining array to get same key and value.
$heading_fonts = array_combine($heading_fonts, $heading_fonts);
But giving me an error.
Warning: array_combine() expects parameter 1 to be array, boolean given in...
Any idea how can I sort an array to work?
The sort function will sort your array in place and return a boolean value indicating its success. You should not assign its return value to the array. Use just:
sort($heading_fonts);
Learn to use the documentation:
bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Notice the ampersand (&) in-front of the $array parameter. The ampersand is a reference operator.
It is also important to always take note of a function's return value. Many questions here on SO can be solved simply by finding out what a function actually does, and what it returns.
Return Values
Returns TRUE on success or FALSE on failure.
So, this is how you sort an array:
$someArray = array(8,2,6,1,0);
sort($someArray);
// $someArray is now sorted
If you assign the return value to $someArray, like this:
$someArray = array(8,2,6,1,0);
$someArray = sort($someArray);
...then $someArray is overwritten by the sort() function's return value (which would be TRUE in this case).
The "Examples" section of the documentation is also very instructive.
Related
How to split 1 array into 2 arrays, remove certain items, and combine them again into 1 array in PHP?
i want to create something using array. I have 1 array and i need to split it into 2 array. After that search specific items from both array and remove it then combine it 2 array into 1 array. How do i do that? I already try to use unset for array but confuse how to use it for specific key since my array data format like 16/2/1/1 and 16/2/1/5. I need to remove data which have 1. My format array is like this Array ( [1] => Array ( [0] => 16/2/1/1 --> remove this have 1 after 2 [1] => 16/2/0/2 [2] => 16/2/0/3 [3] => 16/2/0/4 [4] => 16/2/0/5 [5] => 16/2/0/6 [6] => 16/2/0/7 [7] => 16/2/0/8 [8] => 16/2/0/9 [9] => 16/2/0/10 [10] => 16/2/0/11 [11] => 16/2/0/12 [12] => 16/2/0/13 [13] => 16/2/0/14 [14] => 16/2/0/15 [15] => 16/2/0/16 ) [2] => Array ( [0] => 16/2/0/1 [1] => 16/2/0/2 [2] => 16/2/0/3 [3] => 16/2/0/4 [4] => 16/2/1/5 --> and this have 1 after 2 before 5 [5] => 16/2/0/6 [6] => 16/2/0/7 [7] => 16/2/0/8 [8] => 16/2/0/9 [9] => 16/2/0/10 [10] => 16/2/0/11 [11] => 16/2/0/12 [12] => 16/2/0/13 [13] => 16/2/0/14 [14] => 16/2/0/15 [15] => 16/2/0/16 ) ) i expect the output something like (after combine) Array ( [0] => 16/2/0/2 [1] => 16/2/0/3 [2] => 16/2/0/4 [3] => 16/2/0/6 [4] => 16/2/0/7 [5] => 16/2/0/8 [6] => 16/2/0/9 [7] => 16/2/0/10 [8] => 16/2/0/11 [9] => 16/2/0/12 [10] => 16/2/0/13 [11] => 16/2/0/14 [12] => 16/2/0/15 [13] => 16/2/0/16 ) Thanks for time to help me.
Make the array unique and then extract items that are digits/digits/NOT 1/digits: $array = preg_grep('#^\d+/\d+/[^1]/\d+#', array_unique($array));
I would use preg_grep which allows you to search an array using a Regular expression. $array =[ '16/2/0/13', '16/2/0/16', '16/2/1/5' ]; $array = preg_grep('~^16/2/0/\d+$~', $array); print_r($array); Output Array ( [0] => 16/2/0/13 [1] => 16/2/0/16 ) Sandbox The Regex ^ match start of string 16/2/0/ - match literally (at the start of string, see above) \d+ any digit one or more $ match end of string So Regular expressions is a way to do pattern matching, in this case the pattern is 16/2/0/{n} where {n} is any number. So by doing this we can find only those items that match that pattern. Then if you have duplicates, you can do array_unique() and easily remove those. There are many ways to do this array_filter with a custom callback etc. But this is the most straightforward way (if you know Regex).
How can I put strings in array starting from 0 index
hello I have a string like this "*HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#" I want to put all these values in an array. so I did this $array = explode("*",$str); Now when i printed the array the data is Array ( [0] => [1] => HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566# [2] => HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566# [3] => HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566# [4] => HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566# [5] => HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566# [6] => HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566# [7] => HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566# [8] => HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566# [9] => HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566# [10] => HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566# [11] => HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566# [12] => HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566# ) 0 index is empty. I want to start the array from 0 index. Please help. what I am doing wrong here
The first element of your array is blank because your string begins with *. To solve this simply do: $str = "*HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#"; $arr = explode('*', $str); array_shift($arr); print_r($arr); Which produces: Array ( [0] => HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566# [1] => HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566# [2] => HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566# [3] => HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566# [4] => HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566# [5] => HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566# [6] => HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566# [7] => HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566# [8] => HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566# [9] => HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566# [10] => HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566# [11] => HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566# )
You could pass it to array_filter to remove first child (index 0). It happen because you have * on first part of string. Exploding by * delimiter mean zero string on first value. Something like: $array = explode("*",$str); $array = array_filter($array);
This is occurring simply because you have a "*" character at the start of the string *HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#*.... If you always have that character at the start of the string, you will always have an empty index 0 in your resultant array - you need to either remove the first character before exploding the string or the first item of the array.
You might use a combination of array_filter to remove the empty values and array_values to reindex the array to start from 0: $str = "*HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#"; print_r(array_values(array_filter(explode("*", $str)))); Result: Array ( [0] => HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566# [1] => HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566# [2] => HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566# [3] => HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566# [4] => HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566# [5] => HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566# [6] => HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566# [7] => HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566# [8] => HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566# [9] => HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566# [10] => HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566# [11] => HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566# )
PHP double sort array based on substring
I am building a custom switch manager for work, my current issue is more an aesthetic one but I think it is a good learning experience. I have posted the array below for clarity: Array ( [1] => FastEthernet0/1 [10] => FastEthernet0/10 [11] => FastEthernet0/11 [12] => FastEthernet0/12 [13] => FastEthernet0/13 [14] => FastEthernet0/14 [15] => FastEthernet0/15 [16] => FastEthernet0/16 [17] => FastEthernet0/17 [18] => FastEthernet0/18 [19] => FastEthernet0/19 [2] => FastEthernet0/2 [20] => FastEthernet0/20 [21] => FastEthernet0/21 [22] => FastEthernet0/22 [23] => FastEthernet0/23 [24] => FastEthernet0/24 [3] => FastEthernet0/3 [4] => FastEthernet0/4 [5] => FastEthernet0/5 [6] => FastEthernet0/6 [7] => FastEthernet0/7 [8] => FastEthernet0/8 [9] => FastEthernet0/9 [25] => Null0 ) On our bigger switches I am using asort($arr); to get GigabitEthernet1/1 to come before 2/1, etc... My goal is to sort on the interface number (part after '/') so that 1/8 comes before 1/10. Could someone point me in the right direction, I want to work for the results but I am not familiar enough with PHP to know exactly where to go. Notes: On out larger multi-module switches the IDs are not in order so a sort on $arr[key] won't work.
You can use the flag while using asort(), like below. asort($arr, SORT_NATURAL | SORT_FLAG_CASE);print_r($arr); It will print/sort the data as yo need.
The SORT_NATURAL and SORT_FLAG_CASE requires v5.4+. If you're using an older version of PHP, you could do it with uasort and a custom comparison callback function. $interfaces = array(...); $ifmaj = array(); $ifmin = array(); $if_cmp = function ($a, $b) { list($amaj,$amin) = split('/',$a); list($bmaj,$bmin) = split('/',$b); $maj = strcmp($amaj,$bmaj); if ($maj!=0) return $maj; //Assuming right side is an int return $amin-$bmin; }; uasort($interfaces, $if_cmp);
remove spaces in a array php
for this array, Array ( [0] => 'HOST:' [1] => 'killbill' [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => 'Loss%' [12] => [13] => [14] => 'Snt' [15] => [16] => [17] => 'Last' [18] => [19] => [20] =>'id' ) it has empty values.by using this code it gives foreach($array as $key => $subarray) { $array[$key] = preg_grep('/^$/', $subarray, PREG_GREP_INVERT); } array ( [0] => HOST: [1] => killbill [11] => Loss% [14] => Snt [17] => Last [20] =>id ) that means it removes all the spaces. but it has the original key values .(compair above one and bellow one. then can get a clear idea what i'm saying).but i want to have it like this. array ( [0] => 'HOST:' [1] => 'killbill' [2] => 'Loss%' [3] => 'Snt' [4] => 'Last' [5] => 'id' ) key values as 1,2,3,4.... so how could i get that.
simply use this instead of Foreach array_values(array_filter($array)); that will remove the space and reorder your array. look: http://codepad.org/howl3Opj
Just use array_filter(). $array = array_filter($array); That will remove all the empty values -- ie blank, null, false, zero. If you only want to remove values that are empty strings and keep other empty values (eg zero), you can specify the second parameter for array_filter(), which allows you to define a function to specify which elements should be filtered. $array = array_filter($array, function($val) {return $val!=='';}); Hope that helps.
try this function it will help you to sort out the issue $arr = array_map('array_values', $arr);
Use array_diff function <?php $array_space = array(0,3,4,45,12,"",54,23); $remove = array(""); print_r(array_diff($array_space,$remove)); ?> See Output here
at start you need to take the array i gave him a name $arrWords $arrWords = array_filter($arrWords, 'trim'); after i clean all the empty spaces i will make new array keys $arrWords = array_values($arrWords);
serializing an array and storing in DB
i have an array like $newArray = $_POST[$newId]; print_r($newArray); it prints like Array ( [1] => Yes [2] => a [3] => b [4] => c [5] => d [6] => e [7] => f [8] => [9] => [10] => [11] => [12] => [13] => [14] => ) but when i try to store in in db after serializing like serialize($newArray) it get stored like s:211:"Array ( [1] => Yes [2] => ab [3] => c [4] => d [5] => e [6] => f [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => ) "; which is a single array element in DB..how do i properly serialize the element.
It looks like it's serializing a string, not an array. Are you sure $newArray is an array? The string returned from serialize starts with 's:211'. This means that a string was passed into serialize(). If an array were passed into serialize() the returned string would start with 'a:14'.
#pradeep where you storing $newArray in textfield, store it by serialling $arrayString = $_POST['newId']; You will get seriallized array in $arrayString. If you want to use that array before storing in database , use unserialize , else directly store in database as that is already seriallized. $array = unserialize($arrayString); This will solve your problem
Not really sure I understand the question, if you don't want to serialize though, and if you want to pass it from a text field, maybe do custom syntax like - 1:a;b:2;c:3 then explode(';',$string); loop that and for the result, explode(':',$rows) make the delimiters more difficult to clash explode("[[;]]", string); // 1]]:[[b[[;]]