I am having a multi dimensional array as given before with std objects as well
Array
(
[0] => abc_Object Object
(
[_api:protected] => zvbj
[_values:protected] => Array
(
[count] => 2
[data] => Array
(
[0] => abc_Charge Object
(
[_api:protected] => zvbj
[_values:protected] => Array
(
[id] => dGoEw
[ine] => fKYF0a
[xyz] => abc_Object Object
(
[_api:protected] => zvbj
[_values:protected] => Array
(
[id] => uUmuym
[last] => 4242
)
)
)
)
[1] => abc_Charge Object
(
[_api:protected] => zvbj
[_values:protected] => Array
(
[id] => dblvEw
[ine] => fKyyu0a
[xyz] => abc_Object Object
(
[_api:protected] => z7jj
[_values:protected] => Array
(
[id] => urtuym
[last] => 4242
)
)
)
)
)
)
)
[1] => abc_Object Object
(
[_api:protected] => zvbj
[_values:protected] => Array
(
[count] => 2
[data] => Array
(
[0] => abc_Charge Object
(
[_api:protected] => zvbj
[_values:protected] => Array
(
[id] => dGoEw
[ine] => fKYF0a
[xyz] => abc_Object Object
(
[_api:protected] => zvbj
[_values:protected] => Array
(
[id] => uUmuym
[last] => 4242
)
)
)
)
[1] => abc_Charge Object
(
[_api:protected] => zvbj
[_values:protected] => Array
(
[id] => dblvEw
[ine] => fKyyu0a
[xyz] => abc_Object Object
(
[_api:protected] => z7jj
[_values:protected] => Array
(
[id] => urtuym
[last] => 4242
)
)
)
)
)
)
)
)
I am trying to combine all these into a much simpler array as
Array(
[0]=>(
[id] => dGoEw
[ine] => fKYF0a
[cid] => uUmuym
)
[1]=>(
[id] => dblvEw
[ine] => fKyyu0a
[cid] => urtuym
)
[2]=>(
[id] => dGoEw
[ine] => fKYF0a
[cid] => uUmuym
)
[3]=>(
[id] => dblvEw
[ine] => fKyyu0a
[cid] => urtuym
)
)
I have tried alot of foreach looping and stuff but i am not able to even print first value of the array. I dont know what i am missing.
echo $arr['_api:protected'];
EDIT
foreach($arr as $key=>$val)
{
echo $val['_api:protected'];
print_r($val['_values:protected']);
}
No ouput from this even
foreach($arr as $key=>$val)
{
$aaa = $val['_values:protected']['data'];
$arr111['id'] = $aaa['_values:protected']['invoice'];
$arr111['ine'] = $aaa['_values:protected']['id'];
$arr111['cid'] = $aaa['_values:protected']['xyz']['_values:protected']['id'];
$arr222[] = $arr111;
}
print_r($arr222);
is also not giving me any output. Can anyone please tell me what i am missing?
Because the properties you are trying to access are defined as
protectedthey cannot be accessed unless you are in the context of the class of the object, or a class the extends the class of the object. The:protectedthat you see on the end of the key is not part of the name, it is telling you that the property is defined asprotected. So the name of the key is not_values:protected, it is_valuesand it’s visibility is set toprotected.Here is a slightly horrible solution with two classes that should allow you to fetch the data from a valid context:
If
abc_Objectextendsabc_Chargeor vice versa, it would be possible to wrap this in a single class that extends the child object, but it still wouldn’t be a beautiful solution – someone with a better OO head than mine may have a better idea.As a side note, you should have
error_reporting(E_ALL); ini_set('display_errors', 1);at the top of your script while developing. If you had, you would have seen many error messages with your code samples above to give you a clue what the problem is.