Storing difficult data structure in php arrays - php

I need to store this data structure in php array:
Movie has id, name and show_date. Each of show_dates have show_times. I need to dynamically in cilcle fill this array with data from my data source. When i do like this:
$Movie = array();
$Movie[0]['id']=10;
$Movie[0]['name']='Some Name';
$Movie[0]['date'][0]='12.12.12';
$Movie[0]['date'][0]['time'][0]='12:23:00'; //there it throws error
$Movie[0]['date'][0]['time'][1]='15:23:00';
Could you help me with this issuse ?

You're trying to do array access on a string.
Change to:
$Movie[0]['date'] = array();
$Movie[0]['date'][] = array( // shorthand push notation
"date" => "12.12.12",
"times" => array("12:23:00", "15:23:00")
);
// .. etc

Related

Save data in json format using php

I wanted to save the data in this format into database option field:
{"type":"type_value"}
I am getting data in post varaibles like this
$type_value = $request->input('type_value');
$type = $request->input('type');
How Can I save this in database?
I have tried this
$data['options'] = array($type,$type_value);
But by this it is saving in this format:
["Qualifiers","1"]
I even tried doing this:
$data['options'] = json_encode(array($type,$type_value));
Instead it is saving like this
"[\"Qualifiers\",\"1\"]"
how can I do this?
You just have to change your array definition. Your array considers 2 different elements i.e type and type_value. So just make your array with key value pair and you are all set.
json_encode(array($type => $type_value))
Check this :- Fiddle

mysql data insertion line by line

asking for insert multiple lines in one mysql column (with something like line break like show didn't mean inserting data in multiple rows have multiple variables contain different urls like
$var_one = "http://example.com";
$var_two = "http://example.org";
$var_two = "http://example.net";
want to store these values in one mysql column ever value should be in new line refer image describe better
asking for insert multiple lines in one mysql column (with something like line break like didn't mean inserting data in multiple rows
If you trying in php, then you can serialize the data and store as single column in DB.
While retriving data, we can unserialize and get the data as array. Find below example.
<?php
$var = array();
$var[0] = "http://example.com";
$var[1] = "http://example.org";
$var = array("1","2","3");
print_r($var); // Array ( [0] => http://example.com [1] => http://example.org )
$b=serialize($var);
echo $b; // a:2:{i:0;s:18:"http://example.com";i:1;s:18:"http://example.org";}
$c=unserialize($b);
print_r($c); // Array ( [0] => http://example.com [1] => http://example.org )

How to assign array to array variable which is store in a database

I store array structure in database table.
example
table name - example
id=1
data= array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43")
I want to get that array structure from table and assign data column to an array.
Example
while($row=mysqli_fetch_array($result))
{
$table=$row['data'];
}
I did this way.. but it's not working.
It results in:
$table[0]=>array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43")
You can save array data to database field in multiple ways.
I suggest two ways:
1) Serialized array:
you can save data using serialize() function.
e.g. $arr = array('234' => 'asfdads', 'asdf' => 'asdf2');
$toDb = addslashes(serialize($arr));
And then you can unserialize() them to get it back like:
$toDb = unserialize(stripslashes($fromDb));
2) Using json_encode() and json_decode();
e.g. $arr = array('234' => 'asfdads', 'asdf' => 'asdf2');
$toDb = json_encode($arr);
And then you can json_decode() them to get it back like:
$toDb = json_decode($fromDb)

extract JSON variables with php for email use

I'm having difficulty extracting JSON values (obtained by using a URL) into email variables. The following code works (I have removed identifiable and usable data).
$addr = "https://98905:Twmdf56sn0cb#geoip.maxmind.com/geoip/v2.1/insights/".$ipaddr;
// fetching geoip data in JSON format
$json = (file_get_contents($addr));
//parsing JSON format to object format
$obj = json_decode($json);
// assigning values to separate variables for email message
$var_country = $obj->country->names->en;
$var_city = $obj->city->names->en;
$var_city_confidence = $obj->city->confidence->en;
$var_city_geoname_id = $obj->city->geoname_id->en;
$var_location = $obj->location->accuracy_radius->en;enter code here
However one set of data is an array that looks like this (identifiable and usable data scrubbed):
"subdivisions":[{"geoname_id":5166838,"iso_code":"NY","confidence":24,"names":{"ja":"ニューヨーク州","fr":"New York","ru":"Нью-Йорк","en":"New York","zh-CN":"纽约州","de":"New York","pt-BR":"Nova Iorque","es":"Nueva York"}}]
My difficulty lies in extracting the "names" "en" part from the subdivisions multi-array. All the previous arrays are single units, this multi and I'm having difficulty drilling down to the "names" "en" part from subdivisions and getting that value into an array using the same process as above.
If you are just looking to get the en key, you can do something like this:
$objects->subdivisions[0]->names->en;
If you are wanting retrieve each set of data, loop through them like so:
foreach($objects->subdivisions[0]->names as $key => $value) {
echo $key . ' --- ' . $value . '<br />';
}
and modify to your needs.
Try with
$p=json_decode('{"subdivisions":[{"geoname_id":5166838,"iso_code":"NY","confidence":24,"names":{"ja":"ニューヨーク州","fr":"New York","ru":"Нью-Йорк","en":"New York","zh-CN":"纽约州","de":"New York","pt-BR":"Nova Iorque","es":"Nueva York"}}]}');
$enNames = array();
foreach($p->subdivisions as $subdivision){
$enNames[]=$subdivision->names->en;
}
print_r($enNames);

Multi-dimensional array in php

I would like to create a multi-dimensional array with two variables but don't know how.
This is what i have so far;
$_SESSION['name'][] = $row_subject['name'];
$_SESSION['history'][]= $_SERVER['REQUEST_URI'];
I wanted to know if this is possible?
$_SESSION['name'][] = $row_subject['name'],$_SERVER['REQUEST_URI'];
i want to get the name of a programme which is generated via a data base and also to retrieve the url. What i am actually doing once the name is retrieve, i want to make that a link which the url would be necessary.
any help would be appreciated.
Thanks
I'm not sure what you want to do but the correct notation for your second example could be
$_SESSION['name'][] = array("name" => $row_subject['name'],
"history" => $_SERVER['REQUEST_URI']);
This pushes an associative array with the keys "name" and "history" to the array $_SESSION["name"].
You could then access the entries like so:
echo $_SESSION["name"][0]["name"];
echo $_SESSION["name"][0]["history"];
if you repeat the command with different data:
$_SESSION['name'][] = array("name" => $row_subject['name'],
"history" => $_SERVER['REQUEST_URI']);
the next entry would be addressed like so:
You could then access the entries like so:
echo $_SESSION["name"][1]["name"];
echo $_SESSION["name"][1]["history"];
and so on.
$_SESSION['name'][] = array($row_subject['name'], $_SERVER['REQUEST_URI']);

Categories