Reputation: 3804
I don't know exactly the programming terminology but I have a lot of arrays like this
$arr[3][4][0]='sample value A';
$arr[3][4][1]='sample value B';
...and so on
They have 3 keys each, so I was wondering if PHP has any function to split into values, keys and details of each var like this:
$keyA[0]=3;
$keyB[0]=4;
$keyC[0]=0;
$strg[0]='sample value A';
$keyA[1]=3;
$keyB[1]=4;
$keyC[1]=1;
$strg[1]='sample value B';
without doing a foreach
inside a foreach
to grab the values ?
Upvotes: 2
Views: 59
Reputation: 32392
$array[3][4][0] = 'sample value A';
$array[3][4][1] = 'sample value B';
$iter = new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iter as $key => $value) {
$keys[$iter->getDepth()] = $key;
if(!$iter->callHasChildren()) {
$keyA[] = $keys[0];
$keyB[] = $keys[1];
$keyC[] = $keys[2];
$strg[] = $value;
}
}
print_r($keyA);
print_r($keyB);
print_r($keyC);
print_r($strg);
Upvotes: 1
Reputation: 13728
try using foreach()
don't know any function can help
foreach($a as $k=> $v) {
foreach($v as $k1=> $v1) {
foreach($v1 as $k2=> $v2) {
$keyA[] = $k;
$keyB[] = $k1;
$keyC[] = $k2;
$strg[] = $v2;
}
}
}
print_r($keyA); //Array ( [0] => 3 [1] => 3 )
print_r($keyB); //Array ( [0] => 4 [1] => 4 )
print_r($keyC); //Array ( [0] => 0 [1] => 1 )
print_r($strg); //Array ( [0] => sample value A [1] => sample value B )
Upvotes: 3