remove spaces in a array php - 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);
Related
I want all arrays data in one array in php
I want all arrays data in one array not all arrays in one array only the data of all arrays in one array without any other array in php. I have tried to do too many things but still don't work for me. I also tried json_encode with preg_match But still don't work for me. Here is my code function fetchHashtags($getData){ $body = $getData; $hashtag_set = []; $array = explode('#', $body); foreach ($array as $key => $row) { $hashtag = []; if (!empty($row)) { $hashtag = explode(' ', $row); $hashtag_set[] = '#' . $hashtag[0]; } } print_r($hashtag_set); } Output Array ( [0] => #Residência [1] => #architecture [2] => #casanaserra [3] => #mountainhouse [4] => #interiores [5] => #decoration [6] => #casanaserra [7] => #casadecampo [8] => #construção [9] => #exterior [10] => #rustico [11] => #arpuro [12] => #home [13] => #riodejaneiro [14] => #construir [15] => #casasincriveis [16] => #outdoor [17] => #arquiteto [18] => #casasincriveis [19] => #montanhas [20] => #archdaily [21] => #architecturelovers [22] => #arqlovers [23] => #arqlove [24] => #homedesign [25] => #arquiteturaedecoração ) Array ( [0] => #We [1] => #ascaspenvswheaton ) Array ( [0] => #شجریان_بشنویم... [1] => #۰ [2] => #شجریان_بشنویم [3] => #_ [4] => #شجریانیها [5] => #همایون_شجریان [6] => #مژگان_شجریان [7] => #پرویزمشکاتیان [8] => #موزیک [9] => #سهراب_پورناظری [10] => #محمدرضا_شجریان [11] => #موزیک_ویدیو [12] => #ایران [13] => #ترانه_ماندگار [14] => #تصنیف [15] => #آهنگ_ایرانی [16] => #هنر [17] => #موسیقی_ایرانی [18] => #شعروشاعری [19] => #موسیقی_سنتی_ایران [20] => #آواز_سنتی [21] => #قدیمیها [22] => #دلشدگان [23] => #دلنشین [24] => #سینما [25] => #homayoun_shajarian [26] => #music [27] => #mohamadrezashajarian [28] => #home [29] => #iran [30] => #shajarian ) And one more thing i also want to remove some data that don't look like hashtags. for example: Array ( [0] => #Residência [1] => architecture // This should be removed [2] => #casanaserra [3] => mountainhouse // This also should be removed [4] => #interiores )
As you can see from my code you can use array_merge for merge arrays then use array_filter for filter value without # or other rules you need: $array = [['1', '#2', '3', '#4'], ['5', '#6']]; $flatArray = array_merge(...$array); $filteredArray = array_filter($flatArray, function($a) { if (str_contains($a, '#')) { return $a; } }); print_r($filteredArray); Result: Array ( [1] => #2 [3] => #4 [5] => #6 ) Reference: array_merge array_filter str_contains After seeing your code and the context of the situation I changed things: Instead of use explode i used preg_split for split \n and space; Delete array_map because you don't need it. $array = [" My name is Faraz shaikh i am a php developer and this my #instagram profile.Check out my #merge and my #packages with new #hashtags #chinese #art #colors #home #harmory #peace #handmade #paintings #etsy ", " My name is Hunain shaikh i am a php developer and this my #Facebook profile.Check out my #Berge and my #Hackages with new #tags #english #Kite #colours #me #memory #pee #made #paints #etsafsy "]; function fetchHashtags($getData) { // This Functions takes out the hashtags from the string and put it in to arrays. $body = $getData; $hashtag_set = []; $array = preg_split('/[\s]+/', $body); $mapArray = array_map(function($a) { return str_replace(' ', '', $a); }, $array); $filteredArray = array_filter($mapArray, function($a) { if (str_contains($a, '#')) { return $a; } }); return $filteredArray; } $recentNumberOfPosts = 1; $zeroPost = 0; //Get Post from 0 to recentNumberOfPosts | NOTE: default = 4 $finalArr = []; while ($zeroPost <= $recentNumberOfPosts) { // fetchHashtags($hashtagRecent['data'][$zeroPost]['caption']); $finalArr[] = fetchHashtags($array[$zeroPost]); $zeroPost++; } print_r(array_merge(...$finalArr)); Fiddle
PHP String to array - Stuck with my string
This is my first question, i have been able to solve many issues by using your forum, but I am coming for your help because i do not know how to solve my issue. I hope i will be understandable. I want to convert a string to an array of array, but all my readings dit not help me to find a solution. I have a string in this format $string = "records[0].CardName=TEST records[0].CardNo=01234567 records[0].CreateTime=1566835406 records[0].Door=0 records[0].Method=1 records[0].Password= records[0].RecNo=1366 records[0].Status=1 records[0].URL= records[0].UserID=9901 records[1].CardName=TEST records[1].CardNo=01234567 records[1].CreateTime=1566851904 records[1].Door=0 records[1].Method=1 records[1].Password= records[1].RecNo=1368 records[1].Status=1 records[1].URL= records[1].UserID=9901"; So you can see that this is always the same structure and only values are modified, there is more than 2 repetition of this element, but to keep it readable i just put two of them. I want to have an array created when the records[X] changes and then put each element matching records[X] into this array. I want to convert it as an array which would look like this. Array ( [0] => Array ( [CardName] =>TEST [CardNo] => 01234567 [CreateTime] => 1566835406 [Door] => 0 [Method] => 1 [Password] => [RecNo] => 1366 [Status] => 1 ) [1] => Array ( [CardName] => TEST [CardNo] => 01234567 [CreateTime] => 1566835406 [Door] => 0 [Method] => 1 [Password] => [RecNo] => 1366 [Status] => 1 ) ) The goal is to access the last element of the array and to be precise the value CreateTime like this : end($array)['CreateTime']. What I have try does not help me and i am stuck with no idea on how to deal with that issue. $array = preg_split('/\s+/',$string); if(is_array($array) { print_r($array); } // Gives me this : Array ( [0] => records[0].CardName=TEST [1] => records[0].CardNo=01234567 [2] => records[0].CreateTime=1566835406 [3] => records[0].Door=0 [4] => records[0].Method=1 [5] => records[0].Password= [6] => records[0].RecNo=1366 [7] => records[0].Status=1 [8] => records[1].CardName=TEST [9] => records[1].CardNo=01234567 [10] => records[1].CreateTime=1566835508 [11] => records[2].Door=0 [12] => records[3].Method=1 [13] => records[4].Password= [14] => records[5].RecNo=1366 [15] => records[6].Status=1 ) I have also tried something like that with multiple different delimiter with no success function multiexplode ($delimiters,$string) { $ary = explode($delimiters[0],$string); array_shift($delimiters); if($delimiters != NULL) { foreach($ary as $key => $val) { $ary[$key] = multiexplode($delimiters, $val); } } return $ary; } // Example of use $string = "records[0].CardName=APKO records[0].CardNo=88043527 records[0].CreateTime=1566835406 records[0].Door=0 records[0].Method=1 records[0].Password= records[0].RecNo=1366 records[0].Status=1 records[0].URL= records[0].UserID=9901 records[1].CardName=APKO records[1].CardNo=88043527 records[1].CreateTime=1566851904 records[1].Door=0 records[1].Method=1 records[1].Password= records[1].RecNo=1368 records[1].Status=1 records[1].URL= records[1].UserID=9901"; $delimiters = Array('/\s+/',".",'='); $res = multiexplode($delimiters,$string); print_r($res); Thanks for your help. I hope there is something that can be done to achieve this. TaG
This goes along the lines of splitting the text at multiple parts with explode(). Starting using records[ as the delimiter so that this should mean even values with spaces are maintained. Then breaking the key down into the individual components... $parts = explode("records[", $string); // Remove empty item of start array_shift($parts); $output = []; foreach ( $parts as $part ) { list($key, $value) = explode("=", $part, 2); list($id, $field) = explode("].", $key); $output[$id][$field] = rtrim($value); } print_r($output); gives... Array ( [0] => Array ( [CardName] => TEST [CardNo] => 01234567 [CreateTime] => 1566835406 [Door] => 0 [Method] => 1 [Password] => [RecNo] => 1366 [Status] => 1 [URL] => [UserID] => 9901 ) [1] => Array ( [CardName] => TEST [CardNo] => 01234567 [CreateTime] => 1566851904 [Door] => 0 [Method] => 1 [Password] => [RecNo] => 1368 [Status] => 1 [URL] => [UserID] => 9901 ) )
If there are no spaces other than the delimiter, you can replace a few things and get a valid query string. The replacements will give you a query string similar to this: records[0]["CardName"]=TEST&records[0]["CardNo"]=01234567 That can be parsed into an array: parse_str(str_replace([' ','.','='], ['&','["','"]='], $string), $result); If there are other spaces then even some fancy regex will fail, as there is no way to tell what is a delimiter and what is not.
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# )
find non duplicated item in an array
I have two arrays built from different directories that contain file names stripped of extensions. I want to find the ones that don't make a pair thus I merged the array to obtain the array below. How can I find the only non duplicate item in an array? Array ( [0] => dbbackup_2014.09.03_07_06_27 [1] => dbbackup_2014.09.03_07_07_08 [2] => dbbackup_2014.09.03_07_13_33 [3] => dbbackup_2014.09.03_07_15_24 [4] => dbbackup_2014.09.03_07_21_57 [5] => dbbackup_2014.09.03_07_22_11 [6] => dbbackup_2014.09.03_08_40_35 [7] => dbbackup_2014.09.03_08_41_36 [8] => dbbackup_2014.09.03_08_43_38 [9] => dbbackup_2014.09.04_04_59_08 [10] => dbbackup_2014.09.03_07_06_27 [11] => dbbackup_2014.09.03_07_07_08 [12] => dbbackup_2014.09.03_07_13_33 [13] => dbbackup_2014.09.03_07_15_24 [14] => dbbackup_2014.09.03_07_21_57 [15] => dbbackup_2014.09.03_07_22_11 [16] => dbbackup_2014.09.03_08_40_35 [17] => dbbackup_2014.09.03_08_41_36 [18] => dbbackup_2014.09.03_08_43_38 ) Note: it is [9]
$a = array_flip(array_filter(array_count_values($a),function($item){ return $item == 1 ? true : false; })); print_r($a); Output Array ( [1] => dbbackup_2014.09.04_04_59_08 ) Ideone
foreach($array as $data) { $values=explode("_",$data); $output[$values[1]]++; } foreach($output as $date=>$number) { if($number==1) echo $date; } Output: 2014.09.04 Fiddle
Working with checkbox array values and dumping into MYSQL
From a checkbox submit(post) i have an array like this. So this value will be dynamic in form submit. The variable name is $my_values. Output Array ( [0] => 1 [1] => 2 [2] => 2_6 [3] => 3 [4] => 3_7 [5] => 3_8 [6] => 4 [7] => 4_9 [8] => 4_10 [9] => 4_11 [10] => 4_12 [11] => 4_13 [12] => 4_13_14 [13] => 5 ) Expected Output 1,2,3,4,5,6,7,8,9,10,11,12,13,14 So I need to get the output as above in a single variable. How can I achieve that? In other words : $my_values is having the array as i have mentioned. i want one more variable $my_results which will convert the array values and give it as a single value with comma seperator (i.e 1,2,3,4,5,6,7,8,9,10,11,12,13,14) Thanks Kimz
if you need each id just once, you can do something like this: $tmp = implode(',', $my_values); $tmp = str_replace('_', ',', $tmp); $idList = explode(',', $tmp); $my_results = implode(',', array_unique($idList)); echo $my_results;