how to Store Associative Array data in Database - php

here is my code:
$user_detail = $client->fetch("https://app.asana.com/api/1.0/users/"
.$response['result']['data'][$i]['id'].'.json');
echo $q="INSERT INTO `asana_users`(`Name`, `Email`,`Workspaces`) VALUES ('".$user_detail['result']['data']['name']."',
'".$user_detail['result']['data']['email']."','" .implode("'),
('"$user_detail['result']['data']['workspaces'][$i])."') ";
$user_fetch = mysql_query($q);
Thing i want to insert data of
[workspaces] => Array
(
[0] => Array
(
[id] => ********
[name] => a.com
)
[1] => Array
(
[id] => **********
[name] => Personal Projects
)
here is my array fetched now i want to add index 0 contains id , name in workplace field.
secondly how to fetch data
please suggest
Array
(
[result] => Array
(
[data] => Array
(
[id] => ****
[name] => hiuh
[email] => a#d.com
[photo] =>
[workspaces] => Array
(
[0] => Array
(
[id] => 1
[name] => a.com
)
[1] => Array
(
[id] => 494
[name] => Personal Projects
)
)
)
i want like that if i want fetch workspaces name then i could fetch whole workspce
data

Related

Insert auto generated multidimensional array to database

I need to create a db function for multidimensional array. How deep the array currently dont know, bcoz they will come from xml file.
I have a sample array
Array
(
[employee] => Array
(
[0] => Array
(
[name] => Array
(
[lastname] => Kelly
[firstname] => Grace
)
[hiredate] => October 15, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Printer
[id] => 111
[price] => $111.00
)
[1] => Array
(
[product] => Laptop
[id] => 222
[price] => $989.00
)
)
)
)
[1] => Array
(
[name] => Array
(
[lastname] => Grant
[firstname] => Cary
)
[hiredate] => October 20, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Desktop
[id] => 333
[price] => $2995.00
)
[1] => Array
(
[product] => Scanner
[id] => 444
[price] => $200.00
)
)
)
)
[2] => Array
(
[name] => Array
(
[lastname] => Gable
[firstname] => Clark
)
[hiredate] => October 25, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Keyboard
[id] => 555
[price] => $129.00
)
[1] => Array
(
[product] => Mouse
[id] => 666
[price] => $25.00
)
)
)
)
)
)
I need to enter these type of array to db and then retrieve them in a good non programmer readable format
I created 2 table... 1st for array key with array level field and another for key=value
I tried this
function array_Dump($array, $d=1){
if (is_array($array)){
foreach($array as $key=>$val){
for ($i=0;$i<$d;$i++){
$level=$i;
}
if (is_array($val)){
if (is_int($key)){
array_Dump($val, $d+1);
}else{
$query = "insert into xml_array (level, input) VALUES ('$level','$key')";
insert_sql($query);
array_Dump($val, $d+1);
}
} else {
$query = "insert into xml_data (array_id,level_id, array_key,array_value) VALUES ('$insert_id','$level','$key','$val')";
insert_sql($query);
}
}
}
}
Create a table like this:
attributes(id, parent_id, properties)
where id will be the primary key, parent_id will be the id of the parent record and properties will be a small json field with the atomic properties. This way you support any depth the XML may throw towards your direction.
As about non-programmer representation. For instance you could use a table (you can solve that with divs as well) which will contain a row for each element in the top level array. Such a row would contain separate columns for each property. When a property is an array, then the given cell will be a table of its own, which will be handled similarly as the first table. It is advisable to make the inner tables collapsible, so if one wants to see the main levels only, the user will not have to scroll for a long while.

get array name from multilevel array in php

how i can get WP_Widget_Archives from array,
This is my array:
$control = Array
(
[name] => Archives
[id] => archives-6
[callback] => Array
(
[0] => WP_Widget_Archives Object
(
[id_base] => archives
[name] => Archives
[widget_options] => Array
(
[classname] => widget_archive
[description] => A monthly archive of your site’s Posts.
)
[control_options] => Array
(
[id_base] => archives
)
[number] => 8
[id] => archives-8
[updated] =>
[option_name] => widget_archives
)
[1] => form_callback
)
[params] => Array
(
[0] => Array
(
[number] => 6
)
)
[width] => 250
[height] => 200
[id_base] => archives
)
i have try with this code
`echo '<pre>'; print_r(array_keys($control['callback'])); echo '</pre>';`
but I get result like this
Array
(
[0] => 0
[1] => 1
)
where I think the result will be like this
$result = Array
(
[0] => WP_Widget_Archives Object
[1] => form_callback
)
so i can write $result[0] for get WP_Widget_Archives, please help me and thank you for your help :)
Probably you misunderstood array_key function. It will give you keys of the array not value. In your case you require value which is an object 'WP_Widget_Archives', so you can directly use $control['callback'][0].

