Assalam-o-Alaikuim!
Dear friends today we will learn about PHP arrays. We often need to select records from database. There are different methods to select data from Database. But today we will learn something difficult.
Problem:
Suppose you have following string data that you want to convert into an array to perform action on it:
$string = “one,two,three,four”;
and you need to convert it into:
$string_array = array(“one”,”two”,”three”,”four”);
Solution
You need to write a function with PHP’s explode function for easy job.
How?
Write following function
1 2 3 4 5 6 7 8 9 10 11 12 |
function comma_separated_to_array($string_value, $separator = ',') { //Explode on comma $vals = explode($separator, $string_value); $count = count($vals); $val = array(); //Trim whitespace for($i=0;$i<=$count-1;$i++) { $val[] .= $vals[$i]; } return $val; } |
Explanation:
You wrote a function where you used explode function and then with the use of count function and for loop you add all exploded values into an array and then return it. Now you can use this function any where, but remember you have to pass minimum 1 parameter to function which is $string value. 2nd parameter is optional.
Hope you enjoyed the tutorials. See you in the next tutorial.
Hello Muhammad!
I am trying to explode array names instead of array id’s with INNER JOIN function. However, system is taking only id’s. Would you please assist? Thank you!
<?php $query=mysqli_query($con,"select size.id, size.sizeName, products.productSize from size INNER JOIN products ON size.id=products.productSize WHERE products.id='$pid'");
while($res = mysqli_fetch_array($query)) {
$size=$res['productSize'];
$boom = explode(",", $size);
foreach ($boom as $res){
echo '’.$res[‘sizeName’].”;
}} ?>
Hi Lena Please try this:
///Change this to
foreach ($boom as $res){
echo ‘’.$res[‘sizeName’].”;
}
foreach ($boom as $key=>$value){
echo $key;
echo $value;
}
Hope it will solve your problem. If not please tell me. I am in hurry so can’t test code at the time. Thanks for the comment.
Hello Muhammad! I am trying to explode array names instead if array id’s with INNER JOIN function, but only id’s are shown. Would you please assist? Thank you!
<?php $query=mysqli_query($con,"select size.id, size.sizeName, products.productSize from size INNER JOIN products ON size.id=products.productSize WHERE products.id='$pid'");
while($res = mysqli_fetch_array($query)) {
$size=$res['productSize'];
$boom = explode(",", $size);
foreach ($boom as $row){
echo '’.$row[‘sizeName’].”;
}} ?>
Hi Lena Please try this:
///Change this to
foreach ($boom as $res){
echo ‘’.$res[‘sizeName’].”;
}
foreach ($boom as $key=>$value){
echo $key;
echo $value;
}
Hope it will solve your problem. If not please tell me. I am in hurry so can’t test code at the time. Thanks for the comment.
hai muhammad
i am new in php. i have following inserted values in mysql table like following.
id invoiceno indate statemiles
310 IN/10-11/004 02-15-2020 MI,108.53
OH,194.57
PA,188.22
WA,238.57
308 IN/10-11/001 02-05-2020 IA,59.62
MI,170.99
IN,152.14
OH,241.14
PA,188.22
WA,238.57
I want to display results by date filter and output like following. Is this possible?
Awaiting for your reply. Thanks
STATE MILES
MI 279.52
OH 435.71
PA 376.44
WA 477.14
IA 59.62
IN 152.14
Total 1780.57
Can you please show me your MySql table code? I mean complete structure with some data then I’ll be able to help you.