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 8843087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:07:45+00:00 2026-06-14T11:07:45+00:00

I have an array which looks like this: array(10) { [0]=> string(10) 2012-11-03 [1]=>

  • 0

I have an array which looks like this:

array(10) {
  [0]=>
  string(10) "2012-11-03"
  [1]=>
  string(1) "1"
  [2]=>
  string(10) "2012-11-04"
  [3]=>
  string(1) "3"
  [4]=>
  string(10) "2012-11-05"
  [5]=>
  string(1) "2"
  [6]=>
  string(10) "2012-11-06"
  [7]=>
  string(1) "7"
  [8]=>
  string(10) "2012-11-07"
  [9]=>
  string(1) "4"
}

I would like to get a new multidimensional array from this which would have 5 elements, where each element would look like this: $date => $number.

array(5) {
      [0]=> array(2012-11-03 => 1)
      [1]=> array(2012-11-04 => 3)
      [2]=> array(2012-11-05 => 2)
      [3]=> array(2012-11-06 => 7)
      [4]=> array(2012-11-07 => 4)
    }

I would like to use the dates as keys to te values that come after them. (I would eventually like to plot these values on a line chart, where x axis has the date and y has the value)

What kind of a (foreach?) loop can I write to do this?

I am getting this array from the following lines of code:

$data = "$start_date\n$value\n";
file_put_contents($id . '.csv', $data, FILE_APPEND);
$data = file_get_contents($id . '.csv');
$data_array = explode("\n", trim($data, "\n"));
var_dump($data_array); exit;
  • 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-14T11:07:50+00:00Added an answer on June 14, 2026 at 11:07 am

    I assume that your array contains an even number of elements, as expected.

    You can use the following snippet:

    <?php
    
    $newArray = [];
    for ($i = 0; $i < count($data_array); $i += 2) {
        $newArray[$data_array[$i]] = $data_array[$i + 1];
    }
    

    ^ Above code says that we start at index 0 of $data_array, which is a date. It makes a key in newArray from the even element, and attaches the value of the next odd element to this key. The array will look like this:

    Array(
        '2012-11-03' => "1",
        '2012-11-04' => "3",
        '2012-11-05' => "2",
        ...
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which is converted to string and it looks like this:
I have an array of arrays, which looks something like this: [[Some string, Some
I have an array which looks like this: $arr = ( [0]=>Int(2) [1]=>Array( ....)
I have an array which looks like this: $data = array( array('timestamp' => 1312776000,
I have a PHP array which looks like this example: $array[0][0] = 'apples'; $array[0][1]
I have an array stored in a variable temp which looks like this: temp.inspect
I have an associative array which when var dumped looks like this: Array (
I have a a JSON-encoded Array which looks like this (note: this is in
I have a String which looks like this: [xxx,yyy,zzz] Can anyone give me a
I have a string which looks like this: email@domain1.com|email@domain5.com I need only the email

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.