Insert auto generated multidimensional array to database - php

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.

Related

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 to Store Associative Array data in Database

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

Create a grouped array from another list of array items

I have an array of apps with ids and categories like this:
[apps] => Array
(
[0] => stdClass Object
(
[id] => 0
[categoryid] => 0
)
[1] => stdClass Object
(
[id] => 31265
[categoryid] => 12
)
[2] => stdClass Object
(
[id] => 15965
[categoryid] => 2
)
[3] => stdClass Object
(
[id] => 16554
[categoryid] => 12
)
)
I am trying to get all apps for a category based on this request. So, the resultant output for:
For CategoryId 12:
----------------
[apps] => Array
(
[0] => 31265
[1] => 16554
)
For CategoryId 2:
----------------
[apps] => Array
(
[0] => 15965
)
For CategoryId 0:
----------------
[apps] => Array
(
[0] => 0
)
I believe i need to use nested foreach loops, but is there an efficient method?
Thanks
You could cycle through them, and place them into category-arrays:
foreach ( $apps as $app ) {
$catArray[ $app[CategoryID] ][] = $app;
}
This should result in an array whose key represents a category, and whose nested arrays represent those apps in that category.
I've worked up a demo of this online at: http://codepad.org/WZXIvQ58

Recursive function for a multidimensional array?

