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')");
Related
I have the following question, let's say I'm querying MySQL database to get the following array:
Array ( [0] => Array ( [topicid] => 4 ) [1] => Array ( [topicid] => 5 ) [2] => Array ( [topicid] => 6 ) )
These are the results from that I get from the following SQL request
$sql=$db->query("SELECT topicid from opentopics WHERE userid=?","$userid")->fetchAll();
This query gives me the list of topics that are opened to certain user ids. Now there are several topics with IDs up to 3 that are open to everyone, so I want to use them also on my webpage. These topics are not opened in the opentopics table, but are made available to everyone based on their ID (less than 3).
So the question is following, how do I modify the multidimensional (i assume) array I get from MySQL request, how to add 3 new entries to the beghinning of the above mentioned array so it looks like:
Array ( [0] => Array ( [topicid] => 1 ) [1] => Array ( [topicid] => 2 ) [2] => Array ( [topicid] => 3 ) [3] => Array ( [topicid] => 4 ) [4] => Array ( [topicid] => 5 ) [5] => Array ( [topicid] => 6 ) )
As you may have notices in my text editory I have manually edited the text of the array, by adding this
[0] => Array ( [topicid] => 1 ) [1] => Array ( [topicid] => 2 ) [2] => Array ( [topicid] => 3 )
you can fetch your result like this example then use array_merge
$mysqli = new mysqli('localhost', 'root', '', 'test');
$manual = array(
[ 'id' => 9 , 'first_name' => 'John9' , 'last_name' => 'Doe9'] ,
[ 'id' => 19 , 'first_name' => 'John19' , 'last_name' => 'Doe19']
);
$sql = " SELECT * from users ";
$result = $mysqli -> query($sql);
$rows = $result -> fetch_all(MYSQLI_ASSOC);
$results = array_merge($manual , $rows);
the results will be like this
Array
(
[id] => 9
[first_name] => John9
[last_name] => Doe9
)
Array
(
[id] => 19
[first_name] => John19
[last_name] => Doe19
)
Array
(
[id] => 1
[first_name] => John
[last_name] => Doe
)
Array
(
[id] => 2
[first_name] => John3
[last_name] => Doe
)
Array
(
[id] => 4
[first_name] => John4
[last_name] => Doe
)
Here is my Sample table:
Here is My Query:
SELECT * FROM table
Result for above query is:
Array
(
[0] => Array
(
[id] => 1
[name] => a
[country] => x
)
[1] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
I need to get the field names in the first element of the array.
Expected result array is:
Array
(
[0] => Array
(
[id] => id
[name] => name
[country] => country
)
[1] => Array
(
[id] => 1
[name] => a
[country] => x
)
[2] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
How can I modify the Query to get this result?
Thanks in advance...
Display issues should generally be dealt with in application code, not SQL queries. If you have your values in an array (called $results, say), you can use this code to add the desired entry:
array_unshift($results, array_combine(array_keys($results[0]), array_keys($results[0])));
print_r($results);
Output:
Array
(
[0] => Array
(
[id] => id
[name] => name
[country] => country
)
[1] => Array
(
[id] => 1
[name] => a
[country] => x
)
[2] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
Demo on 3v4l.org
SELECT 'id' id, 'name' name, 'country' country
UNION ALL
SELECT id, name, country FROM table
ORDER BY 'id' != id
One way to do it with array_keys, array_combine and array_unshift. If I were you I will get the SQL result as $array variable and do some processing on php side like below on query side.
<?php
$array = [['id'=>1,'name'=>'a','country'=>'x'],['id'=>1,'name'=>'b','country'=>'y']];
$keys = array_keys($array[0]);
$first = array_combine($keys,$keys);
array_unshift($array,$first);
print_r($array);
?>
WORKING DEMO: https://3v4l.org/IqZVk
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.
I have this request to elastic service.
Url callback is:
GET
/es-main/Project,ProjectStage,Task,TaskComment,TicketComment,Client,User,Attachment/_search
How to add Sort improvement requirements that the results were in sequence as they are embedded in the URL + score? Now alphabetically by type. Example first is type Project, second is ProjectStage, etc...
Thanks all for answers...
GET /es-main/Project,ProjectStage,Task,TaskComment,TicketComment,Client,User,Attachment/_search
Array
(
[query] => Array
(
[query_string] => Array
(
[query] => *a*
[analyzer] => hunspell_cs
[fields] => Array
(
[0] => name
[1] => description
[2] => tags.name
[3] => text
[4] => email
[5] => filename
)
)
)
[from] => 0
[size] => 10
[sort] => Array
(
[_type] => ASC
[_score] => DESC
)
[aggs] => Array
(
[CountByType] => Array
(
[terms] => Array
(
[field] => _type
)
)
)
)
Okey, i have added in document _is_Project = 1, _is_ProjectStage = ,_is_Task = 1, etc...
and set ordering to:
order: {
'_is_Project': {'unmapped_type': 'long'}, // first
'_is_ProjectStage': {'unmapped_type': 'long'}, // second
'_is_Task': {'unmapped_type': 'long'}, // third - etc...
'_score': 'DESC'
}
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