How array data stored in a session - php

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'];
}

Related

Getting Value from Nested Array -- PHP Wordpress --

I am using get_post_meta like below:
$job_owner = get_post_meta($post->ID, 'assignedUsers', true);
That returns the following:
(
[total] => 1
[data] => Array
(
[0] => stdClass Object
(
[id] => 440968
[firstName] => John
[lastName] => Doe
[email] => john#website.com
)
)
)
I am trying to grab the values from the object but catch an error each time I simply try and use echo $job_owner. Error is - Object of class stdClass could not be converted to string
I have tried to use:
$array = json_decode(json_encode($job_owner), true);
Which returns the arrays:
Array
(
[total] => 1
[data] => Array
(
[0] => Array
(
[id] => 440968
[firstName] => Megan
[lastName] => Collins
[email] => megan#bridgeviewit.com
)
)
)
But I cannot seem to get anything to return using echo $array[0]->id etc...
My ideal scenario is to use the array values as variables to use throughout the theme.
So with echo $array[0]->id are trying to get the 0th element of that array which is total. So echo $array[0]->id should fail but echo $array[0] should return 1. If you change your request to $array[data][0]->id or $array[1][0]->id that should get you the value you are looking for - the id of the first element in the data bit.
$array[data][0]->id

Add a value to existing array

I have an array of data:
Array ( [0] => stdClass Object
(
[name] => admin
[email] => admin#adminmail.com
[password] => cxvxvcxvcxv
)
)
I want to add a new data to this array so the final array looks like:
Array( [0] => stdClass Object
(
[name] => admin
[email] => admin#adminmail.com
[password] => cxvxvcxvcxv
[enabled] => true
)
)
I tried array_merge(), but it gives me result like this:
Array( [0] => stdClass Object
(
[name] => admin
[email] => admin#adminmail.com
[password] => cxvxvcxvcxv
)
[enabled] => true
)
How do I achieve the result I want?
$myArray[0]->enabled = 'true';
Just add the new field straight into your object and assign the value you want to it. This should do the trick for you.
If you array has multiple nested arrays you will need a loop though. My code will only work for position 0 like you asked.

PHP - Check if Array exists in Multidimensional Array with an Exact Match of Pair

I need to check if an exact pair of two values exists in a multidimensional array together.
I have an array like this:
Array
(
[code] => 200
[response] => Success
[0] => Array
(
[email] => example123#sample.com
[status] => Approved: Printed & Cleared
)
[1] => Array
(
[email] => xxexample123#sample.com
[status] => Pending
)
[2] => Array
(
[email] => example1345#sample.com
[status] => Approved
)
[3] => Array
(
[email] => example1235#sample.com
[status] => Approved: Printed & Cleared
)
)
Then I have an array that looks like this:
Array
(
[email] => xxexample123#sample.com
[status] => Pending
)
I need to check if that exact pair exists in the multidimensional array. Not just that the status and email appear seperate from each other.
You can use array_search() in the same manner if you need to get the key, but at its simplest (assuming $array1 only has the keys and values that are being search for and in the same order):
$array2 = array('email' => 'example123#sample.com',
'status' => 'Pending');
if(in_array($array2, $array1)) {
//yes
} else {
//no
}
See the Demo showing found and not found.

fetch value from an array of arrays in PHP

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?

adding a LDAP attribute with PHP

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);

Categories