Jump to content

[SOLVED] Rename Array Key


rhyspaterson

Recommended Posts

Hey guys,

 

I have a multidimensional array as follows:

 

Array
(
    [line1] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
            [4] => e
            [5] => f
        )
    [foo] => Array
        (
            [0] => h
            [1] => i
            [2] => j
            [3] => k
            [4] => l
            [5] => m
        )
    [bar] => Array
        (
            [0] => n
            [1] => o
            [2] => p
            [3] => q
            [4] => r
            [5] => s
        )
);

 

I would like to rename the foo and bar keys to line2 and line3 respectively - however i have absolutely no idea. Any suggestions?

 

Cheers :)

Link to comment
https://forums.phpfreaks.com/topic/112713-solved-rename-array-key/
Share on other sites

I don't understand the problem? 

 

Array
(
    [line1] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
            [4] => e
            [5] => f
        )
    [line2] => Array
        (
            [0] => h
            [1] => i
            [2] => j
            [3] => k
            [4] => l
            [5] => m
        )
    [line3] => Array
        (
            [0] => n
            [1] => o
            [2] => p
            [3] => q
            [4] => r
            [5] => s
        )
);

The array has already been created. I want to modify the name of the two keys. See below:

 

$data = array(
'line1'	=> array('a', 'b', 'c', 'd', 'e', 'f'),
);

for ($x = 0; $x < 2; $x++){
$data_temp = array('a', 'b', 'c', 'd', 'e', 'f');
data[] = $data_temp;
}

// this will output

Array
(
    [line1] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
            [4] => e
            [5] => f
        )
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
            [4] => e
            [5] => f
        )
    [1] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
            [4] => e
            [5] => f
        )
);

 

I have an array called $data, i then append a second array called $data_temp onto it. However it gives the arrays the names of 0 and 1, but i would like to change them. I'm fairly sure you can't give them names in the FOR loop..

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.