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 391,964 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 4,116 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: 833 | Replies: 7
Reply
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  
Join Date: Feb 2008
Posts: 4
Reputation: monkey56657 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
monkey56657 monkey56657 is offline Offline
Newbie Poster

Re: Dynamic Declaration of Arrays Keys

  #2  
Feb 12th, 2008
Can you explain again exactly what you want with better examples. I get all of what your saying but dont get exactly what your looking for.

Provides a few array b4 and afters or something.

Okay I am going to have a random guess firstly and hope this is what you want.

Okay we imagine an array called $content. The piece of data we want could be in any of the following

$content[0]
$content[0][0]
$content[0][0][0]
$content[0][0][0][0]
$content[0][0][0][0][0]

But all the keys you know. Firstly we assume that all the keys are 0 and we want to follow all of the 0 keys until we find a key that is not an array?

Then we can do the following
  1. function array_flow($array)
  2. {
  3. if (is_array($array[0]))
  4. {
  5. array_flow($array[0]);
  6. }
  7. else
  8. {
  9. return ($array[0]);
  10. }
  11. }
Which can easily be used as below...
  1. echo array_flow($content);
Now if we assume that the array key isnt always 0 but always remains constant then we can add to the original function as below...
  1. function array_flow($array,$key)
  2. {
  3. if (is_array($array[$key]))
  4. {
  5. array_flow($array[$key]);
  6. }
  7. else
  8. {
  9. return ($array[$key]);
  10. }
  11. }
And using it would differ slightly....
  1. echo array_flow($content,1);
In the new example we use 1 as the key so we could find the value of

$content[1] all the way to $content[1][1][1][1][1] and beyond.

I really hope this is what you are looking for. If not you can contact me via IM or something...

All my contact info can be found here http://profiles.web-starters.net/monkey56657
Last edited by monkey56657 : Feb 12th, 2008 at 6:26 pm.
Reply With Quote  
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

Re: Dynamic Declaration of Arrays Keys

  #3  
Feb 12th, 2008
You are so close to what I mean..



Ok, let me clarify.



My function, previously removes elements from arrays and leaves a specific element back on that array. It doesn't change the keys, it simply unsets the ones I don't want.



For example, if a there is an array like this:



0=>"blah"

1=>"saa"

2=>"wee"



When I use my function:



myfilter($array,"saa")



It unsets 0,2



And leaves 1.



So that resultant array is now



$array =array(1=>"saa");



You can find that out if you test my code out.



But there is a problem, I said up there that the "resultant array" and not the "original array"



So what the function has done, is basically took the $array and made another array which is the result.



So it hasn't actually changed the "original array" but the made a new array "result" which is the one I want.



Of course this is easily solved.

We simply just used $result. Or we can overwrite $original array = $result array.



This is only for a single dimensional array.



BUT!



What happens when it's a multidimensional array?



Well basically the same thing happens. Except for one thing.



When it unsets the array, It doesn't unset the whole array, only where the element resides in.



So if "saa" was in $array[0][0]. Then it only unsets the elements in $array[0]. All the other elements in $array are fine.



Understand?



So everything works, until we get to the overwriting process. The "myfilter" function still makes a resultant array. This array however isn't the whole original array, it makes the array of where the excepted element resides in.



So we need to do a overwriting process again.



But here's the problem.



I know where the element is. This is because I have another function that finds the keys that I need. So I have an $index which is an array of the keys. In normal number format of course.



So say we try to overwrite a 2dimensional array. Example: $ar2

$ar2 is an array

$ar2[0] is an array

$ar2[1] is an array



$ar2[1][0] is "saa"



So we do the function, it removes all of $ar2[1] but leaves $ar[1][0] which is "saa"

$ar[0] is however fine.



So the function produces a result.

$result is an array of results, only one result exists.

$result[1] is the array of the thing we want.



So to overwrite we do



$ar2[1] = $result[1]

Remember that $ar2[1] is an array of the where the element resides in. And $result[1] simply copied it and threw away the ones we didn't want.

And for third dimension arrays.
$ar3[$number1][$number2] = $result[$number1][$number2]

Where the numbers are the keys.

$number1 will be the array of the selected array. $number2 will be the array of the selected element.

So on, so you can see my problem.

I know the numbers. But I don't know the size of the array. How am I going to overwrite it? Other than using eval of course.

Array flow? What is that, I have never seen it before.
Reply With Quote  
Join Date: Feb 2008
Posts: 4
Reputation: monkey56657 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
monkey56657 monkey56657 is offline Offline
Newbie Poster

