Reputation: 13
I'm having issues splitting by two delimiters in PHP. I have the following text and I want to be able to split by the "," then split further by the "|".
Someone Name|paused,Someone Name|paused,Someone Name|paused,Someone Name|paused,Someone Name|paused,Someone Name|available,Someone Name|available,Someone Name|available
So the end result would be for me to be able to determine whether it's set to paused or available, and from there filter out so I only get paused values.
$phoneurl = "Someone Name|paused,Someone Name|paused,Someone Name|paused,Someone Name|paused,Someone Name|paused,Someone Name|available,Someone Name|available,Someone Name|available";
$array = explode(",", $phoneurl);
foreach ($array as $value) {
$split = explode("|", $value);
echo $value[0];
}
However when I try and echo $value[0] it simply echoes the first digit of each value.
I'm lost. Please help! :(
Upvotes: 0
Views: 81
Reputation: 3698
<?php
$phoneurl = "Someone Name|paused,Someone Name|paused,Someone Name|paused,Someone Name|paused,Someone Name|paused,Someone Name|available,Someone Name|available,Someone Name|available";
$array = explode(",", $phoneurl);
foreach ($array as $value) {
$split = explode("|", $value);
if($split[1] == "paused" ){
echo $split[0].'<br>';
}
}
?>
Upvotes: 0
Reputation: 1729
$phoneurl = "Someone Name1|paused,Someone Name2|paused,Someone Name3|paused,Someone Name4|paused,Someone Name5|paused,Someone Name6|available,Someone Name7|available,Someone Name8|available";
$array = explode(",", $phoneurl);
foreach ($array as $value) {
$split = explode("|", $value);
if ($split[1]=="paused")
{
echo $split[0];
echo "<br/>";
}
}
I have added a number to "someone name".
your final results are in table split
. not in table value
.
Upvotes: 0
Reputation: 1142
foreach ($array as $index => $value)
{
$split = explode("|", $value);
echo $split[0];
}
try this one.
Upvotes: 0
Reputation: 37365
That will be
$result = array_map(function($item)
{
return explode('|', $item);
}, explode(',', $string));
-i.e. first we're splitting array by ,
and then applying explode()
with |
to each element of it via array_map()
Upvotes: 1