I'm trying to insert data from the LinkedIn API into a MySql database.
My PHP code is getting all the values from the Array which is not in the second level. I don't know how to explain this, but I'll include the Array structure below:
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] => ########
)
)
My PHP code is as follows:
/* SKILLS */
// An array to hold it temporarily
$skills_arr = array();
foreach ($array->skills->skill as $t) {
// Cast it to a string to get the text value back
$skills_arr[] = (string)$t;
}
// Implode the array to a string
$skills = implode(', ', $skills_arr);
What i need is to get all the "Skills" and seperate them by comma ",".
If my code worked I would have got the following value as $skills = "Start-ups, Business-strategy, ...."
Ideas?
You can use nested Foreach loop for this.Like
$skills_arr = array();
foreach($array['person']['skills']['skill'] as $val)
{
foreach($val['skill'] as $res)
{
$skills_arr[] = $res['name'];
}
}
echo $skills = implode(', ', $skills_arr);
Related
My arrays result is like this one
Array
(
[0] => Array
(
[id] => Bank Transfer
[ec] => 1000
[accounts] => Array
(
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
)
)
[1] => Array
(
[id] => Wired
[ec] => 1001
[accounts] => Array
(
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
[2] => Array
(
[name] => Account ID
[value] =>
)
)
)
)
It's weird because 2nd array of accounts contains same value as first array.
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
How to prevent this duplicated so the 2nd array of accounts will only show
[0] => Array
(
[name] => Account ID
[value] =>
)
Here's my code
$arr = $arr_pay = array();
foreach($site_payment as $key => $value){
if($value['status'] && $value['ec']>=1000){
$payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
foreach ($payment_data_cust as $ke => $va) {
$arr[] = array("name"=>$va,"value"=>'');
}
$spc[] = array(
"id"=>$value['id'],
"ec"=>$value['ec'],
"accounts"=>$arr
);
}
}
Array of $site_payment contains
[Bank Transfer] => Array
(
[id] => Bank Transfer
[ec] => 1000
[status] => 1
[auto_wd_data] => IjZRcWp1aGtzNmZHbjVPZTlkeStGZVNPaWdPY0lrZ0UyQnd6eFhxQUZoR1VEeU82TzVJZkdMelJrZzJKS3lxXC9yTm5meFBndFRlUDQ9Ig==
)
[Dana] => Array
(
[id] => Wired
[ec] => 1001
[status] => 1
[auto_wd_data] => IkNDek9IY1BtelVEeFFxZEtMc0hvalBkbVBRdENEZEJWakZoaFBJWkNBUk09Ig==
)
I want to show the auto_wd_data of $site_payments with different array so it's became the result, but not duplicating in each array
Please help me to solve the problem
Duplication is due to the $arr is not being reset
$arr_pay = array();
foreach($site_payment as $key => $value){
$arr = array(); // Resetting
if($value['status'] && $value['ec']>=1000){
$payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
foreach ($payment_data_cust as $ke => $va) {
$arr[] = array("name"=>$va,"value"=>'');
}
$spc[] = array(
"id"=>$value['id'],
"ec"=>$value['ec'],
"accounts"=>$arr
);
}
}
I'm new to PHP. I'm doing one my project with php and I'm new to array functions and all those things. I have tried but not get success in that. let me show you my sql query array.
I have one my array which is as below:
Array
(
[0] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Rutul
[ulname] => Shah
[name] => Clinic
)
[1] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Rutul
[ulname] => Shah
[name] => Clinic
)
[2] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[3] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[4] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[5] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Amit
[ulname] => Mahida
[name] => Cancer Specialist
)
[6] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Amit
[ulname] => Mahida
[name] => Breach Candy Hospital
)
)
Now I want my resulted array as below :
Array
(
[2016-08-25] => Array
(
[ Clinic] => Array
(
[Rutul Shah] => Array
(
[appointments] => 1
)
[Administrator Administrator] => Array
(
[appointments] => 2
)
)
)
[2016-08-26] => Array
(
[Clinic] => Array
(
[Rutul Shah] => Array
(
[appointments] => 1
)
[Administrator Administrator] => Array
(
[appointments] => 1
)
)
[Cancer Specialist] => Array
(
[Amit Mahida] => Array
(
[appointments] => 1
)
)
[Breach Candy Hospital] => Array
(
[Amit Mahida] => Array
(
[appointments] => 1
)
)
)
)
you want to loop through your appointments array and use its contents to generate the other data structure. let's call your first array $input and your second array $output:
// initialize output array
$output = [];
// loop through each $appt in the $input array
foreach($input as $appt) {
// get shorter var names for appt data
$date = $appt['pc_eventDate'];
$name = $appt['name'];
$uname = $appt['ufname'].' '.$appt['ulname'];
// initialize each level of the data structure if it doesn't already exist
if(!isset($output[$date])) $output[$date] = [];
if(!isset($output[$date][$name])) $output[$date][$name] = [];
if(!isset($output[$date][$name][$uname])) $output[$date][$name][$uname] = [];
// initialize the number of appts to 0
if(!isset($output[$date][$name][$uname]['appointments'])) $output[$date][$name][$uname]['appointments'] = 0;
// increment the number of appts
$output[$date][$name][$uname]['appointments']++;
}
the important thing is the intialization of each sub-array in the new structure according to the data in the old structure -- from there we're just counting the number of appointments that match the new data.
good luck!
Say the array is question is $arr
This will arrange the array in the way you wanted as solution
foreach ($arr as $key => $value) {
if(!is_array($value['pc_eventDate]))
$value['pc_eventDate] = [];
if(!is_array($value['pc_eventDate]['name']))
$value['pc_eventDate]['name'] = [];
if(!is_array($value['pc_eventDate']['name']['ufname'.' ulname'])){
$value['pc_eventDate']['name']['ufname'.' ulname'] = [];
$value['pc_eventDate']['name']['ufname'.' ulname']['appointments'] = 1;
}else{
$value['pc_eventDate']['name']['ufname'.' ulname']['appointments'] += 1;
}
}
You need to do something like the above.
Try running the above code, there could be some typo. If its doesnt yields your desired result, try commenting out all the lines in the body of the foreach and var_dump() each step to test the building of the array structure.
Thanks
I have an array like the following. This is the results of a query on one of our servers.
Array
(
[count] => 1
[0] => Array
(
[name] => Array
(
[count] => 1
[0] => mac
)
[0] => name
[staffid] => Array
(
[count] => 1
[0] => 1234
)
[1] => staffid
[school] => Array
(
[count] => 1
[0] => western
)
[2] => school
[count] => 3
[dn] => cn=mac,cn=staff
)
)
How do I loop through this array and create a new array as follows.
Array
(
[name] => mac
[staffid] => 1234
[school] => western
)
I've tried a foreach loop echoing the key & values, but I'm not sure where to go from there. There will be more results returned as the query is expanded, but original array layout will be the same and the new layout needs to be the same format.
Any ideas ?
Thanks
Try this:
$result = array();
foreach($yourArray as $element){
for($i=0;$i<$element['count']; $i++){
unset($element[$element[$i]]['count']);
$result[$element[$i]] = implode(', ', $element[$element[$i]]);
}
}
I'm trying to unset two specific array positions which contain two values.
My actual code to fill the array.
function get_store_list(){
$data = #file_get_contents('http://www.zwinky.com/xml/clothingList.xml');
$data = #simplexml_load_string($data);
$data = $data->xpath('//stores/store');
foreach($data as $store) {
$storeArray[] = array(
"name" => (string)$store['name'],
"id" => (string)$store['id']
);
}
return $storeArray;
}
$store = get_store_list();
The array looks like the following incase ill echo it out using print_r function:
Array
(
[0] => Array
(
[name] => Artizans
[id] => 20037336
)
[1] => Array
(
[name] => Bwabies!
[id] => 20080134
)
[2] => Array
(
[name] => Crave Mart
[id] => 20097365
)
[3] => Array
(
[name] => David & Goliath
[id] => 20099998
)
[4] => Array
(
[name] => Domo
[id] => 20098166
)
[5] => Array
(
[name] => Emily the Strange
[id] => 20101926
)
[6] => Array
(
[name] => Garfield
[id] => 20098167
)
[7] => Array
(
[name] => Jetsetter
[id] => 26
)
[8] => Array
(
[name] => Like Dat
[id] => 3
)
[9] => Array
(
[name] => Paris Hilton
[id] => 21
)
[10] => Array
(
[name] => Peppermint Place
[id] => 12
)
[11] => Array
(
[name] => Rocawear
[id] => 19
)
[12] => Array
(
[name] => ShoeBuy
[id] => 10
)
[13] => Array
(
[name] => Skelanimals
[id] => 20100198
)
[14] => Array
(
[name] => Snoop Dogg
[id] => 20
)
[15] => Array
(
[name] => SW&TH
[id] => 20096121
)
[16] => Array
(
[name] => The Castle
[id] => 1
)
[17] => Array
(
[name] => The Lair
[id] => 4
)
[18] => Array
(
[name] => The Mix
[id] => 923
)
[19] => Array
(
[name] => The Powerpuff Girls
[id] => 20098121
)
[20] => Array
(
[name] => The Surf Shop
[id] => 5
)
[21] => Array
(
[name] => Tie The Knot
[id] => 20076231
)
[22] => Array
(
[name] => tokidoki
[id] => 20099224
)
[23] => Array
(
[name] => University Club
[id] => 2
)
[24] => Array
(
[name] => Z Avenue
[id] => 6
)
[25] => Array
(
[name] => Z's Greetings
[id] => 20099506
)
)
Now $store does contain 2 array indexes which I have to delete. Which are the following ids: 21 and 20076231
I've been trying the following already:
Array search code without beeing success. Does anyone have a idea what I could try?
There are a handful different approaches for this simple issue. One of them could be using function array_filter():
/**
* #param array $list the list to process
* #param array $IDsToRemove the IDs of elements to remove from $list
* #return array a subset of $list that does not contain elements having 'id' in $IDsToRemove
*/
function removeFromArray(array $list, array $IDsToRemove)
{
return array_filter(
// Filter the input list...
$list,
// ... using a function...
function (array $item) use ($IDsToRemove) {
// ... that accepts an element if its "id" is not in $IDsToRemove
return ! in_array($item['id'], $IDsToRemove);
}
);
}
// Usage
$filteredStore = removeFromArray($store, array(21, 20076231));
Try this in your loop, this will not include in your array than no need to unset like this:
foreach($data as $store) {
if($store['id'] == '20076231')
continue;
$storeArray[] = array(
"name" => (string)$store['name'],
"id" => (string)$store['id']
);
}
If you need to unset an item from your array after it's been created, take a look at array_map.
First map your array to retrieve the index of each ID.
$map = array_map(function($item){ return $item['id']; }, $store);
Then get the index of your ID from the map (e.g. 21).
$index = array_search(21, $map);
Then remove with array_splice.
array_splice($store, $index, 1);
why not use directly the id in your $storeArray? And as it seems to be integer why do you force it to (string)?
Try this:
function get_store_list(){
$data = #file_get_contents('http://www.zwinky.com/xml/clothingList.xml');
$data = #simplexml_load_string($data);
$data = $data->xpath('//stores/store');
foreach($data as $store) {
$storeArray[(int)$store['id']] = array(
"name" => (string)$store['name']
);
}
return $storeArray;
}
// delete the keys you want
unset ($storeArray[21], $storeArray[20076231]);
// or if you have more ids to delete you can create a deleteArray
$deleteArray = array(2, 20076231);
foreach ($deleteArray as $toDelete){
unset($storeArray($toDelete);
}
One line code is cool but sometimes explicit code is preferable.
function filter_by_id(array $data, $id)
{
foreach ($data as $k => &$v) {
foreach ((array) $id as $i) {
if ($v['id'] === $i) {
$v = null;
}
}
}
// 'array_filter()' produces a new array without the null entries.
// 'array_values()' produces a new array with indexes without gaps.
return array_values(array_filter($data));
}
You can filter by one id at time
$store = filter_by_id($store, 21);
Or you can filter multiple ids at the same time:
$store = filter_by_id($store, [21, 20076231]);
I want to compare two tabdelimeted files. I take the files and converts them into two arrays with the following structure:
Array 1
Array
(
[0] => Array
(
[name] => name1
[qty] => 200
)
[1] => Array
(
[name] => name2
[qty] => 9
)
[2] => Array
(
[name] => name3
[qty] => 3
)
[3] => Array
(
[name] => name4
[qty] => 1
)
)
Array 2
Array
(
[0] => Array
(
[name] => name1
[qty] => 180
)
[1] => Array
(
[name] => name2
[qty] => 9
)
)
How can I compare these two arrays and where the value is different to replace the value in the array 2 with array of value 1.
The easiest way to do this would be to create an associative array for the second set of data, instead of the array format you has used above. Since you only seem to have two "columns", and these are effectively a key/value relationship this should be nice and easy.
This example takes the two input arrays you have generated to do it, but you can probably adjust this so that you create the associative array directly as you read the second:
// first create an associative array from the second indexed array
$secondAssoc = array();
foreach ($secondArray as $row) {
$secondAssoc[$row['name']] = $row['qty'];
}
/*
$secondAssoc now looks like:
Array
(
[name1] => 180
[name2] => 9
)
*/
// Now loop the first array and update it
foreach ($firstArray as $rowId => $row) {
if (isset($secondAssoc[$row['name']]) && $secondAssoc[$row['name']] != $row['qty']) {
$firstArray[$rowId]['qty'] = $secondAssoc[$row['name']];
}
}
/*
$firstArray now looks like this:
Array
(
[0] => Array
(
[name] => name1
[qty] => 180
)
[1] => Array
(
[name] => name2
[qty] => 9
)
[2] => Array
(
[name] => name3
[qty] => 3
)
[3] => Array
(
[name] => name4
[qty] => 1
)
)
*/
See it working.
EDIT Here is a version that also creates an array, $modifiedItems, that holds only the items that have changed:
// first create an associative array from the second indexed array
$secondAssoc = array();
foreach ($secondArray as $row) {
$secondAssoc[$row['name']] = $row['qty'];
}
// Now loop the first array and update it
$modifiedItems = array();
foreach ($firstArray as $rowId => $row) {
if (isset($secondAssoc[$row['name']]) && $secondAssoc[$row['name']] != $row['qty']) {
$firstArray[$rowId]['qty'] = $secondAssoc[$row['name']];
$modifiedItems[] = array('name'=>$row['name'],'qty'=>$secondAssoc[$row['name']]);
}
}
See it working.