Re: Dynamic Declaration of Arrays Keys

  #4  
Feb 17th, 2008
Okay try this. I hope its okay this time.

  1. function array_flow(&$arr,$find,$call=0)
  2. {
  3. foreach ($arr as $k => $v)
  4. {
  5. if (is_array($v))
  6. {
  7. array_flow($arr[$k],$find,1);
  8. }
  9. else if ($v != $find)
  10. {
  11. unset($arr[$k]);
  12. }
  13. else
  14. {
  15. $flag = true;
  16. }
  17. }
  18.  
  19. if (!$flag && $call)
  20. {
  21. $arr = null;
  22. }
  23. }

Example Array

---------------------------------------------

$array[0][0] = 'fish';
$array[0]['sleepy'] = 'bed';
$array[0][2] = 'tree';
$array[1][0] = 'sheep';
$array[1][1] = 'cow';
$array[1][2] = 'html';
$array[2][0] = 'monkey';
$array[2]['house'] = 'flat';

---------------------------------------------

Use it as simple as this....

array_flow($array, 'sheep');

The $array will be the original and only array. No need for copy over. And sheep is the thing you are looking for. The "array_flow" is just a function I created with a not so descriptive name.

------------------------------------------------------

For the set of data above here is the result...The best I can do is set the elements no longer needed to NULL but this is virtually the same as un-setting them. the unset() function only works for named elements so thats why it cant be unset fully. This will work for any dimension arrays and remove everything but the element required.

array ( 0 => null, 1 => array ( 0 => 'sheep' ), 2 => null )

MSN me if need more help. http://www.web-starters.net/includes...3366&Width=350
Last edited by monkey56657 : Feb 17th, 2008 at 3:36 pm.
Reply With Quote  
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

Re: Dynamic Declaration of Arrays Keys

  #5  
Feb 18th, 2008
I recently rewrote the function, to this. It works to do what I want it to do. I will try your function, but it will be a while can you tell me now that does your function work like mine?

  1. $typeposition[found][0][1] = "0";
  2. $typeposition[found][0][2] = "1";
  3.  
  4. #ALSO TRY THIS FUNCTION...
  5. /*** Parse n-dimension array and only keep elements with specified value* @param array $array array to be parsed* @param mixed $keep value to keep* @return array*/
  6.  
  7.  
  8. function myFilter(&$array,$keep,$index){
  9. foreach($index as $positions){
  10. $keyedindex[]="[$positions]";
  11. }
  12. $indeximploded = implode("", $keyedindex);
  13.  
  14.  
  15. $result = array();
  16. foreach($array as $key => $val){
  17. if(is_array($val)){
  18. $temp = array();
  19. $temp = myFilter($val, $keep, $index);
  20. if(count($temp)){
  21. $result[$key] = $temp;
  22. unset($temp);
  23. }
  24. }else{
  25. if($val == $keep){
  26. $result[$key] = $val;
  27. }
  28. }
  29. }
  30.  
  31. $function = "
  32. \$array$indeximploded = \$result$indeximploded;
  33. ";
  34. eval($function);
  35. }
  36.  
  37. myfilter($typearray,$element,$typeposition[found][0]);
Last edited by CMCDragonkai : Feb 18th, 2008 at 12:14 am.
Reply With Quote  
Join Date: Feb 2008
Posts: 4
Reputation: monkey56657 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
monkey56657 monkey56657 is offline Offline
Newbie Poster

Re: Dynamic Declaration of Arrays Keys

  #6  
Feb 18th, 2008
Im not even sure whats yours does. But seems a lot more complicated (over complicated).
Reply With Quote  
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

Re: Dynamic Declaration of Arrays Keys

  #7  
Feb 19th, 2008
try my function, use your own array and your own element you want to keep. And the index is just an array of where that element you want to keep is. Refer to typeposition[found][0]
Reply With Quote  
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

Re: Dynamic Declaration of Arrays Keys

  #8  
Feb 23rd, 2008
Oh you nearly did it. The thing is you misunderstood one part of what I wanted. Your code eliminates EVERYTHING but the $keep. But I want to eliminate the other elements in the array in which there is the $keep. This means that in a 2D array, In which there is array of arrays. I want to only eliminate the elements that are inside a certain array, Not all the arrays.

When I do this for example.

array_flow($typearray, $element);

It gives this

Array
(
[0] => Array
(
[1] => es
)

[1] =>
[2] =>
)

But the [1] and [2] should still hold what it should hold.

You're really close, wow I never thought it could be done like this.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:13 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC