Get value from JSON object - php

stdClass Object
(
[form] => stdClass Object
(
[fieldsets] => Array
(
[0] => stdClass Object
(
[fieldset_name] => name_1
[datapoints] => Array
(
[0] => stdClass Object
(
[dp_id] => 4
)
[1] => stdClass Object
(
[dp_id] => 5
)
)
)
[1] => stdClass Object
(
[fieldset_name] => name_2
[datapoints] => Array
(
[0] => stdClass Object
(
[dp_id] => 1
)
[1] => stdClass Object
(
[dp_id] => 3
)
)
)
)
)
)
Now i want the values of dp_id seperately into two arrays ie.,array(4,5) and array(1,3)

foreach($object->form->fieldsets as $fieldset) {
$dp_ids = array();
foreach($fieldset->datapoints as $datapoint)
$dp_ids[] = $datapoint->dp_id;
var_dump($dp_ids);
}

try this.
$object->form['fieldsets'][1]->datapoints[1]

Related

i have an array with 8 values in it i want to print 4 in each row dynamically

this is the array
Array (
[0] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[1] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
[2] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
[3] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[4] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
[5] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[6] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[7] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
)
Since you haven't given very much information about your problem, it's not easy to give a 'correct' answer. But i think you can go with this:
x = your array
foreach(array_chunk($x, 4) AS $chunk){
foreach($chunk AS $value){
echo $value->catcount.' - ';
}
echo "<br />";
}
thx to #smartpal for the array_chunk idea

Remove nested stdClass Object from single stdClass Object

i need to remove nested stdClass Object. Now am get stdClass Object like this,
Array
(
[0] => Array
(
[0] => stdClass Object
(
[cs_id] => 1
[cs_service_name] => 2
)
)
[1] => Array
(
[0] => stdClass Object
(
[cs_id] => 2
[cs_service_name] => 3
)
[1] => stdClass Object
(
[cs_id] => 6
[cs_service_name] => 3
)
)
[2] => Array
(
[0] => stdClass Object
(
[cs_id] => 7
[cs_service_name] => 4
)
)
)
But i need stdClass Object like this,
Array
(
[0] => Array
(
[0] => stdClass Object
(
[cs_id] => 1
[cs_service_name] => 2
)
[1] => stdClass Object
(
[cs_id] => 2
[cs_service_name] => 3
)
[2] => stdClass Object
(
[cs_id] => 6
[cs_service_name] => 3
)
[3] => stdClass Object
(
[cs_id] => 7
[cs_service_name] => 4
)
)
)
any idea to remove nested stdClass Object. am using codeigniter3. Can please help me.
Thanks in advance.
You can try with this code:
/* you have a $collection array with all objects */
$newCollection = array();
foreach ($collection as $item) {
if (is_array($item) && count($item)) {
foreach ($item as $subItem) {
$newCollection[] = $subItem;
}
}
}
/* $newCollection is the new array collection */

Getting first Array in variable data (a number of arrays)

The output of an object I am getting is:
Array
(
[0] => stdClass Object
(
[id] => 1
)
)
[1] => stdClass Object
(
[id] => 7
)
)
Array
(
[0] => stdClass Object
(
[id] => 1
)
)
[1] => stdClass Object
(
[id] => 7
)
)
... and so on repeated.
I just want first array from these all arrays. Please note this is not a multidimentional array. .
I want only first array from the output :
Array
(
[0] => stdClass Object
(
[id] => 1
)
)
[1] => stdClass Object
(
[id] => 7
)
)

reorder multidimensional php array trouble

I need to reorder an multidimensional array in php but I can't get it right and I am struggling with foreach, for and while loops for some time now.
This is what I have:
Array
(
[dimension] => Array
(
[dimension.part] => Array
(
[0] => SimpleXMLElement Object
(
[0] => geheel
)
[1] => SimpleXMLElement Object
(
[0] => geheel
)
)
[dimension.type] => Array
(
[0] => SimpleXMLElement Object
(
[0] => hoogte
)
[1] => SimpleXMLElement Object
(
[0] => breedte
)
)
[dimension.value] => Array
(
[0] => SimpleXMLElement Object
(
[0] => 73
)
[1] => SimpleXMLElement Object
(
[0] => 84
)
)
[dimension.unit] => Array
(
[0] => SimpleXMLElement Object
(
[0] => cm
)
[1] => SimpleXMLElement Object
(
[0] => cm
)
)
)
[material] => Array
(
[material.part] => Array
(
[0] => SimpleXMLElement Object
(
[0] => deelmateriaal
)
[1] => SimpleXMLElement Object
(
[0] => deelmateriaal2
)
)
[material_type] => Array
(
[0] => SimpleXMLElement Object
(
[0] => typemateriaal
)
[1] => SimpleXMLElement Object
(
[0] => typemateriaal2
)
)
[material] => Array
(
[0] => SimpleXMLElement Object
(
[0] => materiaal
)
[1] => SimpleXMLElement Object
(
[0] => materiaal2
)
)
[material.notes] => Array
(
[0] => SimpleXMLElement Object
(
[0] => notemateriaal
)
[1] => SimpleXMLElement Object
(
[0] => notemateriaal2
)
)
)
)
And I need to transform it to:
dimensions => Array ( 1 => array(dimension.part => geheel)
=> array (dimension.type => hoogte) )
.....
=> Array ( 2 => array(dimension.part => ...)
=> array (dimension.type => ...) )
....
....
material => Array ( 1 => ...
2 => ...
=> Array ( 1 =>
=>
....
Anyone got a good approach to this?
Thanks,
Joris
foreach($arr['dimension'] as $keyType => $type) {
foreach($type as $key => $value) {
$aNewArray[$key][$keyType] = $value;
}
}
Something like that.. And then process it a bit more to suit your needs..
What it does: walk through every dimension array (and store the key of it in $keyType) and then walk through every item and add it to a new array in the way you want it. It assumes every type has the same amount of items under it.
$new_array = new array();
$count = count($array['dimension']['dimension_part']);
for($i=0;$i<$count;$i++) {
$new_array[$i]['dimension.part'] = $array['dimension']['dimension.part'][$i][0];
$new_array[$i]['dimension.type'] = $array['dimension']['dimension.type'][$i][0];
$new_array[$i]['dimension.value'] = $array['dimension']['dimension.value'][$i][0];
}
I am not quite sure how you access simplexmlobjects, so I used the array type. maybe change it to [$i] -> 0

How can I parse this result?

Array (
[0] => stdClass Object (
[name] => query1
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 226 ) ) )
[1] => stdClass Object (
[name] => query2
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 247 ) ) )
[2] => stdClass Object (
[name] => query3
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 580 ) ) )
[3] => stdClass Object (
[name] => query4
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 10 ) ) )
[4] => stdClass Object (
[name] => query5
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 508 ) ) )
)
How I can parse this result ?
I searched, but I didn't found out how I could do it.
Assuming your array variable is called $data:
foreach ($data as $object) {
$name = $object->name;
$result_set = $object->fql_result_set;
$fan_count = $result_set[0]->fan_count;
// Do what you need to with the data here
}
Hope that helps

Categories