I'm new to php and would like to know how to make the following possible ...
Below I have several arrays populated from Database rows. Id like to have a new array mappingId that concatenates several rows to form the value for the mappingId array. Below I have used array_combine but this doesn't seem to work. Can anyone advise on what to use?
$appId = array();
$appDate = array();
$appTime = array();
$appDoctorId = array();
$mappingId = array(); // combined values
for ($i = 0; $i < mysqli_num_rows($resultAppointmentsBooked); $i++)
{
$row = mysqli_fetch_row($resultAppointmentsBooked);
$appId = $row[0];
$ids[] = $row[0];
$appTime[] = $row[3];
$appDate[] = $row[4];
$appDoctorId[] = $row[2];
$mappingId = array_combine($row[4], $row[3], $row[2]);
//Test output
echo "MappingId: $row[4]$row[3]$row[2] <br />";
echo "MappingId2: - $mappingId[$i] <br />";
}
I have also used 'array_merge' the following way ...
$ids[] = $row[0];
$appTime[] = $row[3];
$appDate[] = $row[4];
$appDoctorId[] = $row[2];
$mappingId = array_merge($appDate, $appTime, $appDoctorId);
but when I print the values ...
echo "MappingId2: $mappingId[$i] <br />";
It only output the value of the first array.
Try something like this. If this is what you need. Here in the below codes
$mappingId contains array of individual appDate, appTime, appDoctorId in array format. If you don't want array just want concatenate those value with one element then also you can do that. Please let me know!
$mappingId = array(); // combined values
for ($i = 0; $i < mysqli_num_rows($resultAppointmentsBooked); $i++)
{
$row = mysqli_fetch_row($resultAppointmentsBooked);
$appId = $row[0];
$appTime = $row[3];
$appDate = $row[4];
$appDoctorId = $row[2];
$myArr = array();
//array_push($myArr, $appDate, $appTime, $appDoctorId);
// If you want in concatenate format
array_push($myArr, $appDate."".$appTime."".$appDoctorId);
array_push($mappingId, $myArr);
}
You can add values to an array by doing this:
$mappingId[] = 'Hello';
$mappingId[] = 'There';
$mappingId[] = 'Friend';
Results in:
$mappingId = array('Hello', 'There', 'Friend');
--
You can also use array_merge.
This merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
I think what you're looking for is array_merge
array_merge(array(1), array(2,3)); //return array(1,2,3)
try something like this , array_merge should work:
$ids = $row[0];
$appTime = $row[3];
$appDate = $row[4];
$appDoctorId = $row[2];
$mappingId = array_merge($ids, $appTime, $appDate, $appDoctorId);
echo "MappingId: $row[4]$row[3]$row[2] <br />";
echo "MappingId2: - $mappingId <br />";
Related
I have an array with 3 values(56.767, 360.997, 579.728). These are in an array($distance).
Well, when I run the min($distance) I get '360.997'. What gives?
<?php
include('mysql_connect.php');
$MasterState = 'CA';
$query = 'select * from estes_term where Dest_State = "'.$MasterState.'"';
$result = mysql_query($query);
if($result) {
$row = #mysqli_fetch_row($result);
}
$Term_Zip = array();
$Distance = array();
$i = '0';
while ($row = mysql_fetch_array($result, MYSQLI_ASSOC)) {
$Term_Zip[] = $row['Term_Zip'];
$Distance_xml = file_get_contents('http://zipcodedistanceapi.redline13.com/rest/ua6z0ep0djB3zHGz5Z40hONMVc8yuXgY8nx8BX8OhKSRrzqxzvyRjmDeyMM3J32S/distance.xml/90077/'.$Term_Zip[$i].'/mile');
$Distance[] = $Distance_xml;
$i++;
}
echo '<pre>';
var_dump($Term_Zip);
var_dump($Distance).'<br />';
$test1 = min($Distance);
$test = (array_keys($Distance, min($Distance)));
echo '<br />';
echo 'Min'.min($Distance);
?>
As #Rocket pointed out, your variables are stored as strings, not floats. This, the character "3" is smaller than "5", so it's the first one. To avoid this use Type Juggling or floatval() in your code to assure your vars are float as:
$Distance[] = floatval(trim($Distance_xml));
Do like this... We are converting all the array elements to float using array_map and then finding the min value from it.
<?php
$arr= array(56.767, 360.997, 579.728);
echo min(array_map('floatval',$arr));
OUTPUT :
56.767
EDIT :
To get the key , you can make use of array_search()
$key = array_search(min(array_map('floatval',$arr)), $arr);
Hi all Im trying to query a database and store the results ($searchResults[]) as an array :
<?php
if(isset($_POST['indexSearchSubmit']))
{
foreach($_POST['industryList'] as $selected)
{
$_POST['industryList'] = $selected;
$locationListResults = $_POST['locationList'];
$results = mysqli_query($con,"SELECT * FROM currentListings
WHERE location = '$locationListResults' AND industry = '$selected'");
$searchResults = array();
while($row = mysqli_fetch_array($results))
{
$searchResults[] = $row['industry'];
$searchResults[] = $row['location'];
$searchResults[] = $row['title'];
$searchResults[] = $row['description'];
}
}
mysqli_close($con);
}
?>
the problem im getting is when I try to echo the result:
<?php
echo $searchResults[0];
?>
its only bringing back 1 result not displaying all the results in the arrray as i want it to.
Could anybody please point out what it is im doing wrong.
Any help would be greatly appreciated
Do like this
<?php
print_r($searchResults); // Prints all array elements
?>
Alternatively, you can make use of a for loop to echo all elements too..
foreach($searchResults as $k=>$v)
{
echo $v;
echo "<br>";
}
Your code puts your data into 1D array. You probably want sth else so instead of this:
$searchResults[] = $row['industry'];
$searchResults[] = $row['location'];
$searchResults[] = $row['title'];
$searchResults[] = $row['description'];
do this:
$tmp = array();
$tmp['industry'] = $row['industry'];
$tmp['location'] = $row['location'];
$tmp['title'] = $row['title'];
$tmp['description'] = $row['description'];
$searchResults[] = $tmp;
or just this (thanks to Barmar):
$searchResults[] = $row;
This way you store your data as 2D array. So every row you obtain remains in one subarray.
To print the row (which is now in 2D array) iterate over subarrayw:
foreach($one_of_searchResult_rows as $k => $v)
{
// do whatever you need with $k and $v
}
I think you want a 2d array. Try this code snippet :
$searchResults = array();
while($row = mysqli_fetch_array($results))
{
array_push($searchResults,$row);
}
This should push each row as an associative array in every cell of your final searchResuts array. You could then access the values like:
echo $searchResults[0]['industry']; //and so on
echo $searchResults[0]; //Should print out the first match/row of the sql result
Lets make it simple :
$serach_result=mysqli_fetch_all ($result,MYSQLI_NUM);
//you should use print_r while trying to print an array
print_r($search_result[0]);
Reference : mysqli_fetch_all
I want to make implode to display only 5 arrays after that it automatic creates a new row displaying 5 arrays again and it keep repeating itself. like is it possible to use <br /> or something else I had to use to do that?
$result = mysql_query("SELECT * FROM `im_album` WHERE username = '".$user_data['username']."' ");
$types = array();
$d_array = array();
while(($row = mysql_fetch_assoc($result))) {
$types[] = $row['name'];
$d_array[] = $row['description'];
}
echo "<h1>".implode($types )."</h1>"
Try this one:
$result = mysql_query("SELECT * FROM `im_album` WHERE username = '".$user_data['username']."' ");
$types = array();
$d_array = array();
$types_str="";
$types_str_array=array();
$temp=1;
while(($row = mysql_fetch_assoc($result))) {
$types_str .=$row['name'];
if($temp!=1 && $temp%5==0)
{
$types_str_array[]=$types_str;
$types_str="";
}
$types[] = $row['name'];
$d_array[] = $row['description'];
$temp++;
}
echo "<h1>".implode("<br />",$types_str_array )."</h1>";
You could do something like this:
<?php
//sample array
$types = array("John", "Doe", "Bar", "Baz", "Stock", "Overflow", "Meta" );
//Count the number of elements in $types
$types_count = count($types);
//Use $foo1, $foo2, $foo3 as the output array names
$out_types_count = 1;
$out_types_arr = "foo".$out_types_count;
for($i=1;$i<=$types_count;$i++){
$out_types_arr[] = $types[$i];
if(($i%5) == 0){ // if we $i == 5,10,15 etc,
$out_types_count++; // use $foo2
$out_types_arr = "foo".$out_types_count;
}
}// for loop ENDS
//Echo all the output
for($i=1;$i<=$out_types_count;$i++){
for($j=0;$j<=5;$j++){
echo $out_types_arr.$i[$j];
}
echo "<br />".PHP_EOL;
}
?>
P.S. Code has some errors & minor not-desired output. Because OP has got the answer he wanted, so I am gonna correct it later, from home.
I have a while loop that loops through 3 results and echo's these out in a list. It will always be 3 results.
Here is my current PHP:
while($row = sqlsrv_fetch_array($res))
{
echo "<li>".$row['SessionValue']."</li>";
// prefer to store each value in its own variable
}
However I'd like to store the $row['SessionValue'] value in each loop in a new variable.
So....
first loop: $i0 = $row['SessionValue'];
second loop: $i1 = $row['SessionValue'];
third loop: $i2 = $row['SessionValue'];
How would I achieve this with PHP?
Many thanks for any pointers.
$lst_count = array();
while($row = sqlsrv_fetch_array($res))
$lst_count[] = $row["SessionValue"];
You just need another variable that gets incremented:
$count = 0;
while($row = sqlsrv_fetch_array($res))
{
${i.$count++} = $row['SessionValue'];
}
You can do this have SUM of all value:
$total = array();
while($row = sqlsrv_fetch_array($res))
{
$total[] = $row["SessionValue"]
} $sumAll = array_sum($total);
while($info5 = mysql_fetch_array($result)){
$namelist[] = $info5["name"];
$idlist[] = $info5["id"]
}
I want an array which has the entries of the array idlist as it's index and entries of the array namelist as it's values.
Is there a short way to do this?
Like this, if I understand your request. Use $info['id'] as the array key to the accumulating array $namelist (or whatever you decide to call it)
while($info5 = mysql_fetch_array($result)){
$namelist[$info['id']] = $info5["name"];
}
i'm not sure i understand your question but probably something like this should be fine.
while($info5 = mysql_fetch_array($result)){
$values[$info5['id']] = $info5;
}
$result = array();
while($info5 = mysql_fetch_array($result))
{
$id = $info5['id'];
$name = $info5['name'];
$result[$id] = $name;
}
This should give the output array $result you want, if I understood correctly.
You can use array_combine as long as the arrays have the same number of values:
$result = false;
if (count($idlist) == count($namelist))
$result = array_combine($idlist, $namelist);
Check out the docs: http://www.php.net/manual/en/function.array-combine.php
But, I also wonder why you don't just do it in the while loop:
$values = array();
$namelist = array();
$idlist = array();
while($info5 = mysql_fetch_array($result)){
$namelist[] = $info5["name"];
$idlist[] = $info5["id"]
// this is the combined array you want?
$values[$info5["id"]] = $info5["name"];
}