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.

Recommended Answers

All 7 Replies

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

function array_flow($array)
{
	if (is_array($array[0]))
	{
		array_flow($array[0]);
	}
	else
	{
		return ($array[0]);
	}
}

Which can easily be used as below...

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...

function array_flow($array,$key)
{
	if (is_array($array[$key]))
	{
		array_flow($array[$key]);
	}
	else
	{
		return ($array[$key]);
	}
}

And using it would differ slightly....

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

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.

Okay try this. I hope its okay this time.

function array_flow(&$arr,$find,$call=0)
{
	foreach ($arr as $k => $v)
	{
		if (is_array($v))
		{
			array_flow($arr[$k],$find,1);
		}
		else if ($v != $find)
		{
			unset($arr[$k]);
		}
		else
		{
			$flag = true;
		}
	}
	
	if (!$flag && $call)
	{
		$arr = null;
	}
}

Example Array

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

$array[0][0] = 'fish';
$array[0] = 'bed';
$array[0][2] = 'tree';
$array[1][0] = 'sheep';
$array[1][1] = 'cow';
$array[1][2] = 'html';
$array[2][0] = 'monkey';
$array[2] = '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/print-image.php?PrintImage=d2xtQG15eGNwLm5ldA==&Color=003366&Width=350

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?

$typeposition[found][0][1] = "0";
$typeposition[found][0][2] = "1";

#ALSO TRY THIS FUNCTION...
/***  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*/


function myFilter(&$array,$keep,$index){
   foreach($index as $positions){
    $keyedindex[]="[$positions]";
   }
   $indeximploded = implode("", $keyedindex);


   $result = array();
   foreach($array as $key => $val){
    if(is_array($val)){
     $temp = array();
     $temp = myFilter($val, $keep, $index);
     if(count($temp)){
      $result[$key] = $temp;
      unset($temp);
     }
    }else{
     if($val == $keep){
      $result[$key] = $val;
     }
    }
   }

   $function = " 
    \$array$indeximploded = \$result$indeximploded;
   ";
   eval($function);
   }

myfilter($typearray,$element,$typeposition[found][0]);

Im not even sure whats yours does. But seems a lot more complicated (over complicated).

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]

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.