Cakephp retrieving data from database

I'm having trouble modeling my database. Currently it looks like this (I'm hiding the irrelevant fields): http://i.imgur.com/SF9FzaD.png
It is working fine, for example, when I want it to return a list of all the servers:
$this->Server->find('all');
It returns an array with the right information:
Array
(
[0] => Array
(
[Server] => Array (...)
[User] => Array (...)
[Highlight] => Array
(
[0] => Array
(
[id] => 39
[id_server] => 8
[id_highlight] => 1
)
[1] => Array
(
[id] => 40
[id_server] => 8
[id_highlight] => 5
)
)
[SubServer] => Array(...)
)
[1] => Array
(
[Server] => Array (...)
[User] => Array (...)
[Highlight] => Array
(
[0] => Array
(
[id] => 41
[id_server] => 10
[id_highlight] => 4
)
[1] => Array
(
[id] => 42
[id_server] => 10
[id_highlight] => 5
)
)
[SubServer] => Array(...)
)
)
In short, each game "server" has some kind of higlights pointed by the user. Like "Anti-cheat system", "Active staff", "Custom events", etc. Each of these highlights have an id and a name.
Is there a way to grab the name from the table highlight_names corresponding to each of the highlights.id_hightlight (and the rest of the data, like the array above) using Model::find()?

How can I merge or search an object?

Here's my issue:
I have an object filled with arrays that look like this.
[376339] => Array
(
[0] => 1f422730-f54b-4e4d-9289-10258ce74446
[1] => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
I need to search another object to find objects with the matching IDs, like below. Is there a way of doing this without a foreach? There are SEVERAL and I would like to not have to loop over the entire object every time.
stdClass Object
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
stdClass Object
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
I need it to end up looking like this.
[376339] => Array
(
[0] => Array
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
[1] => Array
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
)
I'm not sure if this makes any sense, that's why I had my two inital outputs I need to have merged into one somehow. This is all coming from one huge json object and I'm just using json_decode($jsonStuff) to decode it.
Would this be easier if I added true in the decode function? If I could just search for it like I could in python, that would be neat. But as it is, I'm at a loss as to how to get the output I need.
Note: Input json CANNOT be changed, I have no affiliation with the people that created it.
First loop over your input array and create an array with the key as the id
$input = json_decode($json_input);
$output = array();
foreach($input as $obj){
$output[$obj->id] = $obj;
}
then you can build your other array by searching the id on the array key
$massive_search_array = array(376339 => array
(
0 => 1f422730-f54b-4e4d-9289-10258ce74446,
1 => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
);
$final_output = array();
foreach($massive_search_array as $index => $searches){
foreach($searches as $search){
if(isset($output[$search])){
$final_output[$index][] = $output[$search];
}
}
}

Inserting data into MySQL from a multidimensional array in php

An API I am querying returns the following data which I've used json_decode to put into an array. I need to extract certain values for inserting into a MySQL database. In the example below (which only has one ID), I want to insert ID = 4229850 and 2011 (the value of the field Vanguard). I'm can't figure out how to navigate through the array and generate the appropriate insert statement.
Array
(
[Contacts] => Array
(
[0] => Array
(
[Id] => 4229850 [Url] => https://abc.com
[FirstName] => Mol
[LastName] => Thompson
[FieldValues] => Array
(
[0] => Array
(
[FieldName] => Profile last updated [Value] =>
)
[1] => Array
(
[FieldName] => First name [Value] => Mol
[CustomAccessLevel] => Public
)
[2] => Array
(
[FieldName] => Last name [Value] => Thompson
)
[3] => Array
(
[FieldName] => e-Mail [Value] => abc#yahoo.ca
)
[4] => Array
(
[FieldName] => Vanguard [Value] => 2011
)
)
)
)
)
$field1=$yourArray['contacts'][0]['Id'] //Value 4229850
$field2=$yourArray['contacts'][0]['FieldValues'][4]['Value'] //Value 2011
Using PDO Methods
$sql = "INSERT INTO yourTable (field1,field2) VALUES (:field1,:field2)";
$q = $conn->prepare($sql);
$q->execute(array(':field1'=>$field1,
':field2'=>$field2));
Using mysql_query function
mysql_query("INSERT INTO yourTable (field1,field2) VALUES ('$field1','$field2')");

Categories