0

I'm trying to change the keynames of the keys in my arraylist. Say, I have an array like the one below. I want to change "n" to "name" and "l" to "surname"

Array
(
    [0] => Array
        (
            [n] => Amy
            [l] => Gonzalez
        )

    [1] => Array
        (
            [n] => Jeff
            [l] => Garcia
        )

)

This is my code. $data is where the array is stored and I get it dynamically.

$newkey = array('n'=>'name','l'=>'surname');
$count = count($data);
        $i=0;
        if($i<=$count){
            foreach( $data as &$item )
            {
                foreach( $newkey as $key => $replace )
                {
                    if (key_exists($key,$item))
                    {
                        $item[$replace] = $item[$key];
                        unset($item[$key]); 
                    }
                }
            }
            $i++;
        }

This works for the last entry only. What am I doing wrong? My array may sometimes fetch several results, not only 2.

7
  • 1
    What are the variables $i and $count doing? Can you remove those and leave just the foreach? Commented Mar 3, 2014 at 15:23
  • first : remove the '&' before the $item variable Commented Mar 3, 2014 at 15:24
  • Initially I didn't but as the code worked only for the last entry, that is why I introduced those variables Commented Mar 3, 2014 at 15:24
  • @Miam84 no, use-by-reference is correct here. Commented Mar 3, 2014 at 15:24
  • @user2509780: Please add the expected output to the question. Commented Mar 3, 2014 at 15:25

2 Answers 2

0

Try something like this:

foreach($data as &$array) {
    $array = array_combine(
        str_replace(array_keys($newkey), $newkey, array_keys($array)), $array);
}
Sign up to request clarification or add additional context in comments.

Comments

-1

Not an answer persee but I need some space to post code

I'm not sure what is wrong because this code works:

$data = array(
    array(
        "n" => "a",
        "l" => "b"
    ),
    array(
        "n" => "a",
        "l" => "b"
    )
);
$newkey = array('n'=>'name','l'=>'surname');
foreach( $data as &$item )
{
    foreach( $newkey as $key => $replace )
    {
        if (key_exists($key,$item))
        {
            $item[$replace] = $item[$key];
            unset($item[$key]); 
        }
    }
}
var_dump($data);

Output:

array(2) { [0]=> array(2) { ["name"]=> string(1) "a" ["surname"]=> string(1) "b" } [1]=> &array(2) { ["name"]=> string(1) "a" ["surname"]=> string(1) "b" } }

7 Comments

Not having enough space to post code is not a valid reason to post it as an answer.
@AmalMurali This is a reproducible test case. How would you suggest I post this code? Edit the original question? Seems a bit destructive to me. If you read the discussion you link you will see there are additional opinions, more practical and inline with my post.
@FritsvanCampen: Who said anything about editing the question? This is the same code the OP had in the question (unless I'm missing something). You could have added a demo in eval.in and linked it in the comments. Anyway, this is a bit off-topic here, so if you want to discuss it further, please feel free to open a Meta discussion about it.
@FritsvanCampen, You don't the have right to use such swear word. Flagged your comment as Rude / Offensive
|

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.