0

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
"

}

7
  • 2
    Please make a var_dump, not a print_r Commented Oct 20, 2014 at 10:12
  • What do you mean by "I only get the value in the first line"? Do you mean that $array[0] only returns the first line, or you only see the first line? What system are you running this code on (windows, Linux, OSX or *NIX)? what does strlen tell you? More info would be nice Commented Oct 20, 2014 at 10:15
  • @EliasVanOotegem: $array[0] returns everything at the moment, I only need the first line. Running on windows. I would have given you more info if I weren't so lost. Will look into the other stuff. Commented Oct 20, 2014 at 10:19
  • so whats array_rand() doing in there? Commented Oct 20, 2014 at 10:23
  • Where do you get these values from. Are these newline characters between the lines? Or <br> HTML code? Commented Oct 20, 2014 at 10:25

3 Answers 3

3

Have you tried to split on line return ?

$tmp = explode(PHP_EOL, $myArray[0]);
$tmp = explode('"', $tmp[0]);
$path = $tmp[1];
Sign up to request clarification or add additional context in comments.

3 Comments

use double quotes on the newline
This worked, but I had to correct around. Double quotes on newline just like @Ghost said. '\n' doesn't work. Also $ on the variables within the explode() function. Thanks!
@charlenemasters: Please, don't hard-code the end of line character. PHP has a pre-defined constant PHP_EOL that holds the correct end of line character for the system that it's running on. If your server is running windows, PHP_EOL will be \r\n. For other systems, PHP_EOL will be \n. Using that constant makes your code more portable
2

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).

Comments

0

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]))

2 Comments

OR use the platform dependant, predefined runtime constant PHP_EOL, which will be \r\n on a windows system, and \n on Linux
Yes I wasn't have any knowledge about this constant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.