Inserting data to mysql from .xml php array - php

Im trying to get some data from the LinkedIn API using OAuth 2.0. I've got the data insert for name done, but i can't seem to get the skills values. If tried to implode the data but it doesnt find any data. I know the data is recieved.
My code:
<?php
session_start();
require_once('oAuth/config.php');
require_once('oAuth/linkedinoAuth.php');
require_once('oAuth/class.linkedClass.php');
$linkedClass = new linkedClass();
# First step is to initialize with your consumer key and secret. We'll use an out-of-band oauth_callback
$linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret']);
//$linkedin->debug = true;
if (isset($_REQUEST['oauth_verifier'])){
$_SESSION['oauth_verifier'] = $_REQUEST['oauth_verifier'];
$linkedin->request_token = unserialize($_SESSION['requestToken']);
$linkedin->oauth_verifier = $_SESSION['oauth_verifier'];
$linkedin->getAccessToken($_REQUEST['oauth_verifier']);
$_SESSION['oauth_access_token'] = serialize($linkedin->access_token);
}
else{
$linkedin->request_token = unserialize($_SESSION['requestToken']);
$linkedin->oauth_verifier = $_SESSION['oauth_verifier'];
$linkedin->access_token = unserialize($_SESSION['oauth_access_token']);
}
$content1 = $linkedClass->linkedinGetUserInfo($_SESSION['requestToken'], $_SESSION['oauth_verifier'], $_SESSION['oauth_access_token']);
$xml = simplexml_load_string($content1);
$array = XML2Array($xml);
$content = array($xml->getName() => $array);
/* Info */
$firstname = $array['first-name'];
$lastname = $array['last-name'];
$email = $array['email-address'];
$headline = $array['headline'];
$picurl = $array['picture-url'];
$publicprofileurl = $array['public-profile-url'];
$summary = $array['summary'];
$numconnections = $array['num-connections'];
/* SKIILS */
$skills = $array['skills']->skill->skill->name;
function XML2Array(SimpleXMLElement $parent)
{
$array = array();
foreach ($parent as $name => $element) {
($node = & $array[$name])
&& (1 === count($node) ? $node = array($node) : 1)
&& $node = & $node[];
$node = $element->count() ? XML2Array($element) : trim($element);
}
return $array;
}
?>
The arrays:
Array
(
[person] => Array
(
[id] => ########
[first-name] => ########
[last-name] => ########
[languages] => Array
[skills] => Array
(
[skill] => Array
(
[id] => 1
[skill] => Array
(
[name] => Start-ups
)
[0] => Array
(
[id] => 2
[skill] => Array
(
[name] => Business Strategy
)
)
[1] => Array
(
[id] => 3
[skill] => Array
(
[name] => Marketing Strategy
)
)
[2] => Array
(
[id] => 12
[skill] => Array
(
[name] => Management
)
)
)
)
[email-address] => ########
[phone-numbers] => ########
[im-accounts] => ########
[twitter-accounts] => ########
[headline] => ########
[picture-url] => ########
[public-profile-url] => ########
)
)

These are many multiple array try that :
$person=$array["person"]; // $person is array
//get first array values
$firstname = $person['first-name'];
$lastname = $person['last-name'];
$email = $person['email-address'];
$headline = $person['headline'];
$picurl = $person['picture-url'];
$publicprofileurl = $person['public-profile-url'];
$skills=$person["skills"]; //$skills is array
$skills_inner=$skills["skill"]; //skills_inner is array
//get $skills_inner value (id)
$id=$skills_inner["id"];
$skills_inner_2=$skills_inner["skill"]; // $skills_inner_2 is array
// get $skills_inner_2 value (name)
$name=$skills_inner_2["name"];
Good Luck.

Related

How to create add an array to a multidimensional array

