1

I want to use a function to render an arraykey so I can use it later. I don't even know if something like this is possible. The function I have for now looks like this

function array_key_render($key){

        $result     = explode('.', $key);
        $num        = count($result);

        for($i=0;$i<$num;$i++){
            $array_key .= [$result[$i]];
        }

        return $array_key;

    }

The mainidea is that I can input i.e. PRODUCT.PRIZE and then I would like to get back something like

['PRODUCT']['PRIZE']

UPDATE: i.e. when I have an array like that with numeric keys :

Array
(
    [PRODUCT] => Array
        (
            [0] => Array
                (
                    [NAME] => jeans
                    [TITLE] => Blop
                    [PRIZE] => Array
                        (
                            [NEW] => 13.23€
                            [0] => 24,40€
                        )

                )

            [1] => Array
                (
                    [NAME] => Pullover
                    [TITLE] => OMG
                    [PRIZE] => Array
                        (
                            [0] => 13.23€
                            [NEW] => 24,40€
                        )

                )

        )

)

When I would go for PRODUCT.PRIZE it would return false with my choosen answer. How could I make some kind of wildcard for all numeric keys to make them match? Something like PRODUCT.*.PRIZE

The whole thing is for a little templateengine. If you have some pointers or hints for me, would be great.

2
  • you want your array to look like this? array("product" => "prize") ? Commented Jul 19, 2013 at 20:50
  • no, array('product'=>array('price'=>'somevalue') Commented Jul 19, 2013 at 20:52

6 Answers 6

2

Your function returns a string, so this will not work

$a = array("PRODUCT" => array("PRICE" => 10.99)); 
$key = array_key_render("PRODUCT.PRICE");
echo $a[$key];

I can see where you are going with this though.

<PRE>
<?php

// Obtain an array of keys based on our dotted string
function array_key_render($key)
{
  $result = explode('.', $key);

  return $result;

}

// iterate over the array using the known keys trying to find the right element
function array_access($key, $_array)
{
    $array = $_array;
    $keys = array_key_render($key);

    $found_keys = 0;

    foreach($keys as $k)
    {
        if(isset($array[$k]))
        {
            $array = $array[$k];
            $found_keys++;
        }
        // Oh no, we are lost, break, the keys were not found
        else
        {
            break;
        }
    }

    // Check that we found all the keys we were looking for
    if($found_keys == count($keys))
        return $array;

    return false;
}

$a = array("PRODUCT" => array("PRICE" => 10.99)); 

$key = "PRODUCT.PRICE";

var_dump(array_access($key, $a));

$key = "PRODUCT.NOWAY";

var_dump(array_access($key, $a));

$a = array("PRODUCT" => array("PRIZE" => "REP"));

$key = "PRODUCT.PRIZE";

var_dump(array_access($key, $a));
Sign up to request clarification or add additional context in comments.

3 Comments

That's the best way, I was on a wooden path with my way of approaching the whole thing. Thank you
Do you know how I could incloude some kind of wildcard function for numeric keys?
Updated :) I think I need to catch the wildcard while parsing the keys, but then I dont now whats the best approach to loop through the array elements and save them, since from there on the parser would need to parse for each numeric key that was wildcarded
1

I haven't tested this code so try it out first. Anyway: firsst of all change the for loop to this:

for($i=0;$i<$num;$i++){
  $array_key .= "[" . $result[$i] ."]";
}

Once you have a string representation of the variable you can call eval( ) on the generated string. Most templating engines make use of eval, however beware the numerous security concerns.

1 Comment

Thanks! Didn't know eval()
1

Quick solution, start from the last element of $result array & add it to $array index:

$array = $value;
for($i=$num-1;$i>=0;$i--){
            $array[$result[$i]] = $array;
}

So, if input is PRODUCT.PRIZE.BLA1.BLA2, output will be $array[PRODUCT][PRIZE][BLA1][BLA2]

Comments

0

try this

function array_key_render($key){

        $result     = explode('.', $key);
        $num        = count($result);
        $arr = array();

        for($i=0;$i<$num;$i++){
            $arr[$result[$i]] = array("price" => $i);
        }

    }

i'm assuming your $key looks something like this tho. "1.2.3.4.5"

FYI: not tested and don't quite get what you trying to do... hope this helps

Comments

0

I think this is want you want

function array_key_render($key){
  $keys = array_reverse(explode('.', $key));
  $array = "somevalue";
  foreach ($keys as $key){
    $array = array($key => $array);
  }
  return $array;
}

Comments

0
$string  = 'This.is.my.string';
function stringArray($string = '', $result = array()) 
{
  $keys = explode('.', $string);
  foreach(array_reverse($keys) as $key) {
    $result = array_fill_keys(array($key), $result);
  }
  return $result;
}
print_r(stringArray($string));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.