2
$data = Array ( ['key1'] => 1 , ['key2'] => 20 , ['key3'] => 11)
$key1 = Array (1 => "a" , 2 => "b")
$key2 = Array (1 => "a" , .... 20 => "y") 
$key3 = Array (1 => "a" , .... 11 => "n")

what is the easiest way to replace all values in $data array to return:

$data['key1'] = $key1[$data['key1']]

instead of doing that one by one i.e:

$data['key1'] = $key1[$data['key1']]
$data['key2'] = $key2[$data['key2']]...
3
  • 3
    Your question is not clear..:( Commented Nov 5, 2013 at 13:34
  • I quoted your array keys - assuming this isn't pseudocode, you ought to crank up error reporting, where you'd be seeing piles and piles of E_NOTICE about undefined constants interpreted as strings (unless you actually defined all those constants, which seems unlikely) Commented Nov 5, 2013 at 13:35
  • why not use an $data['key1']['value1'] .... ? Commented Nov 5, 2013 at 13:37

4 Answers 4

2

I think you are looking for this:

foreach($data as $k => &$v)
{
    if($$k)
    {
        $t = $$k;
        if($t[$v]) $v = $t[$v];
    }
}

print_r($data);

but I'd suggest asking yourself some bigger questions about the intent here

Sign up to request clarification or add additional context in comments.

3 Comments

mmm now it's better, so $t = $$k; this made all the difference but why!!
i don't know, i've never really used variable variables much. i guess you just can't index on them directly - i tried using parens ($$k)[$v] without any luck too
any way your answer deserve to be chosen as the right answer because it's a little bit simpler than @andy's
1

The question is quite hard to understand, but I think what you're trying to do is use $data to pull data from the other arrays. If that's the case, this should work:

$data = array('key1' => 1, 'key2' => 2, 'key3' => 0);
$key1 = array(1,2,3,4,5);
$key2 = array(6,7,8,9,10);
$key3 = array(11,12,13,14);

foreach(array_keys($data) as $key) {
  if(isset($$key)) {
    $target = $$key;
    $value = $target[$data[$key]];

    $data[$key] = $value;
  }
}

var_dump($data); #=> [key1 => 2, key2 => 8, key3 => 11]

2 Comments

and since your answer is the only right one could you please explain why jon_darkstar's answer and quinqui's are wrong even they seems just right but they didn't work
If you take a look at the last edit I made, this one wouldn't of worked either because PHP has a weird bug when referencing double variable arrays. $a = array(0,1,2); $b = 'a'; echo $$b[0] wouldn't work but $a = array(0,1,2); $b = 'a'; $c = $$b; echo $c[0] would for some bizarre reason
1

i'd prefer this solution

array_walk(
    $data,
    function(&$a, $b) {
        $a = $$a[$b];
    }
);

Comments

0

You could try using PHP's variables variables, something like this:

foreach ($data as $mkey => $mval)
{
 $data[$mkey] = $$mkey[$data[$mkey]];
}

3 Comments

Answering to user2903728's comment in @andy's post, I must say my code fails because I'm replacing a value from the same array I'm going through. The idea was to show you the use of variables variables :s
No that's not the answer because It works when replacing the value from the main array but it doesn't work without assigning the variables variables to another variable, for example you can use: $t = $$k;$v = $t[$v]; but $v = $$k[$v] won't work
Uh, I didn't know that :o Well, thanks @andy's code works well ^^U

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.