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.

2
  • Do duplicate date keys always point to the same value, and if not, how do you choose? Commented Nov 14, 2012 at 15:52
  • yes, they always point to the same value! Commented Nov 14, 2012 at 15:55

5 Answers 5

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.

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

Comments

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

Comments

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.

Comments

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 ;).

Comments

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

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.