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;
}
}
Related
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);
I get the following output using print_r in php of my decoded JSON:
stdClass Object
(
[assignments] => Array
(
[0] => stdClass Object
(
[assignmentid] => 1
[grades] => Array
(
[0] => stdClass Object
(
[id] => 1
[userid] => 3
[attemptnumber] => 0
[timecreated] => 1484244192
[timemodified] => 1484244203
[grader] => 2
[grade] => 85.00000
)
)
)
)
[warnings] => Array
(
)
)
I want to get the value for [grade] => 85.00000 and store it in a variable. How would I go about doing this?
What about:
$var = $obj->assignments[0]->grades[0]->grade;
Use true as second parameter of json_decode() to decode it to array and then you have to loop over your result.
$data = json_decode($json, true);
foreach ($data['assignments'] as $row) {
foreach ($row['grades'] as $grade) {
echo $grade['grade'];
}
}
I have following multidimensional Array and I want to get the difference, if there is just one sub Array or multiple in that array.
For Example:
In Array [1] there is just one sub Array [example]
In Array [2] there are two sub Arrays [example]
[content] => Array
(
[...]
[1] => Array
(
[example] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
[2] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
[1] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
Now to get the [value] from the first Array I would try:
foreach ($content as $example) {
echo($content['example']['value']);
}
And to get each [value] from the second Array I would try:
foreach ($content as $example) {
foreach ($example as $values) {
echo($value['value']);
}
}
So far so good but how do I decide which function to run? Am I missing something?
Is there an if-statement which can help me there?
Something like:
if(multiple sub-arrays){
// do first code example
} else {
// do second code example
}
I simply want a method to get all values called [value] out of the array.
Thank you in advance!
The most obvious solution is to change function which generates your content array so as it always generates sub arrays in a format like:
[content] => Array
(
[...]
[1] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
[2] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
[1] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
But if you don't have such option - then use a simple check:
foreach ($content as $item) {
// here check if your `$item` has an `value` subkey under `example` key
if (array_key_exists('value', $item['example'])) {
echo($item['example']['value']);
} else {
foreach ($item['example'] as $values) {
echo ($values['value']);
}
}
}
Assuming that your final dimension allways as a 'value' node:
function arrayIterate($array){
foreach ($content as $example) {
if(!isset($example['value'])){
arrayIterate($example);
}else{
echo($example['value']);
}
}
}
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.
I am new to PHP, please help me with the stuff. Below is the code written followed by its results. How can i access [count] inside [property] in most effective way.
code
echo '<pre>';
print_r($result);
echo '</pre>';
output
Array
(
[0] => Array
(
[property] => Array
(
[href] => http://www.example.com
[count] => 2
[sample] => Array
(
)
[data] => Array
(
[0] => 100002067610524
[1] => 675985591
)
[users] =>
[active] => 1
)
)
)
Not the most effective, but the only possible ;-)
echo $result[0]['property']['count'];
if you have more than one element then use this
foreach($result as $childArray)
{
echo $childArray['property']['count'];
}
if you have only one element in it then use
$array = $result[0]['property']['count'];