I'm trying to merge an array into another. If a business is a child of it's sector.
My aim is to create a array that looks like this:
[0] => Array (
[sector] => 198
[business] => Array (
[0] => 201
)
)
[1] => Array (
[sector] => 178
[business] => Array (
[0] => 181
[1] => 182
)
)
I currently have this:
[0] => Array (
[sector] => 198
)
[1] => Array (
[sector] => 178
)
and this (business ids):
Array
(
[0] => 201
)
Array
(
[0] => 181
[1] => 182
)
I need to nest these under sector.
This is what I have at the moment
$targets = array();
foreach ($taxonomy['sector'] as $sectorKey=>$sector) {
$getSectorTerm = get_term_by('slug', $sector, 'sector');
$getSectorId = $getSectorTerm->term_id;
$targets[] = array('sector'=>$getSectorId, 'business'=>[]);
foreach ($taxonomy['business'] as $keyBusiness=>$businesses) {
foreach ($businesses as $key => $business) {
$getBusinessTerm = get_term_by('slug', $business, 'sector');
$getBusinessId = $getBusinessTerm->term_id;
$getParentTerm = get_term( $getBusinessTerm, 'sector' );
$businessParentId = $getParentTerm->parent;
if ($businessParentId == $getSectorId) {
array_push($targets, $getBusinessId);
}
}
}
}
print_r($targets);
die;
The output is
Array
(
[0] => Array
(
[sector] => 198
)
[1] => 201
[2] => Array
(
[sector] => 178
)
[3] => 181
[4] => 182
)
Any help would be much appreciated.
Thanks
It should be work like this:-
$targets = array();
foreach ($taxonomy['sector'] as $sectorKey=>$sector) {
$getSectorTerm = get_term_by('slug', $sector, 'sector');
$getSectorId = $getSectorTerm->term_id;
$target_array = array('sector'=>$getSectorId, 'business'=>[]);
foreach ($taxonomy['business'] as $keyBusiness=>$businesses) {
foreach ($businesses as $key => $business) {
$getBusinessTerm = get_term_by('slug', $business, 'sector');
$getBusinessId = $getBusinessTerm->term_id;
$getParentTerm = get_term( $getBusinessTerm, 'sector' );
$businessParentId = $getParentTerm->parent;
if ($businessParentId == $getSectorId) {
$target_array['business'][] = $getBusinessId;
}
}
}
$targets[] = $target_array;
}
print_r($targets);
die;
If you'll keep current structure of array, it would be hard to populate the array with subarrays. It requires additional loop through the aray to find a certain sector value.
Are you able to change array structure to make it more compact and accessible? I mean that compact array has keys equal to sectors ID, and values are array of business IDs. For example like this:
[198] => Array (
[0] => 201
)
[178] => Array (
[0] => 181
[1] => 182
)
$targets = array();
foreach ($taxonomy['sector'] as $sectorKey=>$sector) {
$getSectorTerm = get_term_by('slug', $sector, 'sector');
$getSectorId = $getSectorTerm->term_id;
$targets[ $getSectorId ] = [];
foreach ($taxonomy['business'] as $keyBusiness=>$businesses) {
foreach ($businesses as $key => $business) {
$getBusinessTerm = get_term_by('slug', $business, 'sector');
$getBusinessId = $getBusinessTerm->term_id;
$getParentTerm = get_term( $getBusinessTerm, 'sector' );
$businessParentId = $getParentTerm->parent;
if ($businessParentId == $getSectorId) {
$targets[ $getSectorId ][] = $getBusinessId;
}
}
}
}
print_r($targets);
die;
To get a table:
foreach($targets as $sector => $businesses) {
echo "Sector ID: $sector<br>";
foreach( $businesses as $business ) {
echo "Business ID: $business<br>";
}
}

group array of php objects and sum by object key

