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
Related
I have the following array:
["theme","1,strand","Medical Ethics and Law,strandyear","Year 3"]
How can I extract the second last value in the array, then the second part of that value after the comma e.g. "Medical Ethics and Law,strandyear"?
This should result, for example, in 'strandyear'.
The actual value, not the position from the end, will be different for other arrays.
PHP 5.3.3...
What about the following ...
$secondLast = array_slice($source, -2, 1);
The PHP function array_slice can do the job in one line of code.
Then you 've got the second last entry from the original array. Now you can explode the string with a comma.
$parts = explode(",", $secondLast);
$strand = array_pop($parts);
The first line explodes the string by a comma. The second line gives you the last part (strand) of all exploded parts.
Try This,
First do json_decode()
then get secondLast then do strstr() to get value after comma and the do ltrim() thats it.
$str = '["theme","1,strand","Medical Ethics and Law,strandyear","Year 3"]';
echo $str;
$array = json_decode($str);
$secondLast = $array[2];//count($array)-2
echo'<pre>';print_r($array);
echo $secondLast.'<pre>';
echo ltrim(strstr($secondLast, ','),',');
die;
OUTPUT:
["theme","1,strand","Medical Ethics and Law,strandyear","Year 3"]
Array
(
[0] => theme
[1] => 1,strand
[2] => Medical Ethics and Law,strandyear
[3] => Year 3
)
Medical Ethics and Law,strandyear
strandyear
Best way to get the second last value in the array
end($array);
$second_last = prev($array);
after that you cant get the second part i.e the value after comma you can use
explode(separator,string) //function in PHP
$temp= explode(',', $second_last);//converted to an array
$second_part=$temp[1];
<?php
$a = ["theme","1,strand","Medical Ethics and Law,strandyear","Year 3"];
$b = explode(',',$a[1]);
$c = $b[1];
var_dump($c);
// tring(6) "strand"
Use explode explode
Returns an array of strings, each of which is a substring of string
formed by splitting it on boundaries formed by the string delimiter.
usage: explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] ) : array
$arr = Array("theme","1,strand","Medical Ethics and Law,strandyear","Year 3");
$secondValueAfterComma = explode(',',$arr[2]);
echo $secondValueAfterComma[1];
You may do like
$arr = ["theme","1,strand","Medical Ethics and Law,strandyear","Year 3"];
Here is your code to access 2nd last element
$value = $arr[count($arr)-2];
Example
I have CSV file that contains data of random number example data in CSV :
639123456789,73999999999,739222222222,839444444444,8639555555555....more
So, if I upload it, I want it to explode in a variable or in an array as long as I get the specific data. example of data I want to get is all 2 first line starting at 73 be extract so meaning all numbers that start with 73 only.
Example: 73999999999,739222222222
I have tried it by using only 1 number using split,substr and explode function but my problem is if the user input a CSV bulk data with no limit.
You can use substr() and loop through your array deleting the elements that do not match.
$str = '639123456789,73999999999,739222222222,839444444444,8639555555555';
$array = explode(',', $str); //Convert your string to an array.
$searchString = '73'; //Set the value your looking for.
foreach($array as $key=>$value){
if(substr($value, 0, 2) != $searchString){ //This will test first two characters.
unset($array[$key]);
}
}
$array = array_values($array);
print_r($array);
This will output:
Array
(
[0] => 73999999999
[1] => 739222222222
)
Updated
This will make a new array with only the numbers you want in it, leaving the original array untouched.
$str = '639123456789,73999999999,739222222222,839444444444,739222222222,839444444444,839444444444,73999999999';
$array = explode(',', $str);
$searchString = '73';
foreach($array as $key=>$value){
if(substr($value, 0, 2) == $searchString){
$results[] = $value;
}
}
print_r($results);
I have an array with some keys and I want to get the array values according to the array keys where the keys are in a string.
Example:
$arr = array(
"COV" => "Comilla Victorians",
"RK" => "Rajshaji Kings"
);
$str = "COV-RK";
Now I want to show Comilla Victorians VS Rajshaji Kings.
I can do it using some custom looping, But I need some smart coding here and looks your attention. I think there are some ways to make it with array functions that I don't know.
You could do something like:
echo implode(' VS ', array_map(function($v) use ($arr) { return $arr[$v]; }, explode('-', $str)));
So explode the string, map the resulting array, returning the value of the matching key in $arr, then just implode it.
You could try this:-
<?php
$arr = array(
"COV" => "Comilla Victorians",
"RK" => "Rajshaji Kings"
);
$str = "COV-RK";
$values = explode("-", $str); // explode string to get keys actually
echo $arr[$values[0]] . " VS " . $arr[$values[1]]; // print desired output
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);