Get data from json array PHP range [0] => Array , [1] => Array - php

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 .

Related

JS array to combine into one array

I have following js array using serialisedArray -
Array
(
[0] => Array
(
[name] => sub_maintenance_template[1][maintenance_location_id]
[value] => 54321
)
[1] => Array
(
[name] => sub_maintenance_template[1][maintenance_problem_id]
[value] => 65432
)
[2] => Array
(
[name] => sub_maintenance_template[1][maintenance_priority_id]
[value] => 76896
)
[3] => Array
(
[name] => sub_maintenance_template[1][description]
[value] => sample description
)
)
Expected array -
[sub_maintenance_template] => Array (
[1] =>
(
[maintenance_location_id]=> 54321
[maintenance_problem_id]=> 65432
[maintenance_priority_id]=>76896
[description]=> sample description
)
)
I tried like this-
foreach( $tableData as $key => $value ) {
echo $key;
$newArray['sub_maintenance_template'][3][] = $value['name'];
$newArray['sub_maintenance_template'][3][] = $value['value'];
}
Even though I iterate it through foreach but failed to get desired output. IS there any way to get desired one?
It would be better to pass these as actual arrays in GET or POST, but since the string in name is how arrays would be passed in a URL query string, you can use parse_str:
foreach($array as $values) {
parse_str("{$values['name']} = {$values['value']}", $result);
}
print_r($result);
Or another way; extract and build key/value pairs to build a query string and then parse it:
parse_str(http_build_query(array_column($array, 'value', 'name')), $result);
print_r($result);

PHP multidimensional array - Search value using key

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.

PHP, how to echo specific object data from array?

I use this in wordpress:
$arr=get_post_meta($post->ID, false);
I receive this array:
Array (
[_edit_last] => Array ( [0] => 2)
[year_completed] => Array ( [0] => 2010 )
[designers] => Array ( [0] => )
[developers] => Array ( [0] => )
[producers] => Array ( [0] => )
[_edit_lock] => Array ( [0] => 1298159324 )
[name_en] => Array ( [0] => game 1)
[name_et] => Array ( [0] => game 2 )
[friends_meta] => Array ( [0] => )
)
How do I echo (no for, foreach, etc please) name_en data? Even print_r ($arr->name_en); doesn't work... I suppose it has to be something like - echo $arr->name_en[0]; ???
It is an array or arrays, so:
print_r($arr['name_en']);
or if you only want to get the data:
echo $arr['name_en'][0];
The -> operator is for accessing properties of objects.
echo $arr['name_en'][0] should work.
this should work
echo $arr[0]['year_completed'];
echo $arr[0]['designers'];
etc

How to get the value form SimpleSAML_Session Object

The saml Session is SimpleSAML_Session Object
$data =
(
[trackid:SimpleSAML_Session:private] => 3eb
[idp:SimpleSAML_Session:private] => https://abs.com
[authenticated:SimpleSAML_Session:private] => 1
[attributes:SimpleSAML_Session:private] => Array
(
[firstName] => Array
(
[0] => Aravind
)
[lastName] => Array
(
[0] => M
)
[email] => Array
(
[0] => aravind.muthu#abc.com
)
)
Here i need to take email value . How do i have parse the SAMLobj.
Thanks in advance
Using the below code we can get the values from SAML object.
$values = $data->getAttributes();
Now $values contains an array values .
For more info goto http://simplesamlphp.org/docs/1.6/simplesamlphp-sp-api
Thanks

how to extract the exact data from an array in php

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

Categories