Reputation: 195
I have an array that looks like the example below. How to I get only the value in the first line (the audio file path)? I have already tried array_rand()
which only gives me values letter by letter. Sorry if this has been answered before, couldn't find anything myself. Thanks in advance.
Array
(
[0] => path/to/audiofile.mp3
3500440
audio/mpeg
[1] => path/to/anotheraudiofile.mp3
5661582
audio/mpeg
...
)
Edit: var_dump gives me this:
array(2) {
[0]=>
string(98) "path/to/audiofile.mp3
3500440
audio/mpeg
"
[1]=>
string(95) "path/to/anotheraudiofile.mp3
5661582
audio/mpeg
"
}
Upvotes: 0
Views: 1887
Reputation: 10968
Have you tried to split on line return ?
$tmp = explode(PHP_EOL, $myArray[0]);
$tmp = explode('"', $tmp[0]);
$path = $tmp[1];
Upvotes: 3
Reputation: 713
B"e aware of it depends on the os Line maybe diffrent character, you may use \r\n if you using a win os $Path = substr($array[0] , 0 , strpos("\n",$array[0]))
Upvotes: 0
Reputation: 76405
The real solution would be to alter the code that is actually constructing this array, however, you can easily extract what you need with a couple array function (of which there are many):
$array[0] = explode(PHP_EOL, $array[0]);//PHP_EOL is the new-line character
echo $array[0][0];//get the first line
You can process the entire array like so:
function extractFirstLine($val)
{
$val = explode(PHP_EOL, $val);
return $val[0];
}
$array = array_map('extractFirstLine', $array);
Or, since PHP 5.3 the more common approach:
$array = array_map(function($v)
{
$v = explode(PHP_EOL, $v);
return $v[0];
}, $array);
And since PHP 5.4, you can write this even shorter:
$array = array_map(function($v)
{
return (explode(PHP_EOL, $v))[0];
}, $array);
That ought to do it (Note, this code is untested, but the man pages should help you out if something isn't quite right).
Upvotes: 2