PHP, stdClass, select elements contains specifed element - php

I have an stClass object like this:
object(stdClass)#2 (6) {
[0]=>
object(stdClass)#44 (2) {
["uid"]=>
int(3232)
["type"]=>
string(7) "sibling"
}
[1]=>
object(stdClass)#43 (2) {
["uid"]=>
int(32323)
["type"]=>
string(7) "sibling"
}
[2]=>
object(stdClass)#42 (2) {
["uid"]=>
int(3213)
["type"]=>
string(10) "grandchild"
}
[3]=>
object(stdClass)#41 (3) {
["uid"]=>
int(-680411188)
["type"]=>
string(6) "parent"
}
[4]=>
object(stdClass)#40 (3) {
["uid"]=>
int(-580189276)
["type"]=>
string(6) "parent"
}
[5]=>
object(stdClass)#39 (2) {
["uid"]=>
int(3213)
["type"]=>
string(7) "sibling"
}
}
How can I get elements with specified value of type element?
For example, if I select "parent", I wanna get this:
object(stdClass)#2 (6) {
[3]=>
object(stdClass)#41 (3) {
["uid"]=>
int(-680411188)
["type"]=>
string(6) "parent"
}
[4]=>
object(stdClass)#40 (3) {
["uid"]=>
int(-580189276)
["type"]=>
string(6) "parent"
}
}
I know, how to write it with "foreach" and "if", but I hope that there is another way. Thanks

Your outer object is in fact, an array in disguise. You can convert it to a real array by typecasting:
$arr = (array)$obj;
Then you can use:
$filtered = array_filter(
$arr,
function($item) {
return $item->type == 'parent';
}
);
to get an array that contains only the objects you need.

Related

php access property of object in array

I have a php variable that I got from a _POST. var_dump shows this:
array(9) { [0]=> array(2) { ["age"]=> string(2) "62"
["amount"]=> string(5) "10878" } [1]=> array(2) { ["age"]=> string(2) "63"
["amount"]=> string(5) "10878" } [2]=> array(2) { ["age"]=> string(2) "64"
["amount"]=> string(5) "10878" } [3]=> array(2) { ["age"]=> string(2) "65"
["amount"]=> string(5) "10878" } [4]=> array(2) { ["age"]=> string(2) "66"
["amount"]=> string(5) "10878" } [5]=> array(2) { ["age"]=> string(2) "67"
["amount"]=> string(5) "28416" } [6]=> array(2) { ["age"]=> string(2) "68"
["amount"]=> string(5) "28416" } [7]=> array(2) { ["age"]=> string(2) "69"
["amount"]=> string(5) "28416" } [8]=> array(2) { ["age"]=> string(2) "70"
["amount"]=> string(5) "28416" } }
I loop through the array but can't get the properties to print:
for ($i=0; $i<count($incomeSched); $i++) {
$age = $incomeSched[$i]->age;
$amt = $incomeSched[$i]->amount;
echo "age=$age, amount=$amt<br>";
}
age and amount are blank:
age=, amount=
There's a difference between associative arrays and objects.
$incomeSched[$i]->age;
is what you'd do to access the property of an object. For an associative array you'd want
$incomeSched[$i]["age"]
You can cast an array as an object if need be:
$obj = (object)$incomeSched;
learn more here:
PHP - associative array as an object
As far as I remember ->age is object syntax. You need array syntax which would be ['age'].
for ($i=0; $i<count($incomeSched); $i++) {
$age = $incomeSched[$i]['age'];
$amt = $incomeSched[$i]['amount'];
echo "age=$age, amount=$amt<br>";
}

Flatten multi dimension array but maintain keys?

