Am trying to come up with a code to search for a string in an array of objects, if string is found, get values of the submenus object. Like if the string "main/dashboard" is found then get submenus
stdClass Object
(
[type] => single
[slug] => view_admin_dashboard
[menus] => stdClass Object
(
[label] => Dashboard
[icon] => dashboard
[url] => main/dashboard
)
[submenus] => Array
(
[0] => stdClass Object
(
[submenu_slug] => view_all_users
[label] => View all Users
[icon] => users
[url] => main/users/all
)
[1] => stdClass Object
(
[submenu_slug] => delete_users
[label] => Delete Users
[icon] => users
[url] => main/user/delete
)
)
)
I have this for now but its giving me error in_array expects parameter 2 to be array;
foreach($mainarray as $menus => $menu){
if(in_array("main/dashboard",$menu)){
foreach($menu as $submenu){
echo $submenu->url;
}
}
}
Maybe you want something like this:
foreach($mainarray as $menu){
if($menu->menus->url == "main/dashboard"){ // if found the url in the object
foreach($menu->submenus as $submenu)
echo $submenu->url;
}
}
}
Related
Someone please can explain me how i can get the values from entry_list in a foreach loop in php?
Following is the return data structure. Please help.
stdClass Object
(
[result_count] => 1
[total_count] => 1
[next_offset] => 1
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => 6adfd27d-0a48-48c9-97b2-5639c6d9e697
[module_name] => Leads
[name_value_list] => stdClass Object
(
[assigned_user_name] => stdClass Object
(
[name] => assigned_user_name
[value] => Administrator
)
[modified_by_name] => stdClass Object
(
[name] => modified_by_name
[value] => Administrator
)
[created_by_name] => stdClass Object
(
[name] => created_by_name
[value] => Administrator
)
[id] => stdClass Object
(
[name] => id
[value] => 6adfd27d-0a48-48c9-97b2-5639c6d9e697
)
Try this,
foreach($arr->entry_list as $row) {
foreach($row->name_value_list as $key => $val){
echo $key;// will echo assigned_user_name
echo $val->name; // assigned_user_name
echo $val->value; // Administrator
}
}
Sample values are given for first row... Similarly, all values will get printed.
Can someone explain me how to get data out of this...like if I just want subject, description..etc...
stdClass Object
(
[tickets] => Array
(
[0] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/1.json
[id] => 1
[external_id] =>
[via] => stdClass Object
(
[channel] => sample_ticket
[source] => stdClass Object
(
[from] => stdClass Object
(
)
[to] => stdClass Object
(
)
[rel] =>
)
)
[created_at] => 2015-04-22T08:30:29Z
[updated_at] => 2015-05-19T06:01:22Z
[type] => incident
[subject] => This is a sample ticket requested and submitted by you
[raw_subject] => This is a sample ticket requested and submitted by you
[description] => This is the first comment. Feel free to delete this sample ticket.
[priority] => high
[status] => closed
[recipient] =>
[requester_id] => 794599791
[submitter_id] => 794599791
[assignee_id] => 794599791
[organization_id] => 39742491
[group_id] => 24344491
[collaborator_ids] => Array
(
)
[forum_topic_id] =>
[problem_id] =>
[has_incidents] =>
[due_at] =>
[tags] => Array
(
[0] => sample
[1] => zendesk
)
[custom_fields] => Array
(
)
[satisfaction_rating] =>
[sharing_agreement_ids] => Array
(
)
[fields] => Array
(
)
[followup_ids] => Array
(
)
[brand_id] => 565681
)
[1] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/10.json
[id] => 10 //multiple object like [0]...
Thanks...Any help would be great..
When you need to access to array's key, use []. When you have object, use ->.
echo $obj->tickets[0]->subject; // returns first subject
echo $obj->tickets[0]->description; // returns first description
You can put it into foreach loop, of course to gain all subjects, etc.
It's STD object so use properties
$obj->tickets[0]->subject
$obj->tickets[0]->description
You can obviously loop tickets
foreach($obj->tickets as $ticket)
{
echo $ticket->subject;
echo $ticket->description
}
this is an std object.to get subject and description follow this
$obj->tickets[0]->subject;
$obj->tickets[0]->description;
if you feel better in array just make it array using this code
$array = get_object_vars($obj);
Here's my issue:
I have an object filled with arrays that look like this.
[376339] => Array
(
[0] => 1f422730-f54b-4e4d-9289-10258ce74446
[1] => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
I need to search another object to find objects with the matching IDs, like below. Is there a way of doing this without a foreach? There are SEVERAL and I would like to not have to loop over the entire object every time.
stdClass Object
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
stdClass Object
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
I need it to end up looking like this.
[376339] => Array
(
[0] => Array
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
[1] => Array
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
)
I'm not sure if this makes any sense, that's why I had my two inital outputs I need to have merged into one somehow. This is all coming from one huge json object and I'm just using json_decode($jsonStuff) to decode it.
Would this be easier if I added true in the decode function? If I could just search for it like I could in python, that would be neat. But as it is, I'm at a loss as to how to get the output I need.
Note: Input json CANNOT be changed, I have no affiliation with the people that created it.
First loop over your input array and create an array with the key as the id
$input = json_decode($json_input);
$output = array();
foreach($input as $obj){
$output[$obj->id] = $obj;
}
then you can build your other array by searching the id on the array key
$massive_search_array = array(376339 => array
(
0 => 1f422730-f54b-4e4d-9289-10258ce74446,
1 => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
);
$final_output = array();
foreach($massive_search_array as $index => $searches){
foreach($searches as $search){
if(isset($output[$search])){
$final_output[$index][] = $output[$search];
}
}
}
Here I have an output from a website using Soap
stdClass Object
(
[page] => 0
[items] => 3
[total] => 3
[saleItems] => stdClass Object
(
[saleItem] => Array
(
[0] => stdClass Object
(
[reviewState] => open
[trackingDate] => 2011-11-03T01:06:43.547+01:00
[modifiedDate] => 2011-11-03T01:06:43.677+01:00
[clickDate] => 2011-10-30T22:57:57.383+01:00
[adspace] => stdClass Object
(
[_] => Beslist.nl [id] => 1437603
)
[admedium] => stdClass Object
(
[_] => 001. Program logo
[id] => 535098
)
[program] => stdClass Object
(
[_] => Zavvi NL
[id] => 8991
)
[clickId] => 1565847253976339456
[clickInId] => 0
[amount] => 40.45
[commission] => 2.83
[currency] => EUR
[gpps] => stdClass Object
(
[gpp] => Array
(
[0] => stdClass Object
(
[_] => shoplink
[id] => zpar0
)
)
)
[trackingCategory] => stdClass Object
(
[_] => Default
[id] => 45181
)
[id] => 46a4f84a-ba9a-45b3-af86-da5f3ec29648
)
)
)
)
I want to have the data (with a foreach loop) from program, commission and gpp->_. I can get the data from program and commission like this:
foreach ($sales->saleItems->saleItem as $sale) {
$programma = $sale->program->_;
$commissie = $sale->commission;
}
Works like a charm. However I can't get the data from the gpp->_ (want to have shoplink as result). I currently have:
foreach ($sales->saleItems->saleItem->gpps->gpp as $tracking) {
echo $tracking->_;
}
I get the error "Trying to get property of non-object". I've tried lots if variations and can't get it to work. Think I'm really close. Anyone has a solution?
This should work
foreach ($sales->saleItems->saleItem as $sale) {
foreach($sale->gpps->gpp as $tracking) {
echo $tracking->_;
}
As saleItem is an array, you won't be able to use chaining on it.
i have a multidimensional array. the array is returned by parsing xml using curl. when curl gave me the output i converted the output into array using $array = (array) simplexml_load_string($query); and the $array is given below. Now i want to fetch this array using foreach loop and want everything from this array
Array
(
[Meta] => SimpleXMLElement Object
(
[Query] => php programming
[ResultOffset] => SimpleXMLElement Object
(
)
[NumResults] => 25
[TotalResults] => 36839
)
[Slideshow] => Array
(
[0] => SimpleXMLElement Object
(
[ID] => 1966058
[Title] => title here
[Description] => description here
[Status] => 2
[Username] =>usrname
[URL] => url here
[ThumbnailURL] => a url
[ThumbnailSmallURL] => a url
[Embed] => some embed code
)
[1] => SimpleXMLElement Object
(
[ID] => 1966058
[Title] => title here
[Description] => description here
[Status] => 2
[Username] =>usrname
[URL] => url here
[ThumbnailURL] => a url
[ThumbnailSmallURL] => a url
[Embed] => some embed code
)
and continue
You can retrieve meta information without using foreach:
echo $array['Meta']->Query;
echo $array['Meta']->NumResults;
and so on...
To fetch slideshows:
foreach($array['Slideshow'] as $slideshow)
{
echo $slideshow->ID;
echo $slideshow->Title;
//-- and so on...
}
If you want to retrieve the ID and Titles of each SimpleXMLElement Object, try this:
<?php
forach ($array['Slideshow'] as $simpleXMLelem) {
echo $simpleXMLelem->getId();
echo $simpleXMLelem->getTitle();
}