parsing xml array not displaying all data in view - php

Im trying to display Ebay GetSingleItem response (http://developer.ebay.com/devzone/shopping/docs/callref/GetSingleItem.html) and Im having problems displaying array ["ItemSpecifics"]. The call is working ok and I´m receiving the following information:
["ItemSpecifics"] => object(DTS\eBaySDK\Shopping\Types\NameValueListArrayType)#208 (2) {
["values":"DTS\eBaySDK\Types\BaseType":private] => array(1) {
["NameValueList"] => object(DTS\eBaySDK\Types\UnboundType)#129 (5) {
["data":"DTS\eBaySDK\Types\UnboundType":private] => array(8) {
[0] => object(DTS\eBaySDK\Shopping\Types\NameValueListType)#207 (2) {
["values":"DTS\eBaySDK\Types\BaseType":private] => array(2) {
["Name"] => string(6) "Format"
["Value"] => object(DTS\eBaySDK\Types\UnboundType)#191 (5) {
["data":"DTS\eBaySDK\Types\UnboundType":private] => array(1) {
[0] => string(10) "DVD/HD-DVD"
}
["position":"DTS\eBaySDK\Types\UnboundType":private] => int(0)
["class":"DTS\eBaySDK\Types\UnboundType":private] => string(44) "DTS\eBaySDK\Shopping\Types\NameValueListType"
["property":"DTS\eBaySDK\Types\UnboundType":private] => string(5) "Value"
["expectedType":"DTS\eBaySDK\Types\UnboundType":private] => string(6) "string"
}
}
["attachment":"DTS\eBaySDK\Types\BaseType":private] => array(2) {
["data"] => NULL
["mimeType"] => NULL
}
}
[1] => object(DTS\eBaySDK\Shopping\Types\NameValueListType)#65 (2) {
["values":"DTS\eBaySDK\Types\BaseType":private] => array(2) {
["Name"] => string(5) "Genre"
["Value"] => object(DTS\eBaySDK\Types\UnboundType)#152 (5) {
["data":"DTS\eBaySDK\Types\UnboundType":private] => array(1) {
[0] => string(16) "Sci-Fi & Fantasy"
}
["position":"DTS\eBaySDK\Types\UnboundType":private] => int(0)
["class":"DTS\eBaySDK\Types\UnboundType":private] => string(44) "DTS\eBaySDK\Shopping\Types\NameValueListType"
["property":"DTS\eBaySDK\Types\UnboundType":private] => string(5) "Value"
["expectedType":"DTS\eBaySDK\Types\UnboundType":private] => string(6) "string"
}
}
["attachment":"DTS\eBaySDK\Types\BaseType":private] => array(2) {
["data"] => NULL
["mimeType"] => NULL
}
}
[2] => object(DTS\eBaySDK\Shopping\Types\NameValueListType)#180 (2) {
["values":"DTS\eBaySDK\Types\BaseType":private] => array(2) {
["Name"] => string(7) "Edition"
["Value"] => object(DTS\eBaySDK\Types\UnboundType)#253 (5) {
["data":"DTS\eBaySDK\Types\UnboundType":private] => array(1) {
[0] => string(10) "Widescreen"
}
["position":"DTS\eBaySDK\Types\UnboundType":private] => int(0)
["class":"DTS\eBaySDK\Types\UnboundType":private] => string(44) "DTS\eBaySDK\Shopping\Types\NameValueListType"
["property":"DTS\eBaySDK\Types\UnboundType":private] => string(5) "Value"
["expectedType":"DTS\eBaySDK\Types\UnboundType":private] => string(6) "string"
}
So in my controller I put the following information:
$event = array();
foreach($item->ItemSpecifics->NameValueList as $data){
$event[] = $data;
}
I set the data using: ->setCollection($event)
and in my html page (view) I used:
<?php $res = $this->getCollection() ?>
<?php foreach ($res as $row)
{
echo "<tr id='0' class='0'>
<td>
<div class='name'>" . $row->Name . " </div>
</td>
<td>
<div class='value'>" . $row->Value . " </div>
</td>
</tr>";
} ?>
but my problem is that the data included in NAME only is retrieved. the information in Value is not retrieved into the view. just to put more in clear with an example. The information = FORMAT is displayed but DVD/HD-DVD is not.
["Name"] => string(6) "Format"
["Value"] => object(DTS\eBaySDK\Types\UnboundType)#191 (5) {
["data":"DTS\eBaySDK\Types\UnboundType":private] => array(1) {
[0] => string(10) "DVD/HD-DVD"
}

->Value is an object, and the ->Value->data property is marked as private. That means you cannot read it from outside (without doing reflection) but instead need to call a relevant method to read its value. Looking at the implementation of UnboundType we see that you should be able to read the ->Value object as if it was an array (they implement the ArrayAccess interface). The data you want is in position 0 so try this:
$row->Value[0]