I am attempting to use a recursive function to search through a multidimensional array, such as the one below, to find certain values, i.e. people who went to certain school, majored in a certain subject, hold a certain job title, etc. In case your wondering, this array is output from the Facebook Graph API. In reality there are more than 3 offset arrays, depending on the number of friends a user has, it could be in the thousands.
Here's a solution I tried with very little knowledge of recursive functions (my first thought was to use in_array before I found out it didn't work for md arrays):
So to give you an idea of how the md array below works, check out this snippet of code:
$friend = $fqlResult[0]['name'];
echo "$friend";
*The output would be "BLANK" since I deleted the person's name.
$data = $fqlResult;
$collegemajor = (isset($value['education'][0]['concentration'][0]['name'])) ? $value['education'][0]['concentration'][0]['name'] : null ;
$major = "Business Administration";
if (isset($collegemajor)) {
foreach($data as $key=> $value) {
if ($value($collegemajor) == $major) {
echo "User $key is majoring in $major";
}
}
}
So here is the multidimensional array referenced above. In this example, I want to pull the names of all of the user's friends who majored in Business Admin. in college. As you can see from this snippet, there aren't any (I think) but in the long version of the array, there are plenty. The code above produces no output and I'm lost as to how to make it work. Any help would be greatly appreciated.
Array
(
[0] => Array
(
[name] => BLANK
[education] =>
[work] =>
)
[1] => Array
(
[name] => BLANK
[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 108087985890571
[name] => St. Andrew's School
)
[year] => Array
(
[id] => 138383069535219
[name] => 2005
)
[type] => High School
)
[1] => Array
(
[school] => Array
(
[id] => 20697868961
[name] => Boston University
)
[concentration] => Array
(
[0] => Array
(
[id] => 108654845832522
[name] => Business Administration
)
)
[type] => College
)
[2] => Array
(
[school] => Array
(
[id] => 108289315859633
[name] => University of Miami
)
[year] => Array
(
[id] => 138879996141011
[name] => 2013
)
[type] => Graduate School
)
)
[work] => Array
(
)
)
[2] => Array
(
[name] => BLANK
[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 115444241803885
[name] => Saint Andrews High School
)
[year] => Array
(
[id] => 137616982934053
[name] => 2006
)
[type] => High School
)
[1] => Array
(
[school] => Array
(
[id] => 112033702149888
[name] => Boca Raton High
)
[year] => Array
(
[id] => 137616982934053
[name] => 2006
)
[type] => High School
)
[2] => Array
(
[school] => Array
(
[id] => 108087985890571
[name] => St. Andrew's School
)
[type] => High School
)
[3] => Array
(
[school] => Array
(
[id] => 107573562605861
[name] => Duke University
)
[concentration] => Array
(
[0] => Array
(
[id] => 104045469631213
[name] => Political science
)
)
[type] => College
)
)
[work] =>
)
[4] => Array
(
[uid] => 1234567
[name] => BOB NO ONE
[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 106039752760627
[name] => Berwick Academy
)
[year] => Array
(
[id] => 137616982934053
[name] => 2006
)
[type] => High School
)
[1] => Array
(
[school] => Array
(
[id] => 108087985890571
[name] => St. Andrew's School
)
[type] => High School
)
[2] => Array
(
[school] => Array
(
[id] => 105690226130720
[name] => Northeastern University
)
[concentration] => Array
(
[0] => Array
(
[id] => 108654845832522
[name] => Business Administration
)
)
[type] => College
[classes] => Array
(
[0] => Array
(
[id] => 189873264368867
[name] => 2011
)
)
)
)
There's really no need for recursion for something like this, considering the depth of the tree is always fixed and the structure is known. Using some nested loops would do the trick:
$friends = $fqlResult;
$friends_BA = array();
foreach ($friends as $friend) {
if (is_array($friend['education'])) {
foreach ($friend['education'] as $school) {
if (isset($school['concentration'])) {
foreach ($school['concentration'] as $concentration) {
if (strpos(strtolower($concentration['name']), 'business') !== false) {
$friends_BA[] = $friend;
continue 3; // skip to the next friend
}
}
}
}
}
}
var_dump($friends_BA);
You want a function that will find a specific value on a specific field in the array,
function arraySearch($key, $value, $array){
$flag = FALSE;
foreach($array as $result){
if(arraySearch($key, $value, $result)){
$flag = TRUE
}elseif(isset($result[$key] && $result[$key] == $value){
$flag = TRUE;
}
}
return $flag
}
to improve performance rather than setting $flag to true, you could return true as it will stop the execution of the function and prevent it from continuing to search the array.
Call it like so
foreach($fqlResult as $result){
if(arraySearch('concentration', 'Business Administration', $result)){
//You have found a user you are looking for, echo $result['name'] or do what you want with the result.
}
}

Remove duplicates from multi-dimensional array based on higher points

I'm racking my brains trying to think of a solution. I can find plenty of solutions to remove dupes from a 2d array but I need to remove dupes where a value is lower than the other. Here is the array:
Array
(
[basketball] => Array
(
[0] => stdClass Object
(
[id] => 2
[username] => Beans
[points] => 30
)
[1] => stdClass Object
(
[id] => 314
[username] => slights
[points] => 20
)
[2] => stdClass Object
(
[id] => 123
[username] => gibb54
[points] => 5
)
)
[soccer] => Array
(
[0] => stdClass Object
(
[id] => 2
[username] => Beans
[points] => 95
)
[1] => stdClass Object
(
[id] => 49
[username] => sans
[points] => 65
)
[2] => stdClass Object
(
[id] => 122
[username] => peano
[points] => 50
)
[3] => stdClass Object
(
[id] => 174
[username] => fordb
[points] => 30
)
[4] => stdClass Object
(
[id] => 112
[username] => danc
[points] => 30
)
)
)
As you may see, user ID 2, Beans is the first selection for both basketball and soccer. As they have more points for soccer, I need to remove their entry for basketball to make ID 314, slights the 0 value.
I would need to do this continually until no user be the 0 value for any of the primary array values twice.
I've tried various combinations of foreach solutions but I'm not getting anywhere. I thought a while loop would be more suitable but I don't know what condition to test for.
Any ideas please?!
I would loop through your data and create a dictionary where the keys are the user ids, and the values are the appropriate user objects with the sport appended. Then you can reconstruct your example data array structure by looping through this de-duped array using the sport data to determine where to put each user.
To create the de-duped array, use something like:
$deDupedData = array();
foreach ($data as $sport => $users) {
foreach ($users as $user) {
if (isset($deDupedData[$user->id])) {
if ($user->points > $deDupedData[$user->id]->points) {
$deDupedData[$user->id]->sport = $sport;
$deDupedData[$user->id]->points = $user->points;
}
} else {
$modifiedUser = $user;
$modifiedUser->sport = $sport;
$deDupedData[$user->id] = $modifiedUser;
}
}
}
// Now reconstruct your array...

Categories