•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 373,469 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,968 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 748 | Replies: 7
•
•
Join Date: Jul 2007
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
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:
Thanks for any help.
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:
php Syntax (Toggle Plain Text)
#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.
Last edited by CMCDragonkai : Feb 9th, 2008 at 9:16 pm.
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
Other Threads in the PHP Forum
- Previous Thread: switching between forms
- Next Thread: Hot to delete



Threaded Mode