php $json google maps parsing - json

I have the following code:
<?php
$q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=35.0078,-97.0929&destinations=45.2078,-97.0929&mode=driving&sensor=false";
$json = file_get_contents($q);
$details = json_decode($json, TRUE);
echo "<pre>"; print_r($details); echo "</pre>";
?>
The result is:
Array
(
[destination_addresses] => Array
(
[0] => 150th St, Summit, SD 57266, USA
)
[origin_addresses] => Array
(
[0] => 38591 Patterson Rd, Wanette, OK 74878, USA
)
[rows] => Array
(
[0] => Array
(
[elements] => Array
(
[0] => Array
(
[distance] => Array
(
[text] => 1,299 km
[value] => 1299343
)
[duration] => Array
(
[text] => 13 hours 26 mins
[value] => 48361
)
[status] => OK
)
)
)
)
[status] => OK
)
I need to extract from $json only the distance (1,299 km).
I am not sure how to do this.
Any help is very much appreciated.

<?php $q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=35.0078,-97.0929&destinations=45.2078,-97.0929&mode=driving&sensor=false";
$json = file_get_contents($q);
$details = json_decode($json);
$distance=$details->rows[0]->elements[0]->distance->text;
echo $distance; ?>

Related

fetch data from array and save it in database

I had this code
echo "<pre>";
print_r($data);
echo "</pre>";
through which i got the following data
Spreadsheet_Excel_Reader Object
(
[sheets] => Array
(
[0] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 2
[numCols] => 5
[cells] => Array
(
[1] => Array
(
[1] => sno
[2] => candidta name
[3] => email
[4] => ctc
[5] => location
)
)
)
)
)
I needed numCols so i used the following code
echo $data->sheets[0]['numCols'];
and got the result 5
Now i wish to fetch the following values
[1] => sno
[2] => candidta name
[3] => email
[4] => ctc
[5] => location
and store them in the table like this
id value
1 sno
2 candidta name
3 email
4 ctc
5 location
Can anyone please tell how it can be done
$Data = array();
foreach($data->sheets[0]['cells']['1'] as $key=>$value):
$Data[] = "('".$key."','".$value."')";
endforeach;
mysql_query("INSERT INTO tbl_name(`id`,`value`) VALUES".implode(",",$Data)) or die (mysql_error());
$newarray = $data->sheets[0]['cells'][1];
for ( $i=1; $i < count($newarray); $i++)
{
echo $newarray[$i];
$sql= "INSERT INTO `tablename` (value) VALUES ('$newarray[$i]')";
if(!mysqli_query($con,$sql))
{
echo "Error: " . $sql. "<br>" . mysqli_error($con);
}
}

How to get the first row of a result in Yii2? (queryRow Equivalent)

how can i get the first row of result? Below is my code which is giving me an error like this Undefined index: module
if ( substr( $action, 0, 4 ) === "stl_" )
{
$query = "SELECT * FROM a_actions LEFT JOIN a_modules ON ( a_modules.id=a_actions.module_id )
WHERE a_actions.id=(SELECT dependency FROM a_actions WHERE action='{$action}') AND a_modules.module_status = 1 ";
$action = \Yii::$app->db->createCommand( $query )
->queryAll();
//print_r($action);die();
$module = $action[ 'module' ];
$action = $action[ 'action' ];
}
$action has value
Array ( [0] => Array ( [id] => 7 [module_id] => 7 [action] => index [label] => Members [dependency] => [created_by] => [created_at] => [updated_by] => [updated_at] => [is_deleted] => 0 [module] => members [module_name] => [module_status] => 1 ) )
in Yii1 i would have used
$action = \Yii::$app->db->createCommand( $query )
->queryRow();
You can use queryOne()
\Yii::$app->db->createCommand( $query )
->queryOne();
QueryOne()

Import categories with magmi datapump

