I have a problem of how I could echo a certain element of an array.
It's easy enough when you know how many keys and what they are.
The problem is, even though I know what the keys are, I don't know how many there, I can find out how many keys by counting the amount of keys I have. But I don't know how to write the amount of keys in PHP:
For example: If there is 3 keys and I know that because I got three zeros.
echo array[0][0][0];
But if there is 4 keys and I know that because I have four zeros,
then I cannot just use the same code.
If you are still confused, I'm basically asking if there is a way to dynamically declare the amount keys in PHP.
I thought up a solution before, and that uses Eval.
If there is another way can anybody help me.
However if there isn't a way, then there's probably a workaround:
This is NogDogs that I used code:
#this function unsets all the elements in a certain array but keeps the
#element in $keep
function myFilter($array, $keep){
$result = array();
foreach($array as $key => $val) {
if(is_array($val)) {
$temp = array();
$temp = myFilter($val, $keep);
if(count($temp)) {
$result[$key] = $temp;
unset($temp);
}
} else {
if($val == $keep) {
$result[$key] = $val;
}
}
}
return($result);
}
#it works for multidimensional arrays aswell.
#try it.
#use your own array for this
#element use your own element.
$firstresult = myfilter($originalarray, $element);
echo "<pre>";
print_r($firstresult);
echo "</pre>";
#as you can see it unsets all the elements in the array but leaves the element
#however if you use this code in an multi dimensional array.
#what happens is that it only takes that array which has the element.
#now dont get me wrong, I want that. But I want it to overwrite the original
#array, but not the whole array only where the array was.
#example:
$originalarray[0][0] = $firstresult[0][0];
echo "<pre>";
print_r($originalarray);
echo "</pre>";
#as you can see there's a problem.
#PROBLEM: The dynamic declaration of keys. Third dimension requires
#2 keys and that needs to be on the sending and recieving end. of the #overwriting process. see $originalarray[0][0] = $firstresult[0][0];
#and if it was two dimensions it needs only one key:
# $originalarray[0] = $firstresult[0]
#so I guess either I have to have someway to dynamically declare array
#keys or I have to use a workaround in the function itself.
Thanks for any help.