Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to send barcode details to a database using a PHP array and want to create tables in the database according to barcode details. for example;
barcode number: D2D6000001
Here 1st D indicates the product type, 2 indicates production line, 2nd D indicates the month, 6 indicates the year and the last 6 numbers indicate product no.
I want to send each one into separate tables in the database using a PHP array. Can someone help me with this?
try below script:
<?php
$ReciveBarcode = "D2D6000001";
$GETBARCODE = str_split($ReciveBarcode,5);
$SepratedDetails['productType'] = $GETBARCODE[0][0];
$SepratedDetails['productionLine'] = $GETBARCODE[0][1];
$SepratedDetails['month'] = $GETBARCODE[0][2];
$SepratedDetails['year'] = $GETBARCODE[0][3];
$SepratedDetails['productNumber'] = $GETBARCODE[1];
echo "<pre>";print_r($SepratedDetails);
?>
output:
Array
(
[productType] => D
[productionLine] => 2
[month] => D
[year] => 6
[productNumber] => 00001
)
Using str_split("D2D6000001") to split the value to array.
You get result some thing like Array ( [0] => D[1] => 2 [2] => D [3] => 6 [4] => 0 ) .
use substr("D2D6000001",-6) to get six digit value.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
$db_string = "|1||3||1||5||1||3||8||1||10||2|2||13||2||15|";
$array = array_filter(explode("|", $db_string));
if(in_array("1", $array)){
echo"<li>
Honeymoon Packages
</li>";
}
RESULT is Coming
* Honeymoon Packages
* Honeymoon Packages
* Honeymoon Packages
* Honeymoon Packages
because in array 1 is there 4 times, how do I group this in array and print result one time without duplicate?
I do prefer splitting with a regex, so no empty values will ever occur.
$db_string = "|1||3||1||5||1||3||8||1||10||2|2||13||2||15|";
$array = preg_split('/\|/', $db_string, 0, PREG_SPLIT_NO_EMPTY);
Now the array has an element for each number in the listed order. With the help of array map we use the empty $data array and build a key for unique element. So we know how many entries are there per item.
$data = [];
array_map(function($e) use (&$data) { isset($data[$e]) ? $data[$e]++ : $data[$e] = 1; }, $array);
Array
(
[1] => 4
[3] => 2
[5] => 1
[8] => 1
[10] => 1
[2] => 3
[13] => 1
[15] => 1
)
You can use the keys to have each item uniquely.
print_r(array_unique(array_keys($data)));
Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 8
[4] => 10
[5] => 2
[6] => 13
[7] => 15
)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a table with locations stored in the format:
30°28'25"
68°28'43"
In the process of converting from DMS to DEC, I'd need a function to convert the string 68°28'43" (no spaces among values-symbols) into
$deg = 68
$min = 28
$sec = 43
Or something similar. Thanks
Simply use preg_match_all :)
the following regex simply captures every digits (\d) which are contains exactly 2 numbers ({2}) and group them (the braces around the expression)
<?php
$value = "68°28'43\"";
preg_match_all('/(\d{2})/', $value, $matches);
print_r($matches);
$matches would be an array with the captured results (the digits)
Array
(
[0] => Array
(
[0] => 68
[1] => 28
[2] => 43
)
[1] => Array
(
[0] => 68
[1] => 28
[2] => 43
)
)
And then you can simply assign it to your variables with the list() function as follow:
list($deg, $min, $sec) = $matches[0];
echo $deg; // 68
echo $min; // 28
echo $sec; // 43
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have an array, I can print it with
print_r($show_arr);
it gives me this output(html source)
Array
(
[0] => Marvel's Daredevil.S01E01 - Into the Ring.mp4
[1] => Marvel's Daredevil.S01E02 - Cut Man.mp4
[2] => Marvel's Daredevil.S02E05 - Kinbaku.mp4
[3] => Marvel's Daredevil.S02E06 - Regrets Only.mp4
)
how would I go about getting the array to look like this?
Array
(
Season[1] => Array
(
Array(
episode => "01 - Into the Ring",
file => "Marvel's Daredevil.S01E01 - Into the Ring.mp4",
)
Array(
episode => "02 - Cut Man",
file => "Marvel's Daredevil.S01E02 - Cut Man.mp4",
)
)
Season[2] => Array
(
Array(
episode => "05 - Kinbaku",
file => "Marvel's Daredevil.S02E05 - Kinbaku.mp4",
)
Array(
episode => "06 - Regrets Only",
file => "Marvel's Daredevil.S02E06 - Regrets Only.mp4",
)
)
I was bored. Just loop your array and use preg_match() to build the array using the matched groups:
foreach($show_arr as $val) {
preg_match('/[^.]+\.S([\d]+)E([0-9]+[^.]+).*/', $val, $m);
$result['Season'][(int)$m[1]][(int)$m[2]] = array('episode' => $m[2],
'file' => $m[0]);
}
[^.]+ is 1 or more NOT dot . characters
\.S([\d]+) is a dot . then S followed by 1 or more digits (capture as group 1)
E([0-9]+[^.]+) is E followed by 1 or more digits followed by 1 or more NOT dot . characters (capture as group 2)
Additionally, this indexes the subarray by the episode. If you don't want that, remove the [(int)$m[2]] and just use [].
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In a text, is it possible to find the most repeated word group with PHP ?
For example, my text is "lkjlk Star Wars jkjhyyyg h Star Wars jkhk Star Wars"
and the result will be Star Wars.
Thanks for your reply.
$string = "lkjlk Star Wars jkjhyyyg h Star Wars jkhk Star Wars";
$words = array_count_values(explode(' ', $string));
arsort($words);
var_dump($words);
that code produces the following array.
array (size=6)
'Star' => int 3
'Wars' => int 3
'jkhk' => int 1
'h' => int 1
'jkjhyyyg' => int 1
'lkjlk' => int 1
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
So, I am trying to parse a string,eg. 1-5-2014, to a DATE type like so:
$todayArray = date_parse_from_format("n-j-Y", $row["whenn"]);
$news_date = strtotime($todayArray['year'].'-'.$todayArray['month'].'-'.$todayArray['day']);
But it returns as 1399909000. It seems like it is the array that is causing the problem but with my limited knowledge of php I have no idea as to what is wrong.
Thank you in advance,
Tyler
The value of $todayArray should be something like this:
Array
(
[year] => 2014
[month] => 1
[day] => 5
[hour] =>
[minute] =>
[second] =>
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)
Now the string
$todayArray['year'].'-'.$todayArray['month'].'-'.$todayArray['day']
i.e. "2014-1-5" is a valid argument for strtotime. But the function will return 1393355280, the UNIX timestamp corresponding to the given date, and that is not what you’re trying to obtain. In fact, the above string is already quite similar to your desired output. The only problem is that the values of $todayArray['month'] and $todayArray['day'] need to be zero-padded on the left side. This can be easily achieved with sprintf:
$news_date = $todayArray['year'].'-'.sprintf("%02d", $todayArray['month']).'-'.sprintf("%02d", $todayArray['day']);
Now the value of $news_date is "2014-01-05".
You don't want to use strtotime() as it just converts your date into a Unix Timestamp. Using DateTime() also makes this simpler than using date_parse_from_format():
$date = DateTime::createFromFormat("n-j-Y", $row["whenn"]);
echo $date->format('Y-m-d');