How can I extract a PHP string from this sub-array? - php

The array I am looking at is this:
Array
(
[0] => Array
(
[0] => Bigcommerce\Api\Resources\ProductCustomField Object
(
[ignoreOnCreate:protected] => Array
(
[0] => id
[1] => product_id
)
[ignoreOnUpdate:protected] => Array
(
[0] => id
[1] => product_id
)
[fields:protected] => stdClass Object
(
[id] => 17
[product_id] => 3232
[name] => Artist
[text] => Test
)
[id:protected] => 17
[ignoreIfZero:protected] => Array
(
)
)
)
)
I want to check to see if 'Artist' exists in a php conditional statement. But I don't know how to turn 'Artist' into a string.
UPDATED:
I did not find understand how to extract that value into a string, but I got what I was looking for using the method related to the bigcommerce api:
$customs = Bigcommerce::getProductCustomFields($product->id);
foreach($customs as $custom) {
if($custom->name == 'Artist'): // do something
endif;
}

Ok, looking at the source, it seems you should be able to use the magic __get method. Try
$array[0][0]->name == 'Artist'

The value of the custom field would be stored in the "text" resource for that custom field.
See the following link where you can see the 4 properties of a custom field.
https://developer.bigcommerce.com/api/stores/v2/products/custom_fields

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

Getting sugarcrm array output via REST api sorted with PHP correctly

First, I'm using sugarcrm pro 6.5 and accessing via rest v4, so I have this array that's being returned from printing $results that is working fine:
stdClass Object
(
[result_count] => 2000
[total_count] => 3390
[next_offset] => 2000
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => 77da301b-83dd-4fe6-e38f-53ba151fb084
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => 77da301b-83dd-4fe6-e38f-53ba151fb084
)
[name] => stdClass Object
(
[name] => name
[value] => Jim Beam
)
[status] => stdClass Object
(
[name] => status
[value] => Dead
)
[website] => stdClass Object
(
[name] => website
[value] => website.com
)
[phone_cr] => stdClass Object
(
[name] => phone_cr
[value] => 1-888-888-8888
)
)
)
[1] => stdClass Object
(
[id] => d0ecc069-d556-98f3-41f2-53ba1468327a
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => d0ecc069-d556-98f3-41f2-53ba1468327a
)
[name] => stdClass Object
(
[name] => name
[value] => John Doe
)
[status] => stdClass Object
(
[name] => status
[value] => New
)
[website] => stdClass Object
(
[name] => website
[value] => web.com
)
[phone_cr] => stdClass Object
(
[name] => phone_cr
[value] => 1-888-888-8888
)
)
)
I'm using a query from the api to filter the results for the user I'm targeting:
'query' => "leads.assigned_user_id='user_ID-here'",
'order_by' => "date_entered DESC",
This works fine. So I've ran a foreach () statement to retrieve only one field on a button click, which also works just fine. What I really need to accomplish is before this statement, a foreach() command (or something else?) to filter out and retrieve ONLY the "New" results in the status value, and from that group output an array showing only the website field. Seen in the "desired end result section of this question."
This is the code I'm filtering the field I'm targeting and having a new array created with if that helps bridge the gap:
$results = call('get_entry_list', $params, $url);
$eresult = array();
foreach ($results->entry_list as $index=>$value_list) {
$listed = $value_list->name_value_list->website->value;
$eresult[] = $listed;}
So the desired end result based on this data should be:
Array
(
[1] => web.com
)
I'm unsure what I need to do to filter the "Status" field to only then be ran with the $eresult array I created to achieve this. To be clear, everything is working as it should, and my print from $eresult is outputting exactly as it should by returning all results in the website value area, I just need some help to get it sorted before going to that step by sorting it by the "new" status first without all the extra 'stuff,' then sorting out the array in my desired format with the foreach() statement above. I tried to cut out all the other code, as it's a pretty long project, so this should be all the relevant information for the particular goal I need to accomplish in this segment. Any help is greatly appreciated! Thank you!
I've decided to create a second script for this as a temp solution by adding:
'query' => "(leads.assigned_user_id='user_ID-here') AND (status='New')"
So I guess that works, I was trying to avoid calling another script for just one separate function, but it is working fine.

