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
"
}
Have you tried to split on line return ?
$tmp = explode(PHP_EOL, $myArray[0]);
$tmp = explode('"', $tmp[0]);
$path = $tmp[1];
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).
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]))
Related
I want to Remove all the Elements from my array which is after , but I am unable to do this.
I have an array like this => ["18-08-2022, 05:08:23pm","18-08-2022, 05:09:05pm"]
and I want to print array something like this => ["18-08-2022","18-08-2022"]
I want to remove Elements after the ,
This is what I tried
<?php
while ($row = mysqli_fetch_array($result)) {
$Etime[] = $row["Etime"];
$Etime1[]=$row["Etime"];
$E2[]=substr($Etime1[],',',true);
}
$Etime = json_encode($Etime);
$Etime1=json_encode($Etime1);
echo $Etime1
?>
this is easy-
steps to do so-
1.Parse your array as string.
2.store that in a variable.
3. then use a if loop and use php explode funtion. explode function will seperate that string elements by the seperator in this case the "," you want to remove.
explode(string $separator, string $string)
where,
separator-The boundary string.
string-The input string.
limit
example=
$text = "hello,there";
//using explode-
var_dump( explode( ',', $input2 ) );
output=
array(2)
(
[0] => string(5) "hello"
[1] => string(5) "there"
)
more about explode() here- https://www.php.net/manual/en/function.explode.php
Here's a simple php that generates the output you want. Since you only need date bits I have provided you with only the date bits. You can get general idea from here
$arr = ["18-08-2022, 05:08:23pm","18-08-2022, 05:09:05pm"];
//
// sample output: [ "18-08-2022", "18-08-2022" ]
//
$dateMappedToYourFormat = array_map(function($dt) {
return explode(",", $dt)[0]; // getting everything before comma
}, $arr);
echo implode(",", $dateMappedToYourFormat); //to view your result
I have a string Field-Text-Datepicker. I need to "explode" it to following array:
array(
[0] => field-text-datepicker
[1] => field-text
[2] => field
);
I've tried some combinations of strrchr(), recursion and for loops, but everything I've made seem to be crazily complicated and ineffective. Is there some simple way I don't see? If not, I will post the mess I've already written. :)
Why do I need it?
For better code organisation, I sometimes need to declare multiple classes per one file. This is a problem for my SPL autoloader, that loads files according to class names. Because of that, I need to get every possible filename to load from most probable to the least.
Thanks in advance! :)
Use array_slice() with variable offsets:
$arr = explode('-', strtolower($str));
for ($i = 1, $c = count($arr); $i < $c; $i++) {
$result[] = implode('-', array_slice($arr, 0, -$i));
}
Demo
There you go!
I just tested and it prints exactly what you need =)
<?php
$str = "Field-Text-Datepicker";
$str = strtolower($str);
$array = explode('-',$str);
$count = count($array);
$output_array = [];
for($i=1;$i<=$count;$i++)
$output_array[$count-$i] = implode('-',array_slice($array,0,$i));
var_dump($output_array);
?>
I have an array of the format
array(
[0]=>x_4556v_7889;
[1]=>y_9908;
[2]=>f_5643u_7865;
)
I need to get output as
array(
[0]=> ([0] =>4556;
[1] =>7889;
)
[1]=>( [0]=>9908;)
[2] =>([0] =>5643;
[1]=>7865;
)
)
how to use strpos and find out the occurance of "_"(underscore) in string and get the next four characters in for loop.
Am getting only the first four digit code the next four digit are not getting.Kindly provide some logic.
Looks like you're trying to find all the numbers. In that case, consider trying this:
$output = array_map(function($item) {
preg_match_all("/\d+/",$item,$m);
return $m[0];
},$input);
Should work just fine :)
This is a regex free solution though..
$arr = array(
0=>'x_4556v_7889;',
1=>'y_9908;',
2=>'f_5643u_7865;'
);
$lettersarr = range('a','z');
array_unshift($lettersarr,'_');
array_unshift($lettersarr,';');
$new_arr=array_map(function ($v) use($lettersarr) {
return explode('#',wordwrap(str_replace($lettersarr,'',$v), 4, "#", true)); },$arr);
print_r($new_arr);
Demonstration
I am new PHP question and I am trying to create an array from the following string of data I have. I haven't been able to get anything to work yet. Does anyone have any suggestions?
my string:
Acct_Status=active,signup_date=2010-12-27,acct_type=GOLD,profile_range=31-35
I want to dynamically create an array called "My_Data" and have id display something like my following, keeping in mind that my array could return more or less data at different times.
My_Data
(
[Acct_Status] => active
[signup_date] => 2010-12-27
[acct_type] => GOLD
[profile_range] => 31-35
)
First time working with PHP, would anyone have any suggestions on what I need to do or have a simple solution? I have tried using an explode, doing a for each loop, but either I am way off on the way that I need to do it or I am missing something. I am getting something more along the lines of the below result.
Array ( [0] => Acct_Status=active [1] => signup_date=2010-12-27 [2] => acct_type=GOLD [3] => profile_range=31-35} )
You would need to explode() the string on , and then in a foreach loop, explode() again on the = and assign each to the output array.
$string = "Acct_Status=active,signup_date=2010-12-27,acct_type=GOLD,profile_range=31-35";
// Array to hold the final product
$output = array();
// Split the key/value pairs on the commas
$outer = explode(",", $string);
// Loop over them
foreach ($outer as $inner) {
// And split each of the key/value on the =
// I'm partial to doing multi-assignment with list() in situations like this
// but you could also assign this to an array and access as $arr[0], $arr[1]
// for the key/value respectively.
list($key, $value) = explode("=", $inner);
// Then assign it to the $output by $key
$output[$key] = $value;
}
var_dump($output);
array(4) {
["Acct_Status"]=>
string(6) "active"
["signup_date"]=>
string(10) "2010-12-27"
["acct_type"]=>
string(4) "GOLD"
["profile_range"]=>
string(5) "31-35"
}
The lazy option would be using parse_str after converting , into & using strtr:
$str = strtr($str, ",", "&");
parse_str($str, $array);
I would totally use a regex here however, to assert the structure a bit more:
preg_match_all("/(\w+)=([\w-]+)/", $str, $matches);
$array = array_combine($matches[1], $matches[2]);
Which would skip any attributes that aren't made up of letters, numbers or hypens. (The question being if that's a viable constraint for your input of course.)
$myString = 'Acct_Status=active,signup_date=2010-12-27,acct_type=GOLD,profile_range=31-35';
parse_str(str_replace(',', '&', $myString), $myArray);
var_dump($myArray);
The title of this question is self-explanatory.
I've heard I can mimic this using http_build_query, but I'd rather use a function that's meant for this.
Input example:
$assoc = array(
"fruit" => "banana",
"berry" => "blurberry",
"vegetable" => "lettice"
);
Desired output (I get this with http_build_query):
string(46) "fruit=banana,berry=blurberry,vegetable=lettice"
output from reversal wanted is the same as input - that's my current problem.
Implode with
serialize($array);
Explode with
unserialize($array);
Found a function in the php .net comments for implode:
function implode_with_key($glue = null, $pieces, $hifen = ',') {
$return = null;
foreach ($pieces as $tk => $tv) $return .= $glue.$tk.$hifen.$tv;
return substr($return,1);
}