This is because you're re-assigning $dataPoints as a new array on each loop.

Change it to:

$dataPoints[] = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);

This will append a new array to the end of $dataPoints

Answer from Hamish on Stack Overflow
🌐
Matt Doyle
elated.com › home › blog › using multidimensional arrays in php
Using Multidimensional Arrays in PHP
July 23, 2022 - Here’s an example that creates a 2-dimensional array of movie information, then loops through the array, displaying the information in the page: $movies = array( array( "title" => "Rear Window", "director" => "Alfred Hitchcock", "year" => 1954 ), array( "title" => "Full Metal Jacket", "director" => "Stanley Kubrick", "year" => 1987 ), array( "title" => "Mean Streets", "director" => "Martin Scorsese", "year" => 1973 ) ); foreach ( $movies as $movie ) { echo '<dl style="margin-bottom: 1em;">'; foreach ( $movie as $key => $value ) { echo "<dt>$key</dt><dd>$value</dd>"; } echo '</dl>'; }
🌐
Laracasts
laracasts.com › discuss › channels › general-discussion › how-to-create-multidimensional-array-with-loop
How to Create multidimensional array with loop
Array ( [week1] => Array ( [0] => '25' [1] => '25' [2] => '30' [3] => '65' [4] => '59' ) [week2] => Array ( [0] => '25' [1] => '25' [2] => '30' [3] => '65' [4] => '59' ) [week3] => Array ( [0] => '25' [1] => '25' [2] => '30' [3] => '65' [4] => '59' ) [week4] => Array ( [0] => '25' [1] => '25' [2] => '30' [3] => '65' [4] => '59' ) [week5] => Array ( [0] => '25' [1] => '25' [2] => '30' [3] => '65' [4] => '59' ) ) ... Please sign in or create an account to participate in this conversation.
🌐
W3Schools
w3schools.com › php › php_arrays_multidimensional.asp
PHP Multidimensional Arrays
For a three-dimensional array you need three indices to select an element · To loop through a multidimensional array, use a for loop or a foreach loop.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › php › multidimensional-arrays-in-php
Multidimensional arrays in PHP - GeeksforGeeks
July 11, 2025 - It is the simplest form of a multidimensional array. It can be created using nested array. These type of arrays can be used to store any type of elements, but the index is always a number. By default, the index starts with zero. ... Example: In this example we creates a two-dimensional array containing names and locations. The print_r() function is used to display the structure and contents of the array, showing the nested arrays and their values. ... <?php // PHP program to create // multidimensional array // Creating multidimensional // array $myarray = array( // Default key for each will // start from 0 array("Ankit", "Ram", "Shyam"), array("Unnao", "Trichy", "Kanpur") ); // Display the array information print_r($myarray); ?>
🌐
Tutorial Republic
tutorialrepublic.com › faq › foreach-loop-through-multidimensional-array-in-php.php
Foreach Loop through Multidimensional Array in PHP
<?php // Multidimensional array $superheroes = array( "spider-man" => array( "name" => "Peter Parker", "email" => "peterparker@mail.com", ), "super-man" => array( "name" => "Clark Kent", "email" => "clarkkent@mail.com", ), "iron-man" => array( "name" => "Harry Potter", "email" => "harrypotter@mail.com", ) ); // Printing all the keys and values one by one $keys = array_keys($superheroes); for($i = 0; $i < count($superheroes); $i++) { echo $keys[$i] .
🌐
FlatCoding
flatcoding.com › home › php multidimensional arrays: data manipulation
PHP Multidimensional Arrays: Data Manipulation - FlatCoding
April 15, 2025 - In this example, we created a nested multidimensional array called $employees that contains three arrays, each representing an employee. Each employee array contains four key-value pairs, including a projects key, which contains an array of the projects the employee is working on. To retrieve the project names for each employee, we can employ another nested loop. Let’s see an example: <?php // Loop through each employee in the $employees array foreach ($employees as $employee) { // Print out the employee name and department echo $employee["name"] .
Top answer
1 of 2
2

