I am receiving a PHP array from a webservice (soap xml).
From this array, I want to keep just a few tags and then convert it to json (see sample in the bottom).
See sample of the array I receive from webservice, below:
(*** marks the information I need in my final Json)
Array (
[0] => stdClass Object (
***[Id] => 445946
[SystemName] =>
[TrackingPixelUrl] => http://www.pix.se/picture.png
[UrlName] => Movie_Renoir_445946
***[Name] => Movie: Film
[ForeignProductId] =>
[PackageId] =>
[OrganizationId] => 1045
[Version] => 12
[TemplateId] => 81
***[Image] => stdClass Object (
[MediaId] => 41315620
[MediaType] => Image
[Url] => http://images.pix.se
[Width] => 214
[Height] => 317
[ProducedBy] =>
[CopyrightBy] =>
[ImageType] => Image
)
[Status] => Active
[PublishedDate] => 2014-11-03T10:23:09.617
[RevisionDate] =>
[ExpirationDate] => 2014-11-14T00:00:00
[Relevance] => Regional
[ParentProductId] =>
[ProductType] => Product
[IsHighlight] =>
[Attributes] => stdClass Object (
[AttributeData] => Array (
[0] => stdClass Object (
[Id] => 18895726
[AttributeId] => 125
[MetaType] => String
[Value] => stdClass Object (
***[Data] => http://www.Movie.se
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 1
[Language] => 1
)
[1] => stdClass Object (
[Id] => 18895762
[AttributeId] => 102
[MetaType] => String
[Value] => stdClass Object (
***[Data] => Text text info tinfo text Long text
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 2
[Language] => 1
)
[2] => stdClass Object (
[Id] => 20562011
[AttributeId] => 106
[MetaType] => String
[Value] => stdClass Object (
***[Data] => 70 SEK
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 3
[Language] => 1
)
[3] => stdClass Object (
[Id] => 27970127
[AttributeId] => 99
[MetaType] => String
[Value] => stdClass Object (
[Data] => Movie: Film
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 11
[Language] => 1
)
[4] => stdClass Object (
[Id] => 27970128
[AttributeId] => 101
[MetaType] => String
[Value] => stdClass Object (
***[Data] => Short info text text text
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 11
[Language] => 1
)
[5] => stdClass Object (
[Id] => 18895724
[AttributeId] => 147
[MetaType] => String
[Value] => stdClass Object (
[Data] => +46
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 2
[Language] => 0
)
[6] => stdClass Object (
[Id] => 18895725
[AttributeId] => 145
[MetaType] => String
[Value] => stdClass Object (
***[Data] => (
0410-1111111
)
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 2
[Language] => 0
)
[7] => stdClass Object (
[Id] => 18895728
[AttributeId] => 526
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 1
[Language] => 0
)
[8] => stdClass Object (
[Id] => 27970129
[AttributeId] => 115
[MetaType] => Media
[Value] => stdClass Object (
[MediaList] => stdClass Object (
[MediaObject] => stdClass Object (
[MediaId] => 41315620
[MediaType] => Image
[Url] => http://images.pix.se
[Width] => 214
[Height] => 317
[ProducedBy] =>
[CopyrightBy] =>
[ImageType] => Image
)
)
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 10
[Language] => 0
)
[9] => stdClass Object (
[Id] => 27970130
[AttributeId] => 418
[MetaType] => MultiAttribute
[Value] => stdClass Object (
[Values] => stdClass Object (
[MultiAttributeObject] => Array (
[0] => stdClass Object (
[Id] => 0
[AttributeId] => 0
[ProductId] => 0
[OrganizationId] => 0
[MultiAttributeId] => 631
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[Language] => 0
)
[1] => stdClass Object (
[Id] => 0
[AttributeId] => 0
[ProductId] => 0
[OrganizationId] => 0
[MultiAttributeId] => 36
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[Language] => 0
)
[2] => stdClass Object (
[Id] => 0
[AttributeId] => 0
[ProductId] => 0
[OrganizationId] => 0
[MultiAttributeId] => 2030
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[Language] => 0
)
)
)
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 4
[Language] => 0
)
[10] => stdClass Object (
[Id] => 27970131
[AttributeId] => 668
[MetaType] => MultiAttribute
[Value] => stdClass Object (
[Values] => stdClass Object (
[MultiAttributeObject] => Array (
[0] => stdClass Object (
[Id] => 0
[AttributeId] => 668
[ProductId] => 445946
[OrganizationId] => 1045
[MultiAttributeId] => 2022
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[Language] => 0
)
[1] => stdClass Object (
[Id] => 0
[AttributeId] => 668
[ProductId] => 445946
[OrganizationId] => 1045
[MultiAttributeId] => 2023
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[Language] => 0
)
[2] => stdClass Object (
[Id] => 0
[AttributeId] => 668
[ProductId] => 445946
[OrganizationId] => 1045
[MultiAttributeId] => 2174
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[Language] => 0
)
)
)
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 5
[Language] => 0
)
)
)
[Categories] => stdClass Object (
[Category] => stdClass Object (
[Id] => 16015
[OrganizationId] => 1045
[Name] => Film
[DefaultTemplateId] => 81
[Status] => Active
[ParentCategoryId] => 3568
[DisplayOrder] => 99
[Icon] =>
)
)
[GeoNode] => stdClass Object (
[Id] => 3558
[Name] => Staden med omnejd
[ParentId] => 66876
[OrgId] => 1045
[Type] => Undefined
[LeftValue] => 11
[RightValue] => 12
[CityCenterLatitude] =>
[CityCenterLongitude] =>
[CityCenterRadius] =>
)
[Occasions] => stdClass Object (
[OccasionObject] => stdClass Object (
[Id] => 19836976
[AttributeId] => 111
[ProductId] => 445946
[OrganizationId] => 1045
***[StartDate] => 2014-11-12T00:00:00
***[EndDate] => 2014-11-13T00:00:00
***[StartTime] => 0001-01-01T19:00:00
[EndTime] => 0001-01-01T19:00:00
***[ValidDays] => Wednesday Thursday
***[ArenaId] => 75316
***[ArenaName] => Moviegrafteatern Movie, Staden
[EntryTime] => 0001-01-01T00:00:00
)
)
[SupplierId] =>
[Duration] =>
[ForeignProductIds] => stdClass Object (
)
[LanguageId] => 1
)
)
I would like the final, cropped, json to look like this:
{
"Id":445946,
"Name": "Movie: Film",
"Image":{
"Url":"http://images.pix.se",
"Width":595,
"Height":709
},
"MovieUrl": "http://www.Movie.se",
"LongText": "Text text info tinfo text Long text",
"Price": "70 SEK",
"ShortText": "Short info text text text",
"Phone": "0410-1111111",
"StartDate": 2014-11-12T00:00:00,
"EndDate": 2014-11-13T00:00:00,
"StartTime": 0001-01-01T19:00:00,
"ValidDays": "Wednesday Thursday",
"ArenaId": 75316,
"ArenaName": "Moviegrafteatern Movie, Staden"
}
How can I convert this array to a much smaller JSON?
I think the only way to get this solved is an exakt mapping of your the items you need in a function an then loop through the different objects, because the structure you get with this lot of different objects is quite crappy.
function cleanUpResult(array)
{
$r = array();
foreach($item in $array){
$result = array();
$result["Id"] = $item["Id"];
$result["Name"] = $item["Name"];
$result["Image"] = array();
$result["Image"]["Url"] =$item["Image"]->Url;
$result["Image"]["Width"] =$item["Id"]->Width;
$result["Image"]["Height"] =$item["Id"]->Height;
$result["MovieUrl"] = $item["Attributes"]->AttributeData[0]->Value->Data;
$result["LongText"] = $item["Attributes"]->AttributeData[1]->Value->Data;
$result["Price"] = $item["Attributes"]->AttributeData[2]->Value->Data;;
$result["ShortText"] = $item["Attributes"]->AttributeData[4]->Value->Data;
$result["Phone"] = $item["Attributes"]->AttributeData[6]->Value->Data;
$result["StartDate"] = $item["StartDate"];
$result["EndDate"] = $item["EndDate"];
$result["StartTime"] = $item["StartTime"];
$result["ValidDays"] = $item["ValidDays"];
$result["ArenaId"] = $item["ArenaId"];
$result["ArenaName"] = $item["ArenaName"];
$r[] = $result;
}
return json_encode($r);
}
This isn't complicated, but since you want various fields from within a nested array, the simplest approach is to just iterate over the array and grab the individual fields you want to encode. I'm not sure it'll be worth the time to construct a clever filter.
For example, let's say $soapArray contains the information you provided above:
$data = array();
// iterate over the full field list
foreach ($soapArray as $soapElement) {
// construct a partial array containing the fields you want
$data[] = array(
'Id' => $soapElement['Id'],
'Name' => $soapElement['Name'],
'Image' => array(
'Url' => $soapElement['Image']->Url,
'Width' => $soapElement['Image']->Width,
'Height' => $soapElement['Image']->Height,
),
'MovieUrl' => $soapElement['Attributes']->AttributeData[0]->Value->Data,
'LongText' => $soapElement['Attributes']->AttributeData[1]->Value->Data,
);
}
// display the encoded array
echo json_encode($data);
Simply create a list of the array keys you want to white list, then remove the rest.
Assume your object is $object
//this line must come before any output is made. (echo, print, etc.)
header('Content-Type: application/json');
//whitelist. the items you want to keep
$keep = array("id", "name", "image" ...);
foreach($object as $object) {
foreach ($object as $key => $product) {
if( !in_array($key, $keep) ) { //check if the current loop key is not in the whitelist
unset($object[$key]);
}
}
}
//Then convert the array to JSon.
echo json_encode($object)
Then $object will hold only what was in the whitelist
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
i have data in json something like that:
stdClass Object
(
[contacts] => stdClass Object
(
[14] => stdClass Object
(
[data] => stdClass Object
(
[email] => veer#gmail.com
[first_name] => veer
[last_name] =>
[user_id] => 16
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 14
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => veer
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 92
[1] => 13
[2] => 12
[3] => 9
)
[files] => Array
(
)
)
[9] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.pratibha1432#gmail.com
[first_name] => Pratibha
[last_name] => Singh
[user_id] => 8
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 9
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => pratibha
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 93
[2] => 92
[3] => 91
[4] => 82
[5] => 15
[6] => 14
[7] => 13
[8] => 9
)
[files] => Array
(
)
)
[4] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.dev1432#gmail.com
[first_name] => Devesh
[last_name] => Singh
[user_id] => 7
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 4
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => devesh
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 93
[1] => 92
[2] => 15
[3] => 12
[4] => 11
[5] => 9
)
[files] => Array
(
)
)
[2] => stdClass Object
(
[data] => stdClass Object
(
[email] => admin#gmail.com
[first_name] => veronica
[last_name] =>
[user_id] => 1
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 2
[gravatar] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => admin
[profile_picture] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 92
[2] => 15
[3] => 14
[4] => 13
[5] => 9
)
[files] => Array
(
)
)
)
[status] => success
)
and now I am trying to fetch "email" from the contacts->id->data->email so for that i am using this code. I am trying to loop the things by which i can fetch email from all the ids present. and in the ids section, there is data and inside data the email is present so how can i fetch all emails from all the ids.
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails = array('email'=>$value->email);
return $emails;
}
}
but it return only single data:
Array
(
[email] => veer#gmail.com
)
i want to fetch all the email like that:
Array
(
[0] => Array
(
[emails] => veer#gmail.com
)
[1] => Array
(
[emails] => singh.pratibha1432#gmail.com
)
[2] => Array
(
[emails] => singh.dev1432#gmail.com
)
[3] => Array
(
[emails] => admin#gmail.com
)
)
how can I achieve this?
Define null array and in foreach run store every email in that variable.
$emails = array();
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails[] = array('emails'=>$contact->data->email;);
}
}
return $emails;
stdClass Object ( [Order] => stdClass Object
( [id] => c62f5d272ghjjgd346lmn34
[type] => [status] => placed
[keys_status] => Keys were generated )
[Products] => Array ( [0] => stdClass Object ( [name] => Mobile Security
[devices] => 1 [months] => 12 [quantity] => 1 [msrp_price] => 9 [discount] => 0.0 [end_user_price] => 9 [reseller_margin] => 0.0 [reseller_price] => 9.01[partner_margin] => 90 [partner_price] => 0.9[Keys] => Array ( [0] => stdClass Object ( [product_id] => 21 [product_name] => Mobile Security [key] => 1234) ) ) ) [Customer] => stdClass Object ( [name] => IPPS [phone] => 25667587576 [address] => Bishop willis Street [contact_person] => Mukama Gordon [city] => Kigali [zipcode] => 250 [country_id] => 202 [state_id] => [email] => mukamatest#test.com ) [Keys] => Array ( [0] => stdClass Object ( [product_id] => 21 [product_name] => Mobile Security [key] => 1234 ) ) )
This is a really hacky way of doing it, but try
$arrayversion = json_decode(json_encode($objectversion), true);
Then
print_r($arrayversion); will be an array.
I download the ShippingGroundFreightWebService PHP examples from UPS.com. I got following error message:
SoapFault Object
(
[message:protected] => An exception has been raised as a result of client data.
[string:private] =>
[code:protected] => 0
[file:protected] => /home5/vizparts/public_html/PHP/SoapGroundFreightShipClient.php
[line:protected] => 193
[trace:private] => Array
(
[0] => Array
(
[file] => /home5/vizparts/public_html/PHP/SoapGroundFreightShipClient.php
[line] => 193
[function] => __soapCall
[class] => SoapClient
[type] => ->
[args] => Array
(
[0] => ProcessShipment
[1] => Array
(
[0] => Array
(
[Request] => Array
(
[RequestOption] => Array
(
[0] => 1
[1] => Shipping
)
)
[Shipment] => Array
(
[ShipFrom] => Array
(
[Name] => Pat Stewart
[TaxIdentification] => 1234567890
[Address] => Array
(
[AddressLine] => 2311 York Road
[City] => Timonium
[StateProvinceCode] => MD
[PostalCode] => 21093
[CountryCode] => US
)
[AttentionName] => String
[Phone] => Array
(
[Number] => 6785851000
[Extension] => 123
)
)
[ShipperNumber] => 222006
[ShipTo] => Array
(
[Name] => Superman
[Address] => Array
(
[AddressLine] => 2010 Warsaw Road
[StateProvinceCode] => GA
[PostalCode] => 30076
[CountryCode] => US
[City] => Roswell
)
[AttentionName] => String
[Phone] => Array
(
[Number] => 6785851000
[Extention] => 111
)
)
[PaymentInformation] => Array
(
[Payer] => Array
(
[Name] => Superman
[Address] => Array
(
[AddressLine] => 2010 Warsaw Road
[City] => Roswell
[StateProvinceCode] => GA
[PostalCode] => 30075
[CountryCode] => US
)
[ShipperNumber] => 00613270
[AttentionName] => String
[Phone] => Array
(
[Number] => 6785851000
)
)
[ShipmentBillingOption] => Array
(
[Code] => 10
[Description] => String
)
)
[Service] => Array
(
[Code] => 308
[Description] => String
)
[HandlingUnitOne] => Array
(
[Quantity] => 16
[Type] => Array
(
[Code] => PLT
[Description] => String
)
)
[Commodity] => Array
(
[CommodityID] => 22
[Description] => BUGS
[Weight] => Array
(
[UnitOfMeasurement] => Array
(
[Code] => LBS
[Description] => String
)
[Value] => 511.25
)
[Dimensions] => Array
(
[UnitOfMeasurement] => Array
(
[Code] => IN
[Description] => String
)
[Length] => 1.25
[Width] => 1.2
[Height] => 5
)
[NumberOfPieces] => 1
[PackagingType] => Array
(
[Code] => PLT
[Description] => String
)
[CommodityValue] => Array
(
[CurrencyCode] => USD
[MonetaryValue] => 265.2
)
[FreightClass] => 60
[NMFCCommodityCode] => 566
)
[Reference] => Array
(
[Number] => Array
(
[Code] => PM
[Value] => 1651651616
)
[BarCodeIndicator] => Array
(
[NumberOfCartons] => 5
[Weight] => Array
(
[UnitOfMeasurement] => Array
(
[Code] => LBS
[Description] => String
)
[Value] => 2
)
)
)
)
)
)
)
)
)
[faultstring] => An exception has been raised as a result of client data.
[faultcode] => Client
[faultcodens] => http://schemas.xmlsoap.org/soap/envelope/
[detail] => stdClass Object
(
[Errors] => stdClass Object
(
[ErrorDetail] => stdClass Object
(
[Severity] => Hard
[PrimaryErrorCode] => stdClass Object
(
[Code] => 9121000
[Description] => Invalid Ship Request Document
)
)
)
)
)
I want to integrate the UPS API into my own website. I want to print the shipping label. How to fix this problem?
you should set the request xsd file,find it in other folder in the package you download.then set the request file with it.
I'm trying to search youtube and grab the videos video ID and title.
In the bellow example I am searching for "youtube":
<?php
$url="http://gdata.youtube.com/feeds/api/videos?q='youtube'&format=5&max-results=2&v=2&alt=jsonc";
$json = file_get_contents($url,0,null,null);
$json_output = json_decode($json);
echo '<pre>';
print_r("query results:");
print_r($json_output);
'</pre>';
foreach ( $json_output->data as $data ){
echo "{$data->id}";
echo "{$data->title}";
}
?>
Here is the json output for the above query:
query results:stdClass Object
(
[apiVersion] => 2.1
[data] => stdClass Object
(
[updated] => 2012-03-04T20:19:06.314Z
[totalItems] => 1000000
[startIndex] => 1
[itemsPerPage] => 2
[items] => Array
(
[0] => stdClass Object
(
[id] => IpSYwvzKukI
[uploaded] => 2009-03-14T08:17:36.000Z
[updated] => 2012-02-29T10:51:35.000Z
[uploader] => kenbedict009
[category] => Music
[title] => one of the funniest kid in youtube!!
[description] => he is a 3 years old korean,this kid makes me laugh
[tags] => Array
(
[0] => rin on the rox
[1] => charice pempengco
[2] => joeydiamond
)
[thumbnail] => stdClass Object
(
[sqDefault] => http://i.ytimg.com/vi/IpSYwvzKukI/default.jpg
[hqDefault] => http://i.ytimg.com/vi/IpSYwvzKukI/hqdefault.jpg
)
[player] => stdClass Object
(
[default] => http://www.youtube.com/watch?v=IpSYwvzKukI&feature=youtube_gdata_player
[mobile] => http://m.youtube.com/details?v=IpSYwvzKukI
)
[content] => stdClass Object
(
[5] => http://www.youtube.com/v/IpSYwvzKukI?version=3&f=videos&app=youtube_gdata
[1] => rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQlCusr8wpiUIhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
[6] => rtsp://v8.cache2.c.youtube.com/CiILENy73wIaGQlCusr8wpiUIhMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
)
[duration] => 344
[rating] => 4.6304307
[likeCount] => 23419
[ratingCount] => 25803
[viewCount] => 9848083
[favoriteCount] => 15390
[commentCount] => 16074
[accessControl] => stdClass Object
(
[comment] => allowed
[commentVote] => allowed
[videoRespond] => moderated
[rate] => allowed
[embed] => allowed
[list] => allowed
[autoPlay] => allowed
[syndicate] => allowed
)
)
[1] => stdClass Object
(
[id] => PPNMGYOm1aM
[uploaded] => 2011-06-23T16:35:33.000Z
[updated] => 2012-02-29T13:15:47.000Z
[uploader] => shakeitupvevo
[category] => Music
[title] => "Watch Me" from Disney Channel's "Shake It Up"
[description] => To download Watch Me visit www.smarturl.it Performed by Bella Thorne and Zendaya
[tags] => Array
(
[0] => Bella
[1] => Thorne
[2] => Zendaya
[3] => Cast
[4] => of
[5] => Shake
[6] => It
[7] => Up:
[8] => Break
[9] => Down
[10] => Watch
[11] => Me
[12] => Walt
[13] => Disney
[14] => Soundtrack
)
[thumbnail] => stdClass Object
(
[sqDefault] => http://i.ytimg.com/vi/PPNMGYOm1aM/default.jpg
[hqDefault] => http://i.ytimg.com/vi/PPNMGYOm1aM/hqdefault.jpg
)
[player] => stdClass Object
(
[default] => http://www.youtube.com/watch?v=PPNMGYOm1aM&feature=youtube_gdata_player
)
[content] => stdClass Object
(
[5] => http://www.youtube.com/v/PPNMGYOm1aM?version=3&f=videos&app=youtube_gdata
)
[duration] => 193
[aspectRatio] => widescreen
[rating] => 4.7312055
[likeCount] => 145059
[ratingCount] => 155509
[viewCount] => 48201555
[favoriteCount] => 69981
[commentCount] => 81938
[status] => stdClass Object
(
[value] => restricted
[reason] => limitedSyndication
)
[restrictions] => Array
(
[0] => stdClass Object
(
[type] => country
[relationship] => deny
[countries] => DE
)
)
[accessControl] => stdClass Object
(
[comment] => allowed
[commentVote] => allowed
[videoRespond] => allowed
[rate] => allowed
[embed] => allowed
[list] => allowed
[autoPlay] => allowed
[syndicate] => allowed
)
)
)
)
)
Im trying to echo out [id] and [title]. Any idea what I am doing wrong?
Thanks!
Change line 9 to:
foreach ( $json_output->data->items as $data ){
You should get first items as an Objects Array:
$items = $json -> data -> items;
So try foreach now:
foreach ( $items as $item ){
echo "{$item->id}";
echo "{$item->title}";
}
I hope it's clear now.
I've got an array of objects as follows ; how can I filter out (remove) the objects that have admins->member = 11 ?
In this example, the resulting array object would only have the 1st object that [245] at the start.
Array
(
[245] => stdClass Object
(
[name] => Programmation Web
[description] =>
[public] => 0
[jointype] => controlled
[grouptype] => course
[membershiptype] => member
[reason] =>
[role] => tutor
[ctime] => 2011-10-12 14:41:35
[mtime] => 0000-00-00 00:00:00
[image] =>
[session_id] => 28
[session_name] => Hiver 2012
[membercount] => 1
[favorite] =>
[requests] => 0
[id] => 245
[members] => Array
(
[0] => stdClass Object
(
[id] => 11
[name] => Yves Otis (otisyves)
)
)
[projects] => Array
(
[0] => stdClass Object
(
[id] => 1923
[title] => Sans titre (1)
[description] =>
[owner] => 11
[ownerformat] =>
[group] =>
[institution] =>
[startdate] =>
[stopdate] =>
[ctime] => 2011-10-12 13:24:26
[mtime] => 2011-10-12 13:24:26
[atime] => 2011-10-12 13:24:26
[submittedgroup] => 245
[submittedhost] =>
[submittedtime] => 2011-10-12 17:00:41
[numcolumns] => 3
[layout] =>
[theme] =>
[template] => 0
[copynewuser] => 0
[type] => portfolio
[visits] => 0
[allowcomments] => 1
[approvecomments] => 0
[skills] =>
[instructions] =>
[accessconf] =>
[image] =>
[competencesvisees] =>
[consignes] =>
[fichierconsignes] =>
[groupevise] =>
)
)
[project_count] => 1
[admins] => Array
(
[0] => stdClass Object
(
[member] => 11
[firstname] => Yves
[lastname] => Otis
)
)
[topic_name] => Le PHP
[activites] => Array
(
[0] => stdClass Object
(
[topic_name] =>
[topic_id] => 42
[post_parent] => 107
[post_body] => Oui moi aussi je me demande ça.
[post_id] => 109
)
)
[forums] => Array
(
[0] => stdClass Object
(
[forum_name] => Discussion générale
[forum_id] => 101
)
)
)
[246] => stdClass Object
(
[name] => Les bases de données
[description] =>
[public] => 0
[jointype] => controlled
[grouptype] => course
[membershiptype] => admin
[reason] =>
[role] => admin
[ctime] => 2011-10-13 15:27:30
[mtime] => 0000-00-00 00:00:00
[image] =>
[session_id] => 27
[session_name] => Automne 2011
[membercount] => 0
[favorite] =>
[requests] => 0
[id] => 246
[project_count] => 0
[topic_name] => Difficulté
[activites] => Array
(
[0] => stdClass Object
(
[topic_name] =>
[topic_id] => 44
[post_parent] => 111
[post_body] => Ouah!
[post_id] => 112
)
)
[forums] => Array
(
[0] => stdClass Object
(
[forum_name] => Le MySQL
[forum_id] => 103
)
)
)
)
I'd like to check against the index
$arr = array_filter($arr, function($item) {
if (!isset($item->admins) || !is_array($item->admins)) {
return false;
}
foreach ($item->admins as $admin) {
if ($admin->member == 11) {
return true;
}
}
return false;
});