1

I have an array that looks like this:

array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(10) "2012-11-14"
    [1]=>
    string(5) "3238"
  }
  [1]=>
  array(2) {
    [0]=>
    string(10) "2012-11-13"
    [1]=>
    string(5) "3231"
  }
  [2]=>
  array(2) {
    [0]=>
    string(10) "2012-11-13"
    [1]=>
    string(5) "3231"
  }

I would like to write a foreach loop that would turn this array into:

array(2) {
  [0]=>
  array(1) {
    "2012-11-14" => "3238"
  }
  [1]=>
  array(1) {
   "2012-11-13" => "3231"
  }

So, basically, I would like to use the array element formatted as Y-M-D date as key to the second element in the array.

4

5 回答 5

2
<?php

$data = array(
    array("2012-11-14", "3238"),
    array("2012-11-13", "3231"),
    array("2012-11-13", "3231") // warning! when there are two record with same date, the second's count will be display
);

$result = array();
foreach ($data as $value) {
    $result[$value[0]] = $value[1];
}

echo '<pre>';
print_r($result);
于 2012-11-14T15:52:42.230 回答
2

Given the following array...

$array = array(
    0 => array(0 => "2012-11-14", 1 => "3238"),
    1 => array(0 => "2012-11-13", 1 => "3231"),
    2 => array(0 => "2012-11-13", 1 => "3231"),
);

putting it into a new array like this:

$new_array = array();
foreach ($array as $key => $item)
{
    $new_array[$key][$item[0]] = $item[1];
}

print_r($new_array);

produces this output:

Array
(
[0] => Array
    (
        [2012-11-14] => 3238
    )

[1] => Array
    (
        [2012-11-13] => 3231
    )

[2] => Array
    (
        [2012-11-13] => 3231
    )

)

My answer doesn't get rid of the duplicates, but the added dimension as specified in the original question means that duplicate dates as keys aren't an issue.

于 2012-11-14T15:58:16.087 回答
0
<?php
    $newArray = array();
    for($i=0;$i<count($arrayVariable);$i++)
    {
       $newArray[$arrayVariable[$i][0]] = $arrayVariable[$i][1];
    }

echo '<pre>';print_r($newArray);echo '</pre>';
?>

Didn't test it but something like this should work in concept. Of course change arrayVariable to your variable.. but that aside.

于 2012-11-14T15:55:17.683 回答
0

You can use this code to get what you want:

$dates = array(
    array("2012-11-01", "3238"),
    array("2012-11-03", "4321")
);

print_r($dates);

$result = array();
foreach($dates as $value) {
    $result[][$value[0]] = $value[1];
}


print_r($result);

The output will look like the requested form:

Array
(
    [0] => Array
        (
            [2012-11-01] => 3238
        )

    [1] => Array
        (
            [2012-11-03] => 4321
        )

)

Codepad demo: http://codepad.org/XAmUEdYh

However, I would personally prefer Aykut's solution. You would of course have a problem when you've got two records with the same date, but the overall array layout is a bit nicer ;).

于 2012-11-14T15:58:58.923 回答
0

Here is what I came up with:

<?php
$original = array(
    array(
        "2012-11-14",
        "3238"
    ),
    array(
        "2012-11-13",
        "3231"
    ),
    array(
        "2012-11-13",
        "3231"
    )
);

$newArray = array();
foreach($original as $subArray){
    $newArray[] = array($subArray[0] => $subArray[1]);
}
var_dump($newArray);
于 2012-11-14T16:01:50.227 回答