I have an sql query that returns the altitude of a place. I have exploded the result to separate longitude and latitude. The result is stored in array.
Now I want to create an array which should contain all the arrays returned by the explode function.
$sql_altitude = mysql_query("SELECT altitude FROM `navigatio_info`
WHERE bus_id='$bus_id'
AND driver_id ='$driver_id'
ORDER BY stop_no ASC
LIMIT 0 , 30");
while ($row = mysql_fetch_assoc($sql_altitude))
{
//echo $row['altitude'];
//$altitude=array();
$altitude=(explode("-",$row['altitude']));
print_r($altitude);
//$lat=array();
$lat=$altitude[0];
//print_r($lat);
echo '<br/>';
//$long=array();
$long=$altitude[1];
//print_r($long);
//echo '<br/>';
}
below is a static array defined:
<?php
$phpArray = array(array('Vadodara',22.3000,73.2000,5),
array('Valsad',20.6300,72.9300,2),
array('Thane',19.1724,72.9570,1));
)?>
I want $phpArray to have dynamic values generated from the query above
Please use mysqli_* functions since mysql_* functions are old now.
$phpArray = array();
while ($row = mysql_fetch_assoc($sql_altitude))
{
/*
I assume $row['altitude'] contains something like below string
$row['altitude'] = "place-latitude-longitute-altitude"
*/
$phpArray[] = explode("-",$row['altitude']);
}
print_r($phpArray);
$sql_altitude = mysql_query("SELECT altitude FROM `navigatio_info` WHERE bus_id='$bus_id'AND driver_id ='$driver_id' ORDER BY stop_no ASC LIMIT 0 , 30");
$phparray = array();
while ($row = mysql_fetch_assoc($sql_altitude))
{
$altitude=(explode("-",$row['altitude']));
$lat=$altitude[0];
$long=$altitude[1];
$phparray = array($lat,$lat);
}
echo "<pre>";
print_r($phparray)
echo "<pre>";
Please check above code:
as i understand your requirement use the following code.
$phpArray = array();
while ($row = mysql_fetch_assoc($sql_altitude)) {
$altitude=(explode("-",$row['altitude']));
$phpArray[]= $altitude;
}
print"<pre>";
print_r($phpArray);
print"</pre>";
above code will generate following array.
Array
(
[0] => Array
(
[0] => Vadodara
[1] => 22.3000
[2] => 73.2000
[3] => 5
)
[1] => Array
(
[0] => Valsad
[1] => 20.6300
[2] => 72.9300
[3] => 2
)
[2] => Array
(
[0] => Thane
[1] => 19.1724
[2] => 72.9570
[3] => 1
)
)
Hope this helps.
Related
I really need your help! How can I do to group the array results and assemble select.
Database
Image Database
PHP
<?php
$rsPA = $mysqli->query("SELECT * FROM provas_agendadas WHERE status = 'A' ");
foreach ($rsPA as $key => $rsRowPA){
$dis1[] = explode("," , $rsRowPA['disciplinas']);
}
echo '<pre>';print_r($dis1);echo '</pre>';
?>
Result:
Array
(
[0] => Array
(
[0] => EJA-1
)
[1] => Array
(
[0] => EJA-1
[1] => EJA-5
[2] => TTI-1
)
)
Expilando: From the result of the array, I will only get the number after the -, Ex: EJA-1, I only need 1, which is the ID of the discipline table.
End result you would like
Image select
I thank everyone who can help me.
You could perform an additional for loop, and explode each element on - and take the last part:
<?php
$rsPA = $mysqli->query("SELECT * FROM provas_agendadas WHERE status = 'A' ");
foreach ($rsPA as $key => $rsRowPA){
$temp = explode("," , $rsRowPA['disciplinas']);
foreach($temp as $elem){
$number = explode('-',$elem);
$number = end($number);
$numbers[] = $number;
}
}
$dis1 = array_count_values($numbers);
echo '<pre>';print_r($dis1);echo '</pre>';
?>
I need the array as (2) from a single query
can anyone help ?
1. Array
(
[0] => Array
(
[crop_id] => 3
[crop_name] => Barley
)
2. Array
(
[0] => Array
(
[Barley] => 3
)
)
Don't know about query but you can do that simply using array_map()
$array[] = array('crop_id' => 3, 'crop_name' => "Barley");
$result = array_map("myfunction",$array);
print_r($result);
function myfunction($v)
{
$data = [];
$data[$v['crop_name']] = $v['crop_id'];
return $data;
}
Let's suppose you have stored your data in array named as crop_data like
$crop_data[0][crop_id]=3;
$crop_data[0][crop_name]='Barley';
....
$crop_data[n][crop_id]=187;
$crop_data[n][crop_name]='Wheat'
Try this code:
$new_crop_result=array()
foreach($crop_data as $key=>$record)
{
$new_crop_result[$key][$record[crop_name]]=$record[crop_id];
}
I am having some issues with the below, wanting to add the query result "id" as the key to the value "concat" -- Do I need a foreach for this? What is the best way?
$concatCol = mysqli_query( $connS, "SELECT id, ShipmentNumber, InvoiceNumber, BillofLading from testTable");
$data =array();
while ($row = mysqli_fetch_array($concatCol)) {
$id = $row["id"];
$ShipmentNumber = $row["ShipmentNumber"];
$InvoiceNumber = $row["InvoiceNumber"];
$BillofLading = $row["BillofLading"];
$concat = $ShipmentNumber.$InvoiceNumber.$BillofLading;
echo $concat."<br>";
$data[] = $concat;
}
I'm not really sure what your expected output is. But I think one of the following will work for you:
$data[]['id'] = $concat;
which will result in an array like this:
Array
(
[0] => Array
(
[id] => ShipmentNumberInvoiceNumberBillofLanding
)
[1] => Array
(
[id] => ShipmentNumberInvoiceNumberBillofLanding
)
)
The second option would be:
$data[$id] = $concat;
Which will yield a result like:
Array
(
[1] => ShipmentNumberInvoiceNumberBillofLanding
[4] => ShipmentNumberInvoiceNumberBillofLanding
)
Where the keys 1 and 4 are the $id for each returned row. In both example the ShipmentNumberInvoiceNumberBillofLanding would of course be the concatenated values for each row.
Fetch all rows based on the query into an array and return single value
Query database for data
//----------------------------------------------------------------------
$result = mysql_query("SELECT * FROM $tableName"); query
$array = mysql_fetch_row($result);
fetch result In Array
$arr = array();
while ($row = mysql_fetch_assoc($result)) {
$arr2 = array();
foreach ($row as $val) $arr2[] = $val;
$arr[] = $arr2;
}
Result Will Be
Array
(
[0] => Array
(
[0] => status_site
[1] => 0
)
[1] => Array
(
[0] => title_site
[1] => Script
)
[2] => Array
(
[0] => keys_site
[1] =>
)
)
I need to make function that return element 0 to 1
ex: function getsetting (title_site){
return value script}
Replace the $arr[] = $arr2; with $arr[$arr2[0]] = $arr2[1];. That might solve your problem.
I'm having trouble fetching data from my database and sorting it in an array.
Here is my current code:
I'm looping through each engine to create a sorted array with each servers ID.
for($i=0; $i <= $servers_default_engines_count; $i++){
//get all servers with this engine type, and get how many
$query = $db->query("SELECT `id` FROM `cgshop_servers` WHERE `server_engine` = $i");
$server_id = mysql_fetch_row($query);
$server[$i] = array();
array_push($server[$i], $server_id);
};
Result with mysql_fetch_row:
[0] => Array
(
[0] => Array
(
[0] => 2
)
)
[1] => Array
(
[0] => Array
(
[0] => 1
)
)
)
also:
[0] => Array
(
)
[1] => Array
(
)
[2] => Array
(
[0] => 1
)
But i'm looking to return it like this:
() placed for explanation reasons.
Array(
[0] (engine1) => Array(
[0](server1) => 2 (serverid)
)
[1(engine2)] => Array(
[0](server1) => 1 (serverid)
[1](server2) => 3 (serverid)
)
)
This is the current data in the mysql tables:
cgshop_servers:
id server_name server_ip serevr_port server_engine source_engine
1 test1 195.62.14.65 15464 1 1
2 test2 195.62.14.63 15464 0 0
3 test3 195.64.14.62 15465 1 1
Have you tried using mysql_fetch_assoc (information)
mysql_fetch_assoc — Fetch a result row as an associative array
Example Usage
$mysql_query = mysql_query("SELECT * FROM `cgshop_servers` WHERE `server_engine` = ?", $i);
$my_array = mysql_fetch_assoc($mysql_query);
print_r($my_array);
you are fetching it wrongly, since you said you are using prepared statement, below is the updated code, you can use.
$server = array();
for($i=0; $i <= $servers_default_engines_count; $i++){
//get all servers with this engine type, and get how many
$sth = $db->prepare("SELECT `id` FROM `cgshop_servers` WHERE `server_engine` = ?");
$sth->bind_param('i', $i);
$sth->execute();
$server[$i] = $sth->fetch();
};
or better of you can fetch it with one single query without any loop like this.
$totalServerCounts = range(0, $servers_default_engines_count);
$sth = $db->query("SELECT `id` FROM `cgshop_servers` WHERE `server_engine` IN(".implode(', ', $totalServerCounts).")");
$servers = $sth->fetch_all();