Get keys from a multi level array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikepower
Forum Newbie
Posts: 2
Joined: Sun Mar 08, 2009 10:59 pm

Get keys from a multi level array

Post by mikepower »

been trying to solve this problem for a while now with no luck.

I have a multi-level (multi dimension) Array. the keys are generated dynamicly by a web page.
for each value, i need to know the value and the set of keys needed to get that value
Ex..$result = array [keys as string][value]

$result[0] = [0][v1][k1] , k1
$result[1] = [0][v1][k2] , k2
....
$result[6] = [1][v1][k3] , k3

search the web and the php manual for a way to do it with no luck

Code: Select all

 
// test array
  $newArray[0][v1][k1] = "k1";
  $newArray[0][v1][k2] = "k2";
  $newArray[0][v1][k3] = "k3";
  $newArray[0][v1][k4] = "k4";
                    
  $newArray[1][v1][k1] = "k1";
  $newArray[1][v1][k2] = "k2";
  $newArray[1][v1][k3] = "k3";
  $newArray[1][v1][k4] = "k4";
 
  $result = somefuntion($newArray)
 
function somefuntion($array){
// Missing code goes here!
 
//debuging
 
echo "<pre>";
print_r(array_values($array); );
echo "</pre>";
 
return $result
}
 
html output:

Code: Select all

Array
(
    [0] => Array
        (
            [v1] => Array
                (
                    [k1] => k1
                    [k2] => k2
                    [k3] => k3
                    [k4] => k4
                )
 
        )
 
    [1] => Array
        (
            [v1] => Array
                (
                    [k1] => k1
                    [k2] => k2
                    [k3] => k3
                    [k4] => k4
                )
 
        )
 
)
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: Get keys from a multi level array

Post by BomBas »

Code: Select all

 
$result = array();
foreach( $array as $key => $value )
{
$result[ $key ] = array($value);
}
 
I think it should work.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Get keys from a multi level array

Post by Benjamin »

Something smells with the implementation here. But I'm always up for a challenge. Here you go...

Code: Select all

 
<?php
 
$newArray = array();
$newArray[0]['v1']['k1'] = "k1";
$newArray[0]['v1']['k2'] = "k2";
$newArray[0]['v1']['k3'] = "k3";
$newArray[0]['v1']['k4'] = "k4";
$newArray[1]['v1']['k1'] = "k1";
$newArray[1]['v1']['k2'] = "k2";
$newArray[1]['v1']['k3'] = "k3";
$newArray[1]['v1']['k4'] = "k4";
 
function mapValues($array, $path = '') {
    static $mv = array();
 
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            mapValues($value, "{$path}['$key']");
        } else {
            $mv["{$path}['$key']"] = $value;
        }
    }
 
    return $mv;
}
 
$mapped = mapValues($newArray);
 
foreach ($mapped as $path => $value) {
    echo "The path to $value is $path<br />";
    echo "The value of \$newArray{$path} is " . eval('$i = $newArray' . preg_replace('#([^a-z0-9\[\]\'])#i', '', $path) . '; return $i;') . "<br /><br />";
}
 
Script Output wrote: The path to k1 is ['0']['v1']['k1']
The value of $newArray['0']['v1']['k1'] is k1

The path to k2 is ['0']['v1']['k2']
The value of $newArray['0']['v1']['k2'] is k2

The path to k3 is ['0']['v1']['k3']
The value of $newArray['0']['v1']['k3'] is k3

The path to k4 is ['0']['v1']['k4']
The value of $newArray['0']['v1']['k4'] is k4

The path to k1 is ['1']['v1']['k1']
The value of $newArray['1']['v1']['k1'] is k1

The path to k2 is ['1']['v1']['k2']
The value of $newArray['1']['v1']['k2'] is k2

The path to k3 is ['1']['v1']['k3']
The value of $newArray['1']['v1']['k3'] is k3

The path to k4 is ['1']['v1']['k4']
The value of $newArray['1']['v1']['k4'] is k4
mikepower
Forum Newbie
Posts: 2
Joined: Sun Mar 08, 2009 10:59 pm

Re: Get keys from a multi level array

Post by mikepower »

I can not thank you enough !
I was SO close .. but yet SO far :banghead:
$mv was the trick. i was passing my 'keys' back into my function loop and ended up with a rat's nest of a array and code.

Your script worked 99.9%.

the only problem was that each time you made a call to mapValues() it would still have the old keys stored in $mv
giving you 'ghost' paths and values.

the only change i had to make was :

static $mv = array(); to global $mv; in function mapValues

and add:
function somefunction(){
global $mv;
/* some code here */
$mv = array();
$mapped = mapValues($foo]);
/* finish code here */

here it is used in my debug script

Code: Select all

 
function debugDraw(){
global $mv;
 
/* other debuging code */
 
reset($_POST);
while(list($key,$val) = each($_POST)){
   if(is_array($val)){
      $mv = array();
      $mapped =array_mapValues($_POST[$key]);
      foreach ($mapped as $path => $value) {
        echo "$key  $path = $value <br />";
      }
     } else {
       echo "$key = $val <br />";
    }
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Get keys from a multi level array

Post by Benjamin »

Glad I could help. If you are using eval() to get the values be sure to use the preg_replace statement in my example to ensure it's secure.
Post Reply