I have an array of php objects that looks something like this:
Array
(
[0] => stdClass Object
(
[order_id] => 1513
[order_total] => 12500.00
[sales_rep] => Doe_John
)
[1] => stdClass Object
(
[order_id] => 1046
[order_total] => 3300.00
[sales_rep] => Doe_John
)
[2] => stdClass Object
(
[order_id] => 337
[order_total] => 4500.00
[sales_rep] => Mosby_Ted
)
)
I am trying to get an array that is set up more like this:
Array
(
[0] => stdClass Object
(
[sales_rep] => Doe_John
[total_sales] => 15800.00
)
[1] => stdClass Object
(
[sales_rep] => Mosby_Ted
[total_sales] => 4500.00
)
)
I want to combine all of the objects with the same "sales_rep" and get the sum of their associated "order_total", which you can see an example of in my desired array above. Any thoughts on how to accomplish this? I've been at it for hours now and have not been able to figure out a solution.
Thanks so much for the help!
try this
$tmp = array();
foreach($objs as $obj){ // where `$objs` is your objects
if(!in_array($obj->sales_rep,array_keys($tmp))){
$tmp[$obj->sales_rep] = (object)array(
'sales_rep' => $obj->sales_rep,
'total_sales' => $obj->order_total
);
}else{
$tmp[$obj->sales_rep]->total_sales += $obj->order_total;
}
}
print_r(array_values($tmp));
$obj0 = new StdClass();
$obj0->order_id = 1513;
$obj0->order_total = 12500.00;
$obj0->sales_rep = 'Doe_John';
$obj1 = new StdClass();
$obj1->order_id = 1046;
$obj1->order_total = 3300.00;
$obj1->sales_rep = 'Doe_John';
$obj2 = new StdClass();
$obj2->order_id = 337;
$obj2->order_total = 4500.00;
$obj2->sales_rep = 'Mosby_Ted';
$array = array(
$obj0,
$obj1,
$obj2,
);
$newArray = array();
foreach ($array as $item) {
if (array_key_exists($item->sales_rep, $newArray)) {
$newObj = $newArray[$item->sales_rep];
$newObj->order_total += $item->order_total;
} else {
$newObj = new StdClass();
$newObj->sales_rep = $item->sales_rep;
$newObj->order_total = $item->order_total;
$newArray[$newObj->sales_rep] = $newObj;
}
}
print_r(array_values($newArray));

PHP Manipulate associative array

I'm using PHP in doing our exercise which is to get the necessary details of a user and the quiz he/she took.
This is my code so far.
<?php
require("dbOption/Db.class.php");
$db = new Db();
$data = array();
$quiz = array();
$easy = "";
$normal = "";
$hard = "";
// SELECT id, course FROM quizz WHERE course = 2
$res_quizz = $db->query("SELECT id, course FROM quizz WHERE course = :course", array("course"=>2));
foreach ($res_quizz as $key => $value) {
$quizzid = $value['id'];
// SELECT easy, normal, hard, userid FROM quizzscore WHERE quizid = $quizid
$res_quizzscore = $db->query("SELECT easy, normal, hard, userid, quizzid FROM quizzscore WHERE quizzid = :quizzid", array("quizzid"=>$quizzid));
foreach ($res_quizzscore as $key => $value2) {
$easy = $value2['easy'];
$normal = $value2['normal'];
$hard = $value2['hard'];
$quizzid = $value2['quizzid'];
$userid = $value2['userid'];
$q = array(
'Qz'.$quizzid => array(
'easy' =>$easy,
'normal' =>$normal,
'hard' =>$hard,
)
);
$quiz['quizzes'] = $q;
// SELECT name FROM USER WHERE id = $userid
$res_user = $db->query("SELECT name FROM user WHERE id = :id", array("id"=>$userid));
foreach ($res_user as $key => $value3) {
$name = $value3['name'];
}
}
$data[$userid] = array();
$data[$userid]['name'] = $name;
$data[$userid]['quizzes'] = $quiz;
echo "<pre>";
print_r($data);
echo "<pre/>";
}
The array is poorly manipulated and that's why I'm getting this result.
Array
(
[1] => Array
(
[name] => John Smith
[quizzes] => Array
(
[quizzes] => Array
(
[Qz1] => Array
(
[easy] => 5
[normal] => 6
[hard] => 7
)
)
)
)
)
Array
(
[1] => Array
(
[name] => John Smith
[quizzes] => Array
(
[quizzes] => Array
(
[Qz2] => Array
(
[easy] => 3
[normal] => 4
[hard] => 5
)
)
)
)
)
What I want to have is this kind of result.
Array
(
[1] => Array
(
[name] => John Smith
[quizzes] => Array
(
[quizzes] => Array
(
[Qz1] => Array
(
[easy] => 5
[normal] => 6
[hard] => 7
)
[Qz2] => Array
(
[easy] => 3
[normal] => 4
[hard] => 5
)
)
)
)
)
Any ideas would be most appreciated.

Adding same element to every level of array php