I don't think this is a restrict Wordpress question, but you might try

$args = array(
    'posts_per_page' => '-1',
    'post_type' => 'work',
    'orderby' => 'ID',
    'order' => 'DESC',
);

$data = array('work' => array());

$loop = new WP_Query($args);
if( $loop->have_posts() ):
    while( $loop->have_posts() ): $loop->the_post();
        $id = $loop->post->ID;

        $attachments=array();
        if(get_field('work')): 
           while(has_sub_field('work')):
              $attachment_id = get_sub_field('image');
              $caption = the_sub_field('caption');
              $image = wp_get_attachment_image_src( $attachment_id, 'work' );   
              $attachments[$attachment_id]=array("caption"=>$caption,"url"=>$image); 
           endwhile;    
        endif; 

        $data['work'][$id] = array(
            'title' => apply_filters( 'the_title', $loop->post->post_title ),
            'content' => apply_filters( 'the_content', $loop->post->post_content ),           
            'items'  => $attachments;  
        );
    endwhile;
endif;
wp_reset_postdata();
2 of 2
0

Put the second loop in a function:

$loop = new WP_Query($args);
if( $loop->have_posts() ):
    while( $loop->have_posts() ): $loop->the_post();
        $id = $loop->post->ID;
        $data['work'][$id] = array(
            'title' => apply_filters( 'the_title', $loop->post->post_title ),
            'content' => apply_filters( 'the_content', $loop->post->post_content ),
            'items' => get_acf_field( $id ),
        );

    endwhile;
endif;

function get_acf_field( $post_id ) {

  $result = array();

  if( get_field( 'work', $post_id ) ) {

    while(has_sub_field('work')) {

      $attachment_id = get_sub_field('image');
      $caption = the_sub_field('caption');
      $image = wp_get_attachment_image_src( $attachment_id, work );

      $result[] = array( 'caption' => $caption, 'url' => $image );

    }
  }

  return $result;

}

I hope this helps.

🌐
Stack Overflow
stackoverflow.com › questions › 41557112 › create-multidimensional-array-in-php-with-dynamic-data
for loop - Create Multidimensional Array in PHP with Dynamic Data - Stack Overflow
January 9, 2017 - This will be defining our primary and main Multidimensional Array `$total_meals_array`. for ($y = 1; $y <= $days_served; $y++) { //once inside the code, now is the time to define/populate our secondary array that will be used as primary array's key value. `$i`, which is the meal count of each day, will be added to the key name to make it easier to read it later. This will be repeated `$meals_count` times. for ($i = 1; $i <= $meals_count; $i++) { $meals_selected_array["meal_id_" .
🌐
DevOpsSchool.com
devopsschool.com › blog › loop-with-multidimensional-numeric-array
How to Print Multidimensional Numeric Array in PHP using loop? -
May 8, 2022 - Foreach Loop with Multidimensional Numeric:- You can use Nested Foreach loop through Multidimensional arrays in multiple arrays for store index value as Nested For Loop in PHP.
🌐
SitePoint
sitepoint.com › php
Advanced loops with multi-dimensional array - PHP - SitePoint Forums | Web Development & Design Community
April 5, 2012 - I’m fairly good in PHP so I understand the main concept of everything. One problem I cant seem to wrap my head around is random dimensions of multidimensional arrays with for loops using the key and value. Here is an example. for example lets say I have something I grabbed from a database and I looped through and placed each entry into an array which looks like this array[0][“id”] = something array[0][“name”] = name array[1][“id”] = something else… array[1][“name”] = another name I can loo...
🌐
HCL GUVI
studytonight.com › php-howtos › foreach-loop-through-multidimensional-array-in-php
HCL GUVI | Learn to code in your native language
Take your tech career to the next level with HCL GUVI's online programming courses. Learn in native languages with job placement support. Enroll now!