Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:31:33+00:00 2026-06-14T07:31:33+00:00

I have an array that looks like this: array(3) { [0]=> array(2) { [0]=>

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:31:34+00:00Added an answer on June 14, 2026 at 7:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of type string that looks like this: test1|True,test2|False,test3|False,test4|True . This
I have a jagged array that looks like this: string[][] list = new string[d.Rows.Count
I have an array that looks like this: array(3) { [Fall Quarter 2012]=> array(2)
I have string that look like Array that fetched from other webservice like this
I have an array that looks like this: [ Object actions: Array[2] comments: Object
I have a array that looks like this Array ( [provider] => Array (
I have an array that looks like this: Array ( [0] => Array (
I have an array that looks like this, Array ( [0] => 849710414 [1]
I have an Array that looks like this: array( 0 => array( 'key1' =>
I have an array that looks like this (sample): Array ( [1600] => Array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.