User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 401,628 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,952 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

Dynamic Declaration of Arrays Keys

Join Date: Jul 2007
Posts: 9
Reputation: CMCDragonkai is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
CMCDragonkai CMCDragonkai is offline Offline
Newbie Poster

Dynamic Declaration of Arrays Keys

  #1  
Feb 9th, 2008
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:

  1. #this function unsets all the elements in a certain array but keeps the
  2. #element in $keep
  3.  
  4.  
  5. function myFilter($array, $keep){
  6. $result = array();
  7. foreach($array as $key => $val) {
  8. if(is_array($val)) {
  9. $temp = array();
  10. $temp = myFilter($val, $keep);
  11. if(count($temp)) {
  12. $result[$key] = $temp;
  13. unset($temp);
  14. }
  15. } else {
  16. if($val == $keep) {
  17. $result[$key] = $val;
  18. }
  19. }
  20. }
  21. return($result);
  22. }
  23.  
  24. #it works for multidimensional arrays aswell.
  25. #try it.
  26.  
  27. #use your own array for this
  28. #element use your own element.
  29.  
  30. $firstresult = myfilter($originalarray, $element);
  31. echo "<pre>";
  32. print_r($firstresult);
  33. echo "</pre>";
  34.  
  35.  
  36. #as you can see it unsets all the elements in the array but leaves the element
  37. #however if you use this code in an multi dimensional array.
  38.  
  39. #what happens is that it only takes that array which has the element.
  40. #now dont get me wrong, I want that. But I want it to overwrite the original
  41. #array, but not the whole array only where the array was.
  42.  
  43. #example:
  44. $originalarray[0][0] = $firstresult[0][0];
  45. echo "<pre>";
  46. print_r($originalarray);
  47. echo "</pre>";
  48.  
  49.  
  50. #as you can see there's a problem.
  51.  
  52. #PROBLEM: The dynamic declaration of keys. Third dimension requires
  53. #2 keys and that needs to be on the sending and recieving end. of the #overwriting process. see $originalarray[0][0] = $firstresult[0][0];
  54. #and if it was two dimensions it needs only one key:
  55. # $originalarray[0] = $firstresult[0]
  56.  
  57. #so I guess either I have to have someway to dynamically declare array
  58. #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.
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 6:00 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC