Jump to content

[SOLVED] Help With multidimensional Array


lachild

Recommended Posts

I have a list of catagoies stored in a multidimensional array.  What I am attempting to do is located the selected catagory submitted through a form and check if the value sent to me is a value or another tree branch.

 

In other words the array is setup like this:

 

 

$myArray = 'Cat1' => array(
                   'Cat2' => array (
                                         'Cat3'),
                                        ),
                     );

 

The data returned in $_POST['cats'] would be something like this:

 

$_POST['cats'] = array (
                                'Cat1', 'Cat2', 'Cat3')

 

What I want to do is to find this option in my array.  The best way I can think of doing this is to see if option1 is a key or value in the array.  If its an array get the values in the array, if its the key do nothing.

 

The obvious answer would be to do something like this:

 

if (isset($myArray[$_POST['cats'][0]][$_POST['cats'][1]$_POST['cats'][2]))
   Do something....
else
   Do something else

 

The problem I have is that the value retrieved from $_POST could contain a catagory tree of 1 - 100 catagories. 

 

So my question is...  Is there a way to simply step through the array based on the input of the user or do I have to code in all 100 possible sinerrios?

Link to comment
https://forums.phpfreaks.com/topic/38362-solved-help-with-multidimensional-array/
Share on other sites

Let me rephrase the question..  How can I translate this

 

<?php
$cats = array('cat0','cat1','cat2','cat3');
$where = '';
for ($i=count($cats)-1; $i >= 0; $i--)
{
$where .= "cat" .$i. " LIKE ".$cats[$i];
if ($i != 0)
	$where .= ' AND ';
}
$query = "SELECT cat".count($cats)." WHERE ".$where;

print $query;
?>

 

for use on a multiple demensional array?

Well After hours of searching I found my answer in a post from casey geene on http://us3.php.net/array

 

function &array_path(&$array, $path) {
   if(!is_array($array)) {
       trigger_error('array_path(): First argument should be an array', E_USER_WARNING);
   }
   settype($path, 'array');
   
   $offset =& $array;
   foreach ($path as $index) {
       if (!isset($offset[$index])) {
           trigger_error("Undefined offset: $index");
           return false;
       }
       $offset =& $offset[$index];
   }
   
   return $offset;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.