I have an array generated out of json_decode().
$array_data = json_decode(json_encode(simplexml_load_string($data)), true);
The output array looks like below:
Array
(
[#attributes] => Array
(
[version] => 1.0
)
[response] => Array
(
[operation] => Array
(
[#attributes] => Array
(
[name] => ADD_REQUEST
)
[result] => Array
(
[statuscode] => 200
[status] => Success
[message] => Request added successfully
)
[Details] => Array
(
[0] => Array
(
[workorderid] => 291885
)
[1] => Array
(
[parameter] => Array
(
[name] => workorderid
[value] => 291885
)
)
)
)
)
)
I need to save the value of the key 'workorderid' in another php varaible,so I can use it further in my code. the value is dynamic.
I have been struggling a lot now and looking for some guidance.
Could anyone please help with out in getting this done? Thanks a lot in advance!
Regards,
Pooja
If you know for sure the first array under Details will contain the workorderid key, you can just access it directly:
$workorderid = $array_data['response']['operation']['Details'][0]['workorderid'];
var_dump($workorderid);
Output:
string(6) "291885"
If you don't know in which array under Details it will be, you'll have to loop over it and find it:
$workorderid = null;
foreach ($array_data['response']['operation']['Details'] as $detail) {
if (isset($detail['workorderid'])) {
$workorderid = $detail['workorderid'];
break;
}
}
if (null !== $workorderid) {
var_dump($workorderid);
}
Output:
string(6) "291885"
This is a viable solution if you only need to fetch 1 key from the response. If you'd need more keys I'd suggest mapping the response data into a more readable structure.
Related
I need help. Please, I have a service that I have no control over that shows me formatted data Json
echo print_r( $data);
Array
(
[0] => Array
(
[user] => one
)
[1] => Array
(
[user] => two
)
[2] => Array
(
[user] => three
)
)
I want to get the data
one
two
three
How can I do that?
Thank You ,
use foreach,
foreach($yourdata as $data)
{
echo $data['user'];
}
this will give you all user .
What would be the most efficient way to loop through this multidemsional array? This array is much larger than the example given, and would contain all the visitors ip address who visit my site. Also I would only like to loop through the information contained in 'sc_data'.
Array
(
[#attributes] => Array
(
[status] => ok
)
[sc_data] => Array
(
[0] => Array
(
[ip_addresss] => 1
)
[1] => Array
(
[ip_address] => 1
)
)
)
The most efficient way to loop through an array is foreach
foreach($array['sc_data'] as $key => $value) {
echo $value['ip_addresss'];
}
I'm a newbie to this associative array concept in PHP. Now I'm having an array named $sample as follows:
Array
(
[name] => definitions
[text] =>
[attributes] => Array
(
[name] => Mediation_Soap_Server_Reporting
[targetnamespace] => https://mediation.moceanmobile.net/soap/reporting
)
[children] => Array
(
[types] => Array
(
[0] => Array
(
[name] => types
[text] =>
[attributes] => Array
(
)
[children] => Array
(
[xsd:schema] => Array
(
[0] => Array
(
[name] => schema
[text] =>
[attributes] => Array
(
[targetnamespace] => https://mediation.moceanmobile.net/soap/reporting
)
[children] => Array
(
[xsd:complextype] => Array
(
[0] => Array
(
[name] => complextype
[text] =>
[attributes] => Array
(
[name] => Mediation_Soap_FaultMessage
)
[children] => Array
(
[xsd:sequence] => Array
(
[0] => Array
(
[name] => sequence
[text] =>
[attributes] => Array
(
)
)
)
)
)
)
)
)
)
)
)
)
)
)
From the above array I want to refer(or access) to the key xsd:schema. But I'm not able to do it. Can you please tell me how should I access or refer this key from the associative array names $sample? Thanks in advance.
To access this value you would use:-
$sample['children']['types'][0]['children']['xsd:schema'];
If you have multiple of these elements in your types array you will need to loop through them:-
foreach($sample['children']['types'] as $type) {
if(isset($type['children']) && isset($type['children']['xsd:schema'])) {
// Perform action on element
$type['children']['xsd:schema'];
}
}
If you do not know your structure (as in xsd:schema can occur outside of types) then you will need to write a recursive function or loop for finding it.
I guess your goal is to seek for the key/value pair where the key is "xsd" ?
If so, in PHP, you can do so by using the follwing logic:
while (list($key, $value) = each($arr)) {
echo "Key: $key; Value: $value<br />\n";
}
// OR
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
Just add a set of recursing or nested loops to traverse the structure until you find the proper key.
[responseheader] => Object (
Array (
[0] => Object (
[id] => id_1
[name] => abc
)
[1] => Object (
[id] => id_2
[name] => xyz
)
)
)
[response] => Object (
[id_1] => Object (
[content] => Array (
[0] => content_1
)
)
[id_2] => Object (
[content] => Array (
[0] => content_2
)
)
)
Both the above 2 objects, responseheader and response are under one object (header).
In the above structure, the order is for response will be same as it is in responseheader.(ie, id_2 will always come after id_1)
I want to get the content from response for each id present in responseheader. I will be iterating responseheader object.
I could loop through response and progressively add another property (say dummy) inside responseheader storing the content, but is there any better, faster approach?
This should do the trick.
$result = array();
//loop through responseheader array
foreach($data['responseheader'] as $row)
{
//if the id exists in the response array add it to the result array
if(array_key_exists($row['id'], $data['response'])) {
$result[] = $data['response'][$row['id']]['content'][0];
}
}
print_r($result);
If the content in response could contain more than one content you have to loop throught it to:
foreach($data['response'][$row['id']]['content'] as $content) {
$result[] = $content;
}
i got this array from an database...how to get the exact value attribute......
Array
(
[0] => TRowResult Object
(
[row] => tests
[columns] => Array
(
[a21ha:link_0] => TCell Object
(
[value] =>testsample
[timestamp] => 1265010584060
)
[a21ha:link_1] => TCell Object
(
[value] => example
[timestamp] => 1265092697040
)
)
)
how to get the [value] alone in php
print $myArray[0]->columns['a21ha:link_0']->value;
This would give you the first. To get all, you'd have to loop through the contents.
foreach ($myArray[0]->columns as $column) {
print $column->value;
}
Supposed your array called $array:
foreach($array as $arridx => $rowobj){
foreach($rowobj->columns as $cellid => $rowcell){
echo $rowcell->value;
}
}