Using the below JSON fragment as an example, if I wanted to access the Value of Year, I would use something like
$result->ItemSpecifics->NameValueList[1]->Value[0] .
However, this only works if you know the index of the Year array element (1 in this case).
My question is, if I don't know what the array index of Year is, how can I still access it' value? Is there a way to find the Year element by name rather than index?
This is not correct but I would expect a solution like:
$result->ItemSpecifics->NameValueList['Year']->Value[0]
JSON Example:
[ItemSpecifics] => stdClass Object
(
[NameValueList] => Array
(
[0] => stdClass Object
(
[Name] => Returns Accepted
[Value] => Array
(
[0] => ReturnsNotAccepted
)
)
[1] => stdClass Object
(
[Name] => Year
[Value] => Array
(
[0] => 2001
)
)
[2] => stdClass Object
(
[Name] => Manufacturer
[Value] => Array
(
[0] => Porsche
)
)
Related
how get value feild[id] in php code
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
You didn't give us a name of stdClass so I'm assuming it's $stdClass.
$stdClass->List_inserted[0]->ID
Let's break it down;
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
We access objects with a -> and we access arrays with []
The first part tells us it's an object, so it's;
$stdClass->List_inserted
List_inserted is an array thanks to the => Array. We can access this with [0].
$stdClass->List_inserted[0]
Well, List_inserted[0] is an object, thanks too [0] => stdClass Object; and you wanted to access the ID? So we need another ->
$stdClass->List_inserted[0]->ID
I have a scenario where an API is returning multiple records inside object containing a numeric array like so;
stdClass Object
(
[Event] => Array
(
[0] => stdClass Object
(
[ID] => 111
[Name] => My First Event
[EventType] => stdClass Object
(
[ID] => 1
[Category] => Music
)
)
[1] => stdClass Object
(
[ID] => 222
[Name] => My Second Event
[EventType] => stdClass Object
(
[ID] => 2
[Category] => Sport
)
)
)
[Errors] => stdClass Object
(
[Result] => 0
[Message] =>
)
[RecordCount] => 2
)
I'm current using a foreach loop to iterate through the records. This works fine.
foreach($result->Event as $Event)
But there is a problem here I have a scenario where a single results is returned in the object like so;
stdClass Object
(
[Event] => stdClass Object
(
[ID] => 11
[Name] => My Only Event
[EventType] => stdClass Object
(
[ID] => 2
[Category] => Sport
)
)
[Errors] => stdClass Object
(
[Result] => 0
[Message] =>
)
[RecordCount] => 1
)
Notice there is no [0] array index for the single results.
What's the best way to overcome this keeping in mind that I have no control of the data returned by the API?
Check if Event is an array or an object
if( is_object( $result->Event ) )
{
// ...
}
else
{
foreach( // [....]
}
You may process the object or overwrite it with a 1 item array as suggested by Sam
Btw: very bad API design. I would complain....
The best workaround I have found is to add the single Event to an array with a zero index within the result object. This way the result object matches the same structure as a result containing multiple records.
if(!is_array($result->Event)){
$result->Event = array($result->Event);
}
I have the following sql resultset after querying a mysql table.So it's producing an array having each row as single element.
Array
(
[0] => stdClass Object
(
[quid] => 9
)
[1] => stdClass Object
(
[quid] => 10
)
[2] => stdClass Object
(
[quid] => 11
)
[3] => stdClass Object
(
[quid] => 9
)
[4] => stdClass Object
(
[quid] => 10
)
[5] => stdClass Object
(
[quid] => 11
)
)
Now I would like to create another array using php where the first key will hold all the entire array resultset and so on.
Array
(
[0] => array
(
[0] => stdClass Object
(
[quid] => 9
)
[1] => stdClass Object
(
[quid] => 10
)
[2] => stdClass Object
(
[quid] => 11
)
)
[1] => array
(
[0] => stdClass Object
(
[quid] => 9
)
[1] => stdClass Object
(
[quid] => 10
)
[2] => stdClass Object
(
[quid] => 11
)
)
)
I'll happily edit/update this answer as further details are provided.
If I may, I think your issue is further "upstream" than your PHP code. I'd suggest something in your database schema is missing...
As best I can tell - (your question needs more detail to be honest) - you've got two dimensional information in your database:
[x,y]
So we could see this as:
[0, 9],[0,10],[0,11]
[1, 9],[1,10],[1,11]
If in your schema you have two columns - x,y - you only need select both and parse appropriately in PHP:
<?php
$arr = array();
$res = do_query("SELECT x,y FROM tbl;");
while ($row = get_row($res))
$arr[$row['x']] = $row['y'];
etc.
If you don't have both columns in your database, I'm completely unclear as to how you're going to procedurally break 1-dimensional information into 2.
Perhaps you could demonstrate your schema and some sample data, and provide a fuller explanation of the problem you're trying to solve?
I after using PHP SoapClient to call a service method, I get return value $result, which when print_r($result) gives:
stdClass Object
(
[GetDataRowResult] => stdClass Object
(
[FieldValueList] => stdClass Object
(
[FieldValuePair] => Array
(
[0] => stdClass Object
(
[Field] => Name
[Value] => Christmas Party
)
[1] => stdClass Object
(
[Field] => Status
[Value] => 3
)
[2] => stdClass Object
(
[Field] => StartDate
[Value] => 18/12/2009 12:00 AM
)
[3] => stdClass Object
(
[Field] => EndDate
[Value] => 01/01/1900 12:00 AM
)
)
)
[Message] =>
[Success] => 1
)
)
I want to use the status value to do something, but I don't know how to get to that value. I tried $result->GetDataRowResult->FieldValueList->FieldValuePair[1]->Value and it didn't work, which I sort of expected. EDIT: It did work actually, I had another typo error in the code else where to cause it to go wrong
How do I get to the value I need and is there a better way to recontruct the entire output into a PHP object?
use this
$result->GetDataRowResult->FieldValueList->FieldValuePair->1->Value
I tried to get an answer for this in other posts with no luck, hope someone can help me here, i have a multidimensional array:
Array (
[0] => stdClass Object (
[affectsVersions] => Array ( )
[assignee] => hmontes
[attachmentNames] => Array ( )
[components] => Array ( )
[created] => 2012-08-15T05:31:26.000Z
[customFieldValues] => Array (
[0] => stdClass Object (
[customfieldId] => customfield_10201
[key] => [values] => Array (
[0] => 123456
)
)
[1] => stdClass Object (
[customfieldId] => customfield_10004
[key] => [values] => Array (
[0] => 30
)
)
)
[description] => [duedate] => [environment] => [fixVersions] => Array ( )
[id] => 10228
[key] => NTP-29
[priority] => 3
[project] => NTP
[reporter] => hmontes
[resolution] => [status] => 1
[summary] => case 123456
[type] => 3
[updated] => 2012-08-15T05:31:26.000Z
[votes] => 0
)
)
this is what i get when i do a print_r with the array variable, i need to search and get the value from [key] that would be in this case NTP-29 and keep it in a variable as string.
You can get the value of an array by the key using $array['keyName'];
But, for you it looks like you just need to go deeper $array[0]['key'];
Both arrays values and properties of objects can be accessed using associative array syntax. To get the value of the key property in your object within the array you'd do the following, assuming $array is a variable containing a reference to your array:
$key = $array[0]['key']; // accesses NTP-29 in this case.
Here's another way to access the same property, using object property-access syntax:
$key = $array[0]->key; // also accesses NTP-29.