I need to add another another element to each level of the array (sorry, think that is bad terminology).
I have an array -
Array ( [0] => Array (
[actor_rt_id] => 162683283,
[item_number] => 3 )
[1] => Array (
[actor_rt_id] => 162657351,
[item_number] => 5 )
)
This code produces the array. The commented out line is what I tried to add to the array. The code before the comment creates the array.
$data_itemone['actor_rt_id'] = $this->input->post('actor_id');
$data_itemtwo['item_number'] = $this->input->post('item_number');
$data_item = array_merge($data_itemone, $data_itemtwo);
$res = [];
foreach($data_item as $key => $value){
foreach ($value as $data => $thevalue) {
$res[$data][$key] = $thevalue;
//$res['film_id'] = $film_id;
}
}
I have an another variable I need to add from post which is a single string.
$film_id = $this->input->post('film_id');
I need it to be in the array like so -
Array ( [0] => Array (
[actor_rt_id] => 162683283,
[item_number] => 3,
[film_id] => 52352
)
[1] => Array (
[actor_rt_id] => 162657351,
[item_number] => 5,
[film_id] => 52352
)
)
...but my code (uncommented) produces -
Array ( [0] => Array (
[actor_rt_id] => 162683283,
[item_number] => 3
)
[film_id] => 16639,
[1] => Array
( [actor_rt_id] => 162657351,
[item_number] => 5 )
)
Tried a few things. Can't seem to get it to work.
Change
$res['film_id'] = $film_id;
to
$res[$data]['film_id'] = $film_id;
this will add it to the right array.
How if you try this.
$data_itemone['actor_rt_id'] = [123, 245];
$data_itemtwo['item_number'] = [456, 789];
$film_id = 52352;
$data_item = array_merge($data_itemone, $data_itemtwo);
$res = [];
foreach($data_item as $key => $value){
foreach ($value as $data => $thevalue) {
$res[$data][$key] = $thevalue;
$res[$data]['film_id'] = $film_id;
}
}
print_r($res);
Try this one
<?
$data_itemone['actor_rt_id'] = $this->input->post('actor_id');
$data_itemtwo['item_number'] = $this->input->post('item_number');
$film_id = $this->input->post('film_id');
$data_item = array_merge($data_itemone, $data_itemtwo);
$res = [];
foreach($data_item as $key => $value){
foreach ($value as $data => $thevalue) {
$res[$data][$key] = $thevalue;
$res[$data]['film_id'] = $film_id;
}
}
?>

php array flip value as key and make it simple

i have been troubling to format array correctly
i have this code:
require('simple_html_dom.php');
$table = array();
$html = file_get_html('apc.html');
foreach($html->find('tr') as $row) {
$rack = ltrim($row->find('td',0)->plaintext);
$location = ltrim($row->find('td',1)->plaintext);
$usage = ltrim($row->find('td',2)->plaintext);
$auk = ltrim($row->find('td',3)->plaintext);
$cost = ltrim($row->find('td',4)->plaintext);
$rack = rtrim($rack);
$location = rtrim($location);
$usage = rtrim($usage);
$auk = rtrim($auk);
$cost = rtrim($cost);
$table[$rack][$usage][$auk][$cost] = true;
}
echo '<pre>';
print_r($table);
echo '</pre>';
using simple_html_dom above i can convert html table to an array as follow:
[Rack01] => Array
(
[741,60] => Array
(
[1.409,04] => Array
(
[267,72] => 1
)
)
[110,88] => Array
(
[210,67] => Array
(
[40,03] => 1
)
)
)
[Rack 09] => Array
(
[843,84] => Array
(
[1.603,30] => Array
(
[304,63] => 1
)
)
)
I would like to have result as below:
array(
[0] => array (
[usage] => 'Rack 01',
[usage] => '741,60',
[auk] => '1.409.04',
[cost] => '267,72')
[1] => array (
[usage] => 'Rack 02',
[usage] => 'value???',
[auk] => 'value???',
[cost] => 'value???')
any help would be apreaciate
Something like this. Also note that trim will do both left and right:
foreach($html->find('tr') as $row) {
$rack = trim($row->find('td',0)->plaintext);
$location = trim($row->find('td',1)->plaintext);
$usage = trim($row->find('td',2)->plaintext);
$auk = trim($row->find('td',3)->plaintext);
$cost = trim($row->find('td',4)->plaintext);
$table[] = array('rack' => $rack,
'usage' => $usage,
'auk' => $auk,
'cost' => $cost);
}

Categories