0

I'm trying to rename my keys in a multidimensional array. I looked at this: Renaming the keys in multidimensional associate arrays and it does rename it, but only for 1 of my 2 arrays. How can I get it to rename the 1st array time and the second array count?

My output right now is :

Array
(
    [0] => Array
        (
            [0] => 00:00
            [1] => 00:15
            [2] => 00:30
        )

    [1] => Array
        (
            [0] => 8
            [1] => 9
            [2] => 8
        )
)

I need [0] to be time and [1] to be count.

If I use this:

foreach ($sliced_array as $id => $dataset) {
    $newArray["time"] = $dataset;
}

I can get it to output only array [1] renamed as time. It should be count and array [0] disappears entirely. Is there a way to focus which dataset the foreach targets? dataset[0] does not work.

My expected output is:

Array
(
    [time] => Array
        (
            [0] => 00:00
            [1] => 00:15
            [2] => 00:30
        )

    [count] => Array
        (
            [0] => 8
            [1] => 9
            [2] => 8
        )
)
1
  • Your expected output result looks like? Commented May 15, 2014 at 20:56

2 Answers 2

3

Did you just try:

$myArray["time"] = $myArray[0];
$myArray["count"] = $myArray[1];
unset($myArray[0]);
unset($myArray[1]);

or just:

$newArray["time"] = $myArray[0];
$newArray["count"] = $myArray[1];

?

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

Comments

2
$newArray = array_combine(array('time','count'),$sliced_array);

https://www.php.net/array_combine

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.