This is an example of my category CSV.
I never used datapump and I can't find any sample code.
I use this code to create a category tree:
ini_set('display_errors', '1');
error_reporting(E_ALL);
set_time_limit(0);
$url = "www.xxxxxxxxx.com/category.csv";
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
$app = Mage::app('default');
$exit= 0;
$continua = 0;
$cont = 0;
$f = fopen('php://temp', 'w+');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FILE, $f);
curl_exec($curl);
curl_close($curl);
rewind($f);
function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['id'] === $id) {
return $val['nombre'];
}
}
return null;
}
while (($data = fgetcsv($f, 10000 , ",")) !== FALSE ) {
$fila="";
foreach($data as $row) {
$fila= $fila.$row;
}
$datos = explode(";",$fila);
// ID_CATEGORY CATEGORY_NAME Father_CATEGORY
$clave = $datos[0];
$cat = $datos[1];
$padre = $datos[2];
$categoria[$cont] = array ('id'=>$clave, 'nombre'=>$cat);
$nomPad = searchForId($padre,$categoria);
}
Example Result:
[21] => Array
(
[id] => 160
[name] => Madera de Teca
)
[22] => Array
(
[id] => 163
[name] => Polyrattan Natur
)
[23] => Array
(
[id] => 165
[name] => Polyrattan / acero / cristal
)
[24] => Array
(
[id] => 166
[name] => Polyrattan / forja / cristal
)
How can use this in magmi datapump?
Do I need to import a category creator plugin? How can I combine the sample code with my own.

Create multi dimensional array in cakephp

Now I have 3 row of data.
id name date
1 data1 12-06-2013
2 data2 12-06-2013
3 data3 16-06-2013
now I want to group the 3 rows of data into a new array, something like:
Array
(
[2013-06-12] => Array
(
[0] => data1
[1] => data2
)
[2013-06-16] => Array
(
[0] => data3
)
)
the question is 1st i using foreach to show all date out.
<?php foreach($datax as $data):
echo $data['Model_Name']['id'];
endforeach;?>
then, while i looping i want to group up the $data sort to date as the multidimensional array as above.
Try below method,. you can add an additional column for date
$a = array(
array('datax' => array('id' => 1, 'name' => 'data1')),
array('datax' => array('id' => 2, 'name' => 'data2')),
array('datax' => array('id' => 3, 'name' => 'data3'))
);
$result = Set::classicExtract($a, '{n}.datax.id');
/* $result now looks like:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
*/
$result = Set::classicExtract($a, '{n}.datax.name');
/* $result now looks like:
Array
(
[0] => data1
[1] => data2
[2] => data3
)
*/

Add JSON array to MYSQL

I'm trying to get specific value by array name:
<?php
$json = json_decode($_POST['json'], true);
print_r($json);
?>
I'm getting this varray:
Array
(
[0] => Array
(
[name] => pav
[value] => g
)
[1] => Array
(
[name] => ppav
[value] => f
)
[2] => Array
(
[name] => kiekis
[value] => g
)
[3] => Array
(
[name] => kaina
[value] => g
)
[4] => Array
(
[name] => ppav
[value] => f
)
[5] => Array
(
[name] => kiekis
[value] => g
)
[6] => Array
(
[name] => kaina
[value] => f
)
[7] => Array
(
[name] => ppav
[value] => g
)
)
Tried using foreach function, but cant get specific value:
foreach ($json as $key => $value) {
echo "name".$key['name']." value".$value['value']."<br />";
}
It prints all array values:
name value<br />name valueasd<br />name valueasd<br />name values<br />name values<br />name values<br />name values<br />name valuea<br />name valueasd<br />name valued<br />
But I cant select specific value by name to add to nysql. How to do that?
Following is the tested code
<?php
$json_array = array(
array('name'=>'pav', 'value'=>'g'),
array('name'=>'ppav', 'value'=>'f'),
array('name'=>'kiekis', 'value'=>'g'),
array('name'=>'ppav', 'value'=>'f')
);
echo "<pre>";
print_r($json_array);
echo "</pre>";
$assoc_array = array();
for($i = 0; $i < sizeof($json_array); $i++)
{
$key = $json_array[$i]['name'];
$assoc_array[$key] = $json_array[$i]['value'];
}
echo "<pre>";
print_r($assoc_array);
echo "</pre>";
echo "assoc_array['pav'] = ".$assoc_array['pav'];
?>
output of the code is given below and you can see that exactly same array as yours is converted into associative array, there is one problem as your array has repeated names eg. ppav or kiekis so there will only 1 index for kiekis or ppav having the latest value.
you have to recreate array
$json_array = json_decode($_POST['json'], true);
$assoc_array = array();
for($i = 0; $i < sizeof($json_array); $i++)
{
$key = $json_array[$i]['name'];
$assoc_array[$key] = $json_array[$i]['value'];
}
after this you will get $assoc_array and you can access its elements by keys.