Related

PHP Echo from multidimensional arrays

I'm trying to echo the "name" variable from this array.
array(2) {
["error"] => bool(false)["response"] => array(8) {
["id"] => int(560277)["name"] => string(7)
"Jeff" ["avatar"] => string(55)
"https://etc.etc" ["joinDate"] => string(19)
"2015-09-21 16:47:53" ["steamID64"] => int(76521228272726998)
["groupName"] => string(6)
"Player" ["groupID"] => int(1)["permissions"] => array(2) {
["isGameAdmin"] => bool(false)["showDetailedOnWebMaps"] => bool(false)
}
}
}
I'm not sure how to access it, I've tried:
json->response->name;, but this won't work because this is an array not an object and json[0]["response"]["name"];
EDIT:
The array is coming from a json API, here is an example. https://api.truckersmp.com/v2/player/2
I am then converting that using
$json = (json_decode($tmpServer, true, JSON_BIGINT_AS_STRING));
i got it working on php fiddle like this
$arr = file_get_contents("https://api.truckersmp.com/v2/player/2");
$decoded = json_decode($arr,true);
echo $decoded["response"]["name"];

Square Connect - OrdersApi Error EXPECTED_ARRAY when field was sent as an array

I have been working with the square-connect api, and I am having issues trying to create an order. I have followed the docs for creating an order. Below is the code in question.
$api = new \SquareConnect\Api\OrdersApi();
$location = $this->get_location();
if (!empty($items)) {
if (is_string($items)) {
$items = json_decode($items);
}
// print_r($items);
if (!is_array($items)) {
// echo "Items is not an array -- exiting now";
return false;
}
$order_data = [];
$order_data['idempotency_key'] = uniqid();
$order_data['line_items'] = [];
foreach ($items as $key => $li) {
$order_data['line_items'][] = [
'name' => $li->name,
'base_price_money' => ['amount' => ($li->price * 100)],
'quantity' => $li->qty
];
}
$order_data['taxes'] = [
'type' => 'ADDITIVE',
'name' => 'State Sales Tax',
'percentage' => '7'
];
print_r($order_data);
// exit;
$apiResponse = $api->createOrder($location, new \SquareConnect\Model\CreateOrderRequest($order_data));
$order = $apiResponse->getOrder();
print_r($apiResponse);
print_r($order);
}
When I make the request I get the following error
Message: [HTTP/1.1 400 Bad Request] {"errors":[{"category":"INVALID_REQUEST_ERROR","code":"EXPECTED_ARRAY","detail":"Expected an array.","field":"taxes"}]}
I have checked that the taxes field is an array
["taxes"]=>
array(3) {
["type"]=>
string(8) "ADDITIVE"
["name"]=>
string(15) "State Sales Tax"
["percentage"]=>
string(1) "7"
}
Any assistance would be greatly appreciated
EDIT: print_r($items) output
Array
(
[0] => stdClass Object
(
[id] => 24
[square_id] => L7PCKMIEDQFDR34IZ3E3VIDO
[variation_id] => SUJCAPZJQLM7VGAILP66NMMM
[variation_name] => Regular
[qty] => 1
[price] => 3.99
[name] => Ham & Cheese
)
[1] => stdClass Object
(
[id] => 4
[square_id] => 37RNDFXRVJPLR4UT7UXS5QOO
[variation_id] => 27Z5H3V6ZRHW2X5LVNGXOARY
[variation_name] => Starbucks DOUBLESHOT Expresso
[qty] => 1
[price] => 2.5
[name] => Starbucks DOUBLESHOT Expresso
)
)
var_dump(order_data)...
array(3) {
["idempotency_key"]=>
string(13) "59c138b7a67e2"
["line_items"]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(12) "Ham & Cheese"
["base_price_money"]=>
array(1) {
["amount"]=>
float(399)
}
["quantity"]=>
string(1) "1"
}
[1]=>
array(3) {
["name"]=>
string(29) "Starbucks DOUBLESHOT Expresso"
["base_price_money"]=>
array(1) {
["amount"]=>
float(250)
}
["quantity"]=>
string(1) "1"
}
}
["taxes"]=>
array(3) {
["type"]=>
string(8) "ADDITIVE"
["name"]=>
string(15) "State Sales Tax"
["percentage"]=>
string(1) "7"
}
}
The issue here is that you are expected to give an array of tax objects, and it is confusing because you are using the implicit object creation capability of the SDK with PHP arrays. in JSON, your taxes would look like:
"taxes":[
{
"type":"additive"
...
}
]
(note that is is an array of objects), you are providing:
"taxes":{
"type":"additive"
...
}
So if you want to just make a quick change to your code, you should do something like:
$order_data['taxes'] =array(array(
'type' => 'ADDITIVE',
'name' => 'State Sales Tax',
'percentage' => '7'
));
If you wanted to be more verbose in your code you could do something like:
$order_data = new \SquareConnect\Model\CreateOrderRequest();
$taxes = \SquareConnect\Model\CreateOrderRequestTax();
$taxes->setType('ADDITIVE');
...
$order_data->setTaxes($taxes);
Clearly the implicit array way is a bit easier, but it can be confusing in PHP. Does that help?

