PHP Echo from multidimensional arrays - php

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"];

Related

PHP Multidimensional Array with multiple array with same keys?

I've been trying multiple things and for the life of me can not get this to work. I'm beginning to think it maybe isn't possible at this point.
So I have a SOAP API I'm sending this array too. Below is the code I currently have that works, but does not send the multiple values. It just uses the last one as it overwrite the previous.
Looking at this thread, what I'm doing should work?
$my_array['sn'] = "234234232";
$my_array['arrayparams'] = array(
'Param' => array( 'Name' => 'sending_key', 'Value' => 'blah',),
'Param' => array( 'Name' => 'sending_key2', 'Value' => '2',),
);
$my_array['push'] = true;
$my_array['endsession'] = false;
returns:
array(4) {
["sn"]=>
string(12) "234234232"
["arrayparams"]=>
array(1) {
["Param"]=>
array(2) {
["Name"]=>
string(61) "sending_key2"
["Value"]=>
string(1) "2"
}
}
["push"]=>
bool(true)
["endsession"]=>
bool(false)
}
I'm just having a time getting it to send this instead:
array(4) {
["sn"]=>
string(12) "234234232"
["arrayparams"]=>
array(2) {
["Param"]=>
array(2) {
["Name"]=>
string(61) "sending_key"
["Value"]=>
string(1) "blah"
}
["Param"]=>
array(2) {
["Name"]=>
string(61) "sending_key2"
["Value"]=>
string(1) "2"
}
}
["push"]=>
bool(true)
["endsession"]=>
bool(false)
}
The 'Param' array is very strict and has to have this value, I can not change to 'Param2' to get it to work. Thanks in advanced!
can you do this?
$my_array['arrayparams'] = array(
array('Param' => array( 'Name' => 'sending_key', 'Value' => 'blah',)),
array('Param' => array( 'Name' => 'sending_key2', 'Value' => '2',)),
);
The problem is you can't have the key 'Param' set in more than one key.
You would need to define 'Param' as an actual array, instead of as multiple keys within in array.
like so...
$my_array['Param'] = [
['Name' => 'sending_key', 'Value' => 'blah'],
['Name' => 'sending_key2', 'Value' => '2']
];

putting key and value of one associative array into another associative array [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 7 years ago.
I have an array of arrays like this :
array(2) {
[0]=>
array(2) {
["ssmenu_id"]=>
string(1) "5"
["ssmenu_titre"]=>
string(10) "newsletter"
}
[1]=>
array(2) {
["ssmenu_id"]=>
string(1) "6"
["ssmenu_titre"]=>
string(9) "sous-test"
}
}
How do I get from that to :
array(2) {
5 => "newsletter",
6 => "sous-test"
}
I have tried various things using foreach, for, list... can't get my head around it.
With PHP 5 >= 5.5.0 and PHP 7, you can use array_column():
$array = [
[
"ssmenu_id" => 5,
"ssmenu_titre" => "newsletter",
],
[
"ssmenu_id" => 6,
"ssmenu_titre" => "sous-test",
],
];
print_r(array_column($array, 'ssmenu_titre', 'ssmenu_id'));
/* Output:
Array
(
[5] => newsletter
[6] => sous-test
)
*/
$res = array();
foreach($arr as $item){
$res[$item['ssmenu_id']] = $item['ssmenu_titre'];
}
var_dump($res);
output:
array(2) {
[5]=>
string(10) "newsletter"
[6]=>
string(9) "sous-test"
}
try below solution:
$array = array(
array(
"ssmenu_id" => 5,
"ssmenu_titre" => "newsletter",
),
array(
"ssmenu_id" => 6,
"ssmenu_titre" => "sous-test",
),
);
$res = array();
foreach($array as $item){
$res[$item['ssmenu_id']] = $item['ssmenu_titre'];
}
print_r($res);
output:
Array
(
[5] => newsletter
[6] => sous-test
)
I'd suggest using the array_flip() function. As this is a multi-dimensional array you will need to loop through it. There are examples in the comments to the PHP page I linked to, but it's a pretty trivial task when using this function.

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

parsing xml array not displaying all data in view

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]

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