I have an array that looks like this:
Array
(
[0] => 41
[1] => 43
[2] => 44
[comment] =>
)
is there any way to implode this array ignoring ['comment']??
Now ['comment']doesn't has content but sometimes it can have content. I need to ignore ['comment'] always.
Also ['comment'] will be always the last array.
Simply use unset and implode
unset($arr['comment']);
echo implode(',',$arr);
Demo
Use a negative offset with array_splice to remove the last element of the array.
$string = implode(',', array_splice($array, -1));
Related
I may be misunderstanding the documentation, but when I code
explode(" ","here's a sentence",2 )
I end up with...
Array
(
[0] => here's
[1] => a sentence with a few words in it
)
Is there a way to make explode return...
Array
(
[0] => here's
[1] => a
)
I'm trying to make it take the first two words of any string it's given and put them into an array.
Thank you
You need array_slice.
$result = array_slice(explode(" ","here's a sentence", 3), 0, 2);
I have a string which contains numbers separated by commas. It may or may not have a space in between the numbers and a comma in the end. I want to convert it into an array, that I can do using following code:
$string = '1, 2,3,';
$array = explode(',', $string);
However the additional irregular spaces gets in the array values and the last comma causes an empty index (see image below).
How can I remove that so that I get only clean values without spaces and last empty index in the array?
Simply use array_map, array_filter and explode like as
$string = '1, 2,3,';
print_r(array_map('trim',array_filter(explode(',',$string))));
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Explanation:
Firstly I've split string into an array using explode function of PHP
print_r(explode(',',$string));
which results into
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] =>
)
So we need to remove those null values using array_filter like as
print_r(array_filter(explode(',',$string)));
which results into
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Now the final part need to remove that (extra space) from the values using array_map along with trim
print_r(array_map('trim',array_filter(explode(',',$string))));
SO finally we have achieved the part what we're seeking for i.e.
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Demo
The simple solution would be to use str_replace on the original string and also remove the last comma if it exists with a rtrim so you dont get an empty occurance at the end of the array.
$string = '1, 2,3,';
$string = rtrim($string, ',');
$string = str_replace(' ', '', $string);
$array = explode(',', $string);
print_r($array);
RESULT:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
First perform following on the string -
$string = str_replace(' ', '', $string);
Then use explode.
$string = '1, 2,3,';
$array = explode(',', $string);
$array = array_filter(array_map('trim', $array));
print_r($array);
array_map is a very useful function that allows you to apply a method to every element in an array, in this case trim.
array_filter will then handle the empty final element for you.
This will trim each value and remove empty ones in one operation
$array = array_filter( array_map( 'trim', explode( ',', $string ) ) );
I have this array and want to remove all # char from first of all values of it:
$array = ('#test' , '#test1' , '#test2' ... etc);
I know How I can to remove all special chars with "Foreach" or "for" or any function
But I need to find out is there any way to remove a special character from all values of an array with one or maximum two line in PHP
Kind Regards
Simple do an array_walk
<?php
$array = ['#test' , '#test1' , '#test2','nohash','#test4'];
array_walk($array,function (&$v){ if(strpos($v,'#')!==false){ $v = str_replace('#','',$v);}},$array);
print_r($array);
OUTPUT :
Array
(
[0] => test
[1] => test1
[2] => test2
[3] => nohash
[4] => test4
)
Much simpler if you don't need to test for the first character:
$array = str_replace('#', '', $array);
I'm using PHP.
I have the following array:
Array
(
[home] => 9
[pets] => 8
[dogs] => 7
[shampoo] => 7
[cover] => 6
)
I want to create a comma separated list which is:
home,pets,dogs,shampoo,cover
Here's what I'm trying but giving me blank string ($words is the array):
$myWords = implode(',',$words[0]);
Do I need to loop instead?
You're close. You just need the keys from that array. array_keys() will do that for you:
$myWords = implode(',',array_keys($words));
$string = implode(',', array_keys($words));
$words[0] does not exist in your array, because all of your keys are strings.
I get a string that looks like this
<br>
ACCEPT:YES
<br>
SMMD:tv240245ce
<br>
is contained in a variable $_session['result']
I am trying to parse through this string and get the following either in an array or as separate variables
ACCEPT:YES
tv240245ce
I first tried
to explode the string using as the delimiter, and that did not work
then I already tried
$yes = explode(":", strip_tags($_SESSION['result']));
echo print_r($yes);
which gives me an array like so
Array ( [0] => ACCEPT [1] => YESSEED [2] => tv240245ce ) 1
which gives me one of my answers.
Please what would be a great way of trying to achieve what I am trying to achieve?
is there a way to get rid of the first and last?
then use the remaining one as a delimiter to explode the string ?
or what's the best way to go about this ?
This will do it:
$data=preg_split('/\s?<br>\s?/', str_replace('SMMD:','',$data), NULL, PREG_SPLIT_NO_EMPTY);
See example here:
CodePad
You can also skip caring about the spurious <br> and treat the whole string as key:value format with a simple regex like:
preg_match_all('/^(\w+):(.*)/', $text, $result, PREG_SET_ORDER);
This requires that you really have line breaks in it though. Gives you a $result list which is easy to convert into an associative array afterwards:
[0] => Array
(
[0] => ACCEPT:YES
[1] => ACCEPT
[2] => YES
)
[1] => Array
(
[0] => SMMD:tv240245ce
[1] => SMMD
[2] => tv240245ce
)
First, do a str_replace to remove all instances of "SMMD:". Then, Explode on "< b r >\n". Sorry for weird spaced, it was encoding the line break.
Include the new line character and you should get the array you want:
$mystr = str_replace( 'SMMD:', '', $mystr );
$res_array = explode( "<br>\n", $mystr );