dynanmo db filterexpression with multiple condition php

I am unable to retrieve the item using filterExpression using logical and or or in Dyanamodb php using aws sdk. can any any one provide the code with the correct syntax to retrieve the item satisfying both the condition.
$scan_response = $dynamodb->scan(array(
'TableName' => $tableName,
'ExpressionAttributeValues' => [
':val1' => ['S' => '20'],
':val2' => ['S' => 'ajay'],
'FilterExpression' => 'age = :val1 AND name = :val2'
]));
i am getting error that
filterExpression must be a associative array
Thanks in advance
try this
$scan_response = $dynamodb->scan(array(
'TableName' => $tableName,
'ExpressionAttributeValues' => [
':val1' => ['S' => '20'],
':val2' => ['S' => 'ajay']],
'FilterExpression' => 'age = :val1 AND name = :val2'
));
badly placed closing ] creates
array(2) {
["TableName"]=>
string(3) "..."
["ExpressionAttributeValues"]=>
array(3) {
[":val1"]=>
array(1) {
["S"]=>
string(2) "20"
}
[":val2"]=>
array(1) {
["S"]=>
string(4) "ajay"
}
["FilterExpression"]=>
string(28) "age = :val1 AND name = :val2"
}
}
instead of required
array(3) {
["TableName"]=>
string(3) "..."
["ExpressionAttributeValues"]=>
array(2) {
[":val1"]=>
array(1) {
["S"]=>
string(2) "20"
}
[":val2"]=>
array(1) {
["S"]=>
string(4) "ajay"
}
}
["FilterExpression"]=>
string(28) "age = :val1 AND name = :val2"
}

How to compare two multidimensional arrays by certain keys in each?

I have two multidimensional arrays of the same structure.
Like this:
array(2) {
[0] =>
array(9) {
'id' =>
string(5) "44994"
'ersatzteil_id' =>
string(3) "120"
'lang' =>
string(6) "name2_tag2"
'title' =>
string(12) "Seitentüren"
'alias' =>
string(12) "seitentueren"
'content' =>
string(1610) "LOREM ISPUM BLALABLBL"
'on_main' =>
string(1) "0"
'disabled' =>
string(1) "1"
'short_text' =>
NULL
}
[1] =>
array(9) {
'id' =>
string(5) "44996"
'ersatzteil_id' =>
string(3) "122"
'lang' =>
string(6) "name1_tag1"
'title' =>
string(7) "Spoiler"
'alias' =>
string(7) "spoiler"
'content' =>
string(1513) "SOME OTHER RANDOM TEXT"
'on_main' =>
string(1) "0"
'disabled' =>
string(1) "0"
'short_text' =>
NULL
}
}
What I need to do is I need to compare first array with the second one.
I have to compare them by keys ersatzteil_id and content , and I find that they have same content I need to store element from first array in another new array, that wasn't existing before.
For example I need something like this, but more efficient:
if(array1[20]['ersatzteil_id'] == array2[145]['ersatzteil_id']
&& array1[20]['content'] == array2[145]['content']){
array3 = array1[20];
}
Try this code:-
$result = [];
foreach($array1 as $arr1){
foreach($array2 as $arr2){
if(($arr1['id'] == $arr2['id']) && ($arr1['ersatzteil_id'] == $arr2['ersatzteil_id'])){
$result[] = $arr1;
}
}
}
echo '<pre>'; print_r($result);

how to decode json that starts with [{ in php

SO I've been searching for a while now but I haven't found anything useful. Im interested in decoding a json code like this:
[
{"server":"1","available":true},
{"server":"2","available":false},
{"server":"3","available":true},
{"server":"4","available":true}
]
I want to decode that using json_decode in php. How can I do that? =)
With the php native function json_decode you will get an structure that matches the json structure.
Use the function like this:
$myobject = json_decode('[
{"server":"1","available":true},
{"server":"2","available":false},
{"server":"3","available":true},
{"server":"4","available":true}
]');
With this code you will have in $myobject this structure:
array(4) {
[0] =>
array(2) {
'server' => string(1) "1"
'available' => bool(true)
}
[1] =>
array(2) {
'server' => string(1) "2"
'available' => bool(false)
}
[2] =>
array(2) {
'server' => string(1) "3"
'available' => bool(true)
}
[3] =>
array(2) {
'server' => string(1) "4"
'available' => bool(true)
}
}
As array of objects:
$data = json_decode($rawData);
As array of assoc arrays:
$data = json_decode($rawData, true);
http://php.net/json_decode

Categories