Arbitrary element in array (the value) to a key that is an array

So, I have an array that looks like this:
$grades =
Array(
[0] => Array ( [Grade] => Array ( [id] => 0 [name] => Kindegarten ) )
[1] => Array ( [Grade] => Array ( [id] => 1 [name] => First ) )
[2] => Array ( [Grade] => Array ( [id] => 2 [name] => Second ) )
[3] => Array ( [Grade] => Array ( [id] => 3 [name] => Third ) )
[4] => Array ( [Grade] => Array ( [id] => 4 [name] => Fourth ) )
[5] => Array ( [Grade] => Array ( [id] => 5 [name] => Fifth ) )
)//End array
I am wondering is there a way to arbitrarily way to pick a key (the first 0-5)? CakePHP returns the items in my table like this and I don't understand how I can conform the to the printing of Form helper with the options.
echo $this->Form->input('grade_selection',
array('type' => 'radio', 'options' => array($grades[?]['Grade']['id'] => $grades[?]['Grade']['name'])));
The ? being how to get it to change in the option so I can get each of the items in the array?
class GradesController extends AppController {
public function index(){
//Gets all the rows in the grade table and stores it into a variable called grade.
//$grades = $this->Grade->find('all');
$grades = $this->Grade->getGrades();
//Returns the $grades variable when it is requested.
if($this->request->is('requested')){
return $grades;
}
//Sends the $grades variable to the view file. The string 'grades' is the name of the variable that the Grades View file will have the same setup as the $grades.
$this->set('grades', $grades);
}
}
I am wondering is there a way to arbitrarily way to pick a key (the first 0-5)?
Yes, php, not CakePHP
$keys = array_keys($grades);
$foo = $keys[rand(0, count($keys) - 1)]
Now, I don't think that's what you need. I see you are using a radio button, what do you want to display in the radio button(s)?
EDIT
Assuming this data come from a Model named "Grade", you would do this in your crontroller
$grades = $this->Grade->find('list');
$this->set(compact('grades'));
Then, in your view:
echo $this->Form->input('grade_selection', array('options' => $grades));

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

How do I add/remove an entry to an array of objects?

I have an array that's made up of data returned by a SQL query:
$_SESSION['licensed_users'] = $db->get_results("SELECT np.id FROM `new_people` np LEFT JOIN institute.users_roles ur ON np.institute_uid = ur.uid WHERE np.company_id=$company_id AND ur.rid=8 AND np.active = 1");
This returns an array that looks like this:
Array (
[0] => stdClass Object ( [id] => 25590 )
[1] => stdClass Object ( [id] => 40657 )
[2] => stdClass Object ( [id] => 60685 )
[3] => stdClass Object ( [id] => 61900 )
[4] => stdClass Object ( [id] => 65224 )
[5] => stdClass Object ( [id] => 65369 )
[6] => stdClass Object ( [id] => 79171 )
[7] => stdClass Object ( [id] => 80763 )
[8] => stdClass Object ( [id] => 80762 )
[9] => stdClass Object ( [id] => 80761 )
)
In another section of the code I loop through the values to see if the current user is part of that array:
foreach($_SESSION'licensed_users'] as $key=>$value) {
if($value->id == $people_id) {
$is_licensed = true;
}
}
$is_licensed is used to determine which set of form fields to display to the user. When the user submits the form, if a certain group of fields is set to a certain value, I need to either add the user ($people_id) to the $_SESSION['licensed_users'] array if $is_licensed is false, or remove them from the array if $is_licensed is true. The code for determining which action to take is no problem; I just can't for the life of me remember/figure out how to add to an array of objects. I've seen this and this, but I already know if the ID is in the array or not; I just need to be able to add or remove it.
(Yes, there is a reason we're using session variables - we need to be able to pass values between pages of the site. It's either that or cookies.)
I've tried $_SESSION['licensed_users'][] = array("id"=>$people_id); but it doesn't seem to actually do anything.
To remove $lu[6]:
unset($lu[6]);
To add an element:
$lu[] = $current_user;

Categories