I have used login through facebook on my website, and after login I am fetching values and profession of user.
I used:
$work = $me['work'];
code to get the work history and got the data in an array
Array
(
[0] => Array
(
[employer] => Array
(
[id] => 178191330369
[name] => Office
)
[position] => Array
(
[id] => 106011616105574
[name] => Students
)
[start_date] => 0000-00
)
)
now I wish to get the value students from the array. for this I used
$totalwork = $work['position']['name'];
echo '<pre>';
print_r($totalwork);
echo '</pre>';
but I am not able to fetch value from the array. How can it be done?
Related
Working on a personal project that will pull results from an API with full details of each pokemon.
So far I got the contents of the URL and returned the results into a JSON array format.
At the moment I am stuck on trying to retrieve results for[stats] inside from the array in an efficient manner.
private function getGenOnePokemon()
{
// the url of the api
$url = $this->baseUrl;
//get the contents of $url var and decode it into a json array
$json = file_get_contents($url , true);
$pokemon = json_decode($json, true, JSON_UNESCAPED_UNICODE);
// array output of pokemon
echo '<pre> ';
print_r($pokemon);
echo'</pre>';
//echo out value as speed
foreach($pokemon['results'][0] as $happy)
{
echo $happy['name'] . '<br />';
}
// echo base_stat value for speed with value of 90
echo $pokemon['stats'][0]['base_stat'];
}
However I do not seem to get anywhere much printing values/keys as I need to add something else to have full access to the values?
Would prefer not to directly access results, like I am doing with base_stat as plan on using this logic to pass into HTML View layer later.
Example of print_r dump (not full dump as really long) Full example: https://pokeapi.co/api/v2/pokemon/pikachu
Array
(
[forms] => Array
(
[0] => Array
(
[url] => https://pokeapi.co/api/v2/pokemon-form/25/
[name] => pikachu
)
)
[abilities] => Array
(
[0] => Array
(
[slot] => 3
[is_hidden] => 1
[ability] => Array
(
[url] => https://pokeapi.co/api/v2/ability/31/
[name] => lightning-rod
)
)
[1] => Array
(
[slot] => 1
[is_hidden] =>
[ability] => Array
(
[url] => https://pokeapi.co/api/v2/ability/9/
[name] => static
)
)
)
[stats] => Array
(
[0] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/6/
[name] => speed
)
[effort] => 2
[base_stat] => 90
)
[1] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/5/
[name] => special-defense
)
[effort] => 0
[base_stat] => 50
)
[2] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/4/
[name] => special-attack
)
[effort] => 0
[base_stat] => 50
)
[3] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/3/
[name] => defense
)
[effort] => 0
[base_stat] => 40
)
[4] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/2/
[name] => attack
)
[effort] => 0
[base_stat] => 55
)
[5] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/1/
[name] => hp
)
[effort] => 0
[base_stat] => 35
)
)
Any advice on how to access the data using foreach or other tips greatly appreciated. Thank you!
PHP has a specific function designed to target columnar data from arrays. It is called array_column()
If you want to isolate all of the name elements inside the forms subarray, use this:
$names=array_column($pokemon['forms'],'name');
If you want to isolate all of the base_stat elements inside of the stats subarray, use this:
$base_stats=array_column($pokemon['stats'],'base_stat');
Now you will have $names and $base_stats which are single-dimensional arrays by which you can perform additional processes or return from the function. Clean, intuitive, and simple.
Your $pokemon array doesn't contain a results field. There's only an forms field. So you should iterate over forms to print the names of the forms.
foreach($pokemon['forms'] as $happy) {
echo $happy['name'] . '<br />';
}
You could do the same thing with the stats
foreach($pokemon['stats'] as $stat) {
$base_stat = $stat['base_stat'];
// ...
}
I'm trying to echo an array value from an array that was created using the following code.
The data for the array is pulled from a MySQL table.
$names = $db->fetchAll("select `name` from `classes`");
This is what is stored in the $names variable
Array ( [0] => Array ( [name] => Web Design ) [1] => Array ( [name] =>
Art History ) [2] => Array ( [name] => Gym ) [3] => Array ( [name] =>
English ) [4] => Array ( [name] => Biology ) [5] => Array ( [name] =>
3D Animation ) [6] => Array ( [name] => Tech Disc ) [7] => Array (
[name] => Math ) [8] => Array ( [name] => Dance ) [9] => Array (
[name] => Video Production ) [10] => Array ( [name] => Home Ec ) [11]
=> Array ( [name] => Government ) [12] => Array ( [name] => Physics ) )
I'm attempting to echo a [name] value OR all [name] values, but I can't figure it out. I've tried the following....
<?php echo $names['name'];?>
returns nothing
<?php echo $names['0'];?> //AND\\ <?php echo $names[0];?>
Both return the string Array
Can someone please help me echo a single value from the array?
Example: Web Design or Art History
Also can someone please help me echo all the values from the array?
Example: Web Design Art History Gym English Biology ......
You should try <?php echo $names[0]['name'];?>, <?php echo $names[1]['name'];?>, etc....
This is because your query function returns an array with all results in it.
In that array, each row returned is an array again. And each field is a key in that array.
Echoing a single value:
echo $names[0]['name'];
Echoing all values:
foreach ($names as $name) {
echo $name['name'].' ';
}
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
I have a challenge in getting back the array i stored in a session.
I stored the array in a array like this
$downlines = '2,3,4,5,6,7,8';
$_SESSION['downline'] = $afrisoft->dbarray("SELECT username, email FROM users WHERE id IN $downlines")
When i print_r the session i get this
Array (
[0] => Array (
[username] => mcbel
[email] => firstmail#gmail.com
)
[1] => Array (
[username] => bimibola
[email] => secondmail#yahoo.com
)
[2] => Array (
[username] => shadie
[email] => thirdmail#gmail.com
)
[3] => Array (
[username] => Hifee
[email] => ife#ife.net
)
)
What i intend to achieve is to get the data stored in ['usernmae'] and ['email'], however when i try to print_r($_SESSION['downline']['username']) and print_r($_SESSION['downline']['email']) it returns no values.
I'll appreciate any help i can get on this.
You have an array that is returned by your function. you can access the first element of that array by $_SESSION['downline'][0]['username']
you can access the full results by traversing through $_SESSION['downline'] by doing
foreach ($_SESSION['downline'] as $item) {
echo $item['username']
}
<?php
foreach($_SESSION['downline'] as $userDetails){
echo $userDetails['username'];
}
can't figure out what is the right syntax to add an additional attributes to a LDAP entry. When authenticating i get this array:
Array
(
[0] => Array
(
[cn] => Array
(
[0] => Vit Kos
)
[shortname] => Array
(
[0] => vit.kos
)
[uid] => Array
(
[0] => vit.kos
)
[mail] => Array
(
[0] => vit.kos#email.com
)
[objectclass] => Array
(
[0] => top
[1] => person
[2] => organizationalPerson
[3] => inetOrgPerson
[4] => dominoPerson
)
[givenname] => Array
(
[0] => Vit
)
[userpassword] => Array
(
[0] => password here
)
[sn] => Array
(
[0] => Kos
)
[localadmin] => Array
(
[0] => CN=#SysHQAdmin
)
[mailaddress] => Array
(
[0] => Vit.Kos#email.com
)
[maildomain] => Array
(
[0] => EMAIL
)
[dn] => CN=Vit Kos,OU=###,O=EMAIL
)
)
need to get an additional attribute member to be like
Array (
[uid] => Array
(
[0] => vit.kos
)
[mail] => Array
(
[0] => vit.kos#email.com
)
[member] => Array
(
[0] => MEMBER HERE
)
)
Never worked with LDAP before so it's quite confusing for me. Thanks for the answers.
To assign data to the directory item that you retrieved above, you will perform a "modify" operation with ldap_modify(). This is assuming that the schema of your database allows an attribute called member on this object - which it may not, you cannot simply add attributes to any object as and when you feel like it.
Firstly, you will need to create the entry or entries that will belong to the member attribute, and store them in an array:
$member = array (
0 => "This is some data",
1 => "This is some more data"
);
In order to tell the directory which object we want to modify, we will need it's DN. We can get this from the result of your previous search/list/read operation - the array that you show you have retrieved already (I assume this is stored in a variable called $array):
$dn = $array[0]['dn'];
Now we have all the information we need to perform the modify operation (I assume your connected/bound LDAP resource is held in a variable called $ds):
$result = ldap_modify($ds, $dn, array('member'=>$member));
After this, $result will be a boolean indicating whether the operation was successful or not. If it was unsuccessful, you can get an error message explaining why by calling:
$error = ldap_error($ds);