Domanda

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
        )
)
È stato utile?

Soluzione

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];

?

Altri suggerimenti

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

http://es1.php.net/array_combine

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top