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 */
Related
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
I'm looking for the way to replace my array.
My first array below:
$arr1 = Array
(
[0] => stdClass Object
(
[values] => Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 0
)
[1] => stdClass Object
(
[field_value] => Red
[count] => 0
)
)
)
[1] => stdClass Object
(
[values] => Array
(
[0] => stdClass Object
(
[field_value] => Plastic
[count] => 0
)
[1] => stdClass Object
(
[field_value] => Metall
[count] => 0
)
)
)
My second array:
$arr2 = Array
(
[0] => 2
[1] => 6
[2] => 3
[3] => 4
)
And I would like to get this:
Array
(
[0] => stdClass Object
(
[values] => Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => Red
[count] => 6
)
)
)
[1] => stdClass Object
(
[values] => Array
(
[0] => stdClass Object
(
[field_value] => Plastic
[count] => 3
)
[1] => stdClass Object
(
[field_value] => Metall
[count] => 4
)
)
)
I have tried to use array_map function but without any success.
array_map(function($a,$b){$a = $b; return $a;}, $arr1, $arr2);
Thanks!
$array=$arr[0]->values;
$new_array=array();
foreach($array as $key=>$val)
{
$new_array[$key]=$val;
$new_array[$key]->count=$arr2[$key];
}
$result=array();
$result[0]->values=$new_array;
Create your own function as needed, and customize as you like, see below example:
function buildMyArray($array1, $array2)
{
foreach($array1[0]->values as $key => $value){
$array1[0]->values[$key]['count'] = $array2[$key];
}
return $array1;
}
and you may call it like:
$result = buildMyArray($arr1, $arr2);
If you want to use array_map, then something should be like below:
$arr1[0]->values = array_map(function($v, $k) use ($arr2) {
// if not found in $arr2, remain the original value.
$v->count = isset($arr2[$k]) ? $arr2[$k] : $v->count;
return $v;
}, $arr1[0]->values, array_keys($arr1[0]->values));
How on Earth do I get the ids, and names from this array (possibly by using a ForEach construct)?
stdClass Object (
[data] => Array (
[0] => stdClass Object ( [name] => Corinne Culbard [id] => 5005561918 )
[1] => stdClass Object ( [name] => Tom West [id] => 5034297044 )
[2] => stdClass Object ( [name] => Sabri Tasci [id] => 5057373639 )
Is this not working?
foreach ($object->data as $item) {
$name = $item->name;
$id = $item->id;
}
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
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]