I have the following:
[6199]=>
array(12) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(4) "Cuba"
}
["slug"]=>
array(1) {
[0]=>
string(27) "cuabn-havana-habanos-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Dominican"
[1]=>
string(10) "Nicaraguan"
}
["slug"]=>
array(2) {
[0]=>
string(9) "dominican"
[1]=>
string(10) "nicaraguan"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
}
[6192]=>
array(11) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(9) "Nicaragua"
}
["slug"]=>
array(1) {
[0]=>
string(27) "nicaraguan-new-world-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Java"
[1]=>
string(10) "Nicaraguan"
}
["slug"]=>
array(2) {
[0]=>
string(9) "java"
[1]=>
string(10) "nicaraguan"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
}
and my expected output is:
array(12) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(4) "Cuba".
[1]=>
string(9) "Nicaragua"
}
["slug"]=>
array(1) {
[0]=>
string(27) "cuabn-havana-habanos-cigars",
[0]=>
string(27) "nicaraguan-new-world-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Dominican"
[1]=>
string(10) "Nicaraguan"
[2]=>
string(9) "Java"
}
["slug"]=>
array(2) {
[0]=>
string(9) "dominican"
[1]=>
string(10) "nicaraguan"
[3]=>
string(9) "java"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
See how it eliminates dupes and merges each array maintaining the "origin" key.
I've tried :
foreach ($resultterms as $keyname => $valuename){
foreach ($valuename as $keysub => $valuesub) {
foreach($valuesub['name'] as $keysubsub => $valuesubsub){
# code...
$prods_atts[$keysub]['name'][$keysubsub] = $valuesubsub;
$prods_atts[$keysub]['slug'][$keysubsub] = $valuesub['slug'][$keysubsub];
$prods_atts[$keysub]['id'][$keysubsub] = $valuesub['id'][$keysubsub];
}
}
}
where $resultterms is the original arrays but it's not working. I was wondering if there was a wonderful php function I could use to merge these instead of so many nested for each loops?
I believe you're just looking for array_merge_recursive.
call_user_func_array('array_merge_recursive', array_values($prod_atts));
call_user_func_array allows to transform an array into a list of arguments
array_values because in the end, you seem to want to get rid of the first layer of your array
In order to try it, could you post the var_export of your variable instead of the var_dump?
echo(var_export($prod_atts, true));
merge your array by any suggested method. After that you will get duplicated values. And you need save only the unique items
$new = array_merge_recursive($resultterms['6199'], $resultterms['6192']);
foreach($new['Origin'] as &$item) { $item = array_unique($item); }
foreach($new['Filler'] as &$item) { $item = array_unique($item); }

Adding an extra row to a PHP array with new values

I have this array called $items. My CMS created this array with three products and I just did a var_dump with the results below.
array(3) { [0]=> array(11) { ["item_id"]=> string(4) "1320" ["name"]=> string(5) "ITEM_A" ["price"]=> string(6) "$5.00" }
[1]=> array(11) { ["item_id"]=> string(4) "1321" ["name"]=> string(5) "ITEM_B" ["price"]=> string(6) "$5.00" }
[2]=> array(11) { ["item_id"]=> string(4) "1323" ["name"]=> string(5) "ITEM_D" ["price"]=> string(6) "$5.00" }
}
Is there a way I can inject another item, "ITEM_C" into this array so that it's now
array(3) { [0]=> array(11) { ["item_id"]=> string(4) "1320" ["name"]=> string(5) "ITEM_A" ["price"]=> string(6) "$5.00" }
[1]=> array(11) { ["item_id"]=> string(4) "1321" ["name"]=> string(5) "ITEM_B" ["price"]=> string(6) "$5.00" }
[2]=> array(11) { ["item_id"]=> string(4) "1323" ["name"]=> string(5) "ITEM_D" ["price"]=> string(6) "$5.00" }
[3]=> array(11) { ["item_id"]=> string(4) "1322" ["name"]=> string(5) "ITEM_C" ["price"]=> string(6) "$5.00" }
}
and then sort it by "name"? I basically want to always add one new row to this array which will always have different names and then sort it by the name. When I do the foreach php command I don't want it to be in the order of ITEM_A, ITEM_B, ITEM_D, ITEM_C. It needs to be ITEM_A, ITEM_B, ITEM_C, ITEM_D.
Appending to an array in PHP is easy as $array[] :
$myArray = array(1,2,3);
$myArray[] = 4; //now array(1,2,3,4)
So to add to your existing array, first create your new array element
$element = array("item_id" => 1322, "name" => "ITEM_C", /*etc.*/);
Then add it your array
$myArray[] = $element;
As for sorting, there are various sort functions depending on your exact needs. Since you're sorting according to a given array key, you'll probably need to call usort with a given function.
function nameCompare($a, $b)
{
$a = $a["name"];
$b = $b["name"];
return strcmp($a, $b);
}
usort($myArray, 'nameCompare');

Accesing JSON response data via PHP

I'm using the Paypal API PHP REST SDK. I got a response that looks like in JSON format but i'm unable to access the properties inside the object and array.
How do I access the "state" properties from this JSON response via PHP? The response is being wrapped by the object of Paypal\Api\Payment type. foreach looping returns NULL
var_dump($response) looks like below:
object(PayPal\Api\Payment)#8 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(8) {
["id"]=>
string(28) "PAY-8JC052XXXXKKZMQNQ"
["create_time"]=>
string(20) "2013-12-19T10:19:34Z"
["update_time"]=>
string(20) "2013-12-19T10:20:38Z"
["state"]=>
string(8) "approved"
["intent"]=>
string(4) "sale"
["payer"]=>
object(PayPal\Api\Payer)#33 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(2) {
["payment_method"]=>
string(6) "paypal"
["payer_info"]=>
object(PayPal\Api\PayerInfo)#30 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(5) {
["email"]=>
string(11) "some#email.com"
["first_name"]=>
string(6) "fname"
["last_name"]=>
string(5) "lname"
["payer_id"]=>
string(13) "UAGGF3392CUTG"
["shipping_address"]=>
object(PayPal\Api\Address)#31 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(5) {
["line1"]=>
string(26) "Address"
["city"]=>
string(13) "City"
["state"]=>
string(8) "State"
["postal_code"]=>
string(5) "000000"
["country_code"]=>
string(2) "US"
}
}
}
}
}
}
["transactions"]=>
array(1) {
[0]=>
object(PayPal\Api\Transaction)#34 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(4) {
["amount"]=>
object(PayPal\Api\Amount)#35 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["total"]=>
string(4) "1.00"
["currency"]=>
string(3) "USD"
["details"]=>
object(PayPal\Api\Details)#36 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["subtotal"]=>
string(4) "1.00"
}
}
}
}
["description"]=>
string(33) "Item name: 1"
["item_list"]=>
object(PayPal\Api\ItemList)#37 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["items"]=>
array(1) {
[0]=>
object(PayPal\Api\Item)#38 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(4) {
["name"]=>
string(20) "Item name"
["price"]=>
string(4) "1.00"
["currency"]=>
string(3) "USD"
["quantity"]=>
string(1) "1"
}
}
}
}
}
["related_resources"]=>
array(1) {
[0]=>
object(PayPal\Api\RelatedResources)#40 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["sale"]=>
object(PayPal\Api\Sale)#42 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(7) {
["id"]=>
string(17) "5DH04XXX63X"
["create_time"]=>
string(20) "2013-12-19T10:19:34Z"
["update_time"]=>
string(20) "2013-12-19T10:20:38Z"
["state"]=>
string(9) "completed"
["amount"]=>
object(PayPal\Api\Amount)#44 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(2) {
["total"]=>
string(4) "1.00"
["currency"]=>
string(3) "USD"
}
}
["parent_payment"]=>
string(28) "PAY-8JC05XXXXKZMQNQ"
["links"]=>
array(3) {
[0]=>
object(PayPal\Api\Links)#46 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(65) "https://api.sandbox.paypal.com/v1/payments/sale/5DHXX91763X"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
[1]=>
object(PayPal\Api\Links)#47 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(72) "https://api.sandbox.paypal.com/v1/payments/sale/5DHXXA691763X/refund"
["rel"]=>
string(6) "refund"
["method"]=>
string(4) "POST"
}
}
[2]=>
object(PayPal\Api\Links)#48 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8JC052914XX1034SKKZMQNQ"
["rel"]=>
string(14) "parent_payment"
["method"]=>
string(3) "GET"
}
}
}
}
}
}
}
}
}
}
}
["links"]=>
array(1) {
[0]=>
object(PayPal\Api\Links)#49 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8JC0XX914D601034SKKZMQNQ"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
}
}
}
I tried json_decode($response) but it returned NULL so i assumed that this is already the correct JSON format.
I tried echo $response->id and it returns blank
I've also tried multiple variations of foreach ($response->id as $value) { var_dump($value); } which also returns nothing
Help!
If you use only json_decode($result) it will not convert entire objects into an array.
So simply use
$result=json_decode($result, true, 512);
It will convert all the objects into associative array recursively.
Try it. It works for me.
It turns out that this is not a standard JSON format. For some reason the Paypal API SDK return it in their own "json" format through this line
$ret->fromJson($json);
return $ret;
I just skipped that and return $json instead and it gives me the format that i can put into json_decode for further processing.
return $json;
That took me 1 full freaking day! Pff...
You can also use
$response->toJSON();
then you can use
$result = json_decode($response);
echo ($result->state);
Convert json to a string then store the content in an array (decode it with json_decode).

Retrieve value from an Object / Multi-Dimensional Array Received from a Web Service

I am calling a web service, and receive this response which I can see using var_dump. How do I get the Name value? Also, what structure is this?
object(DescribeEntityTypeResponse)#2 (1) {
["DescribeEntityTypeResult"]=>
object(DescribeEntityTypeResult)#6 (1) {
["EntityTypes"]=>
object(ArrayOfEntityType)#7 (1) {
["EntityType"]=>
array(5) {
[0]=>
object(EntityType)#8 (3) {
["ID"]=>
int(0)
["Name"]=>
string(7) "Contact"
["Type"]=>
string(4) "Base"
}
[1]=>
object(EntityType)#9 (3) {
["ID"]=>
int(0)
["Name"]=>
string(7) "Company"
["Type"]=>
string(4) "Base"
}
[2]=>
object(EntityType)#10 (3) {
["ID"]=>
int(0)
["Name"]=>
string(8) "Prospect"
["Type"]=>
string(4) "Base"
}
[3]=>
object(EntityType)#11 (3) {
["ID"]=>
int(0)
["Name"]=>
string(7) "Visitor"
["Type"]=>
string(4) "Base"
}
[4]=>
object(EntityType)#12 (3) {
["ID"]=>
int(0)
["Name"]=>
string(17) "ProcessedActivity"
["Type"]=>
string(4) "Base"
}
}
}
}
}
I think this should work:
foreach($result->DescribeEntityTypeResult->EntityTypes->EntityType as $entityType) {
$name = $entityType['Name'];
}
However the objects may have methods() to access these properties.

Categories