Best way to create a multidimensional array w/this loop?

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

So I have a loop that cycles through some data that I am pulling from an API. Whats the best way to create a multidimensional array with it? In the past I just make the array with array_push() but I am not sure how to do the same with multidimensional arrays. The array that I need to build is below and the loop looks something like this:


The loop:

PHP Code: [ Select ]
foreach ($x as $value)
{
$name = $value[0];
$number1 = $value[1];
$number2 = $value[2];
$number3 = $value[3];
}
 
  1. foreach ($x as $value)
  2. {
  3. $name = $value[0];
  4. $number1 = $value[1];
  5. $number2 = $value[2];
  6. $number3 = $value[3];
  7. }
  8.  


The multidimensional array:
PHP Code: [ Select ]
$data = array(
  array('John', 24, 54, 38),
  array('Mark', 67, 22, 57),
  array('Tim', 69, 32, 58),
  array('Sarah', 81, 78, 68),
  array('Susan', 16, 44, 38),
);
 
  1. $data = array(
  2.   array('John', 24, 54, 38),
  3.   array('Mark', 67, 22, 57),
  4.   array('Tim', 69, 32, 58),
  5.   array('Sarah', 81, 78, 68),
  6.   array('Susan', 16, 44, 38),
  7. );
  8.  
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

You can push entries onto the end of an array by simply assigning to the array with an empty index:

PHP Code: [ Select ]
$data = array();
foreach ($x as $value) {
  $data[] = array($value[0], $value[1], $value[2], $value[3]);
}
  1. $data = array();
  2. foreach ($x as $value) {
  3.   $data[] = array($value[0], $value[1], $value[2], $value[3]);
  4. }
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

spork wrote:
You can push entries onto the end of an array by simply assigning to the array with an empty index:

PHP Code: [ Select ]
$data = array();
foreach ($x as $value) {
  $data[] = array($value[0], $value[1], $value[2], $value[3]);
}
  1. $data = array();
  2. foreach ($x as $value) {
  3.   $data[] = array($value[0], $value[1], $value[2], $value[3]);
  4. }



Yeah, that will work to create a normal array, but not a multidimensional one like I have above.
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

spork wrote:
You can push entries onto the end of an array by simply assigning to the array with an empty index:

PHP Code: [ Select ]
$data = array();
foreach ($x as $value) {
  $data[] = array($value[0], $value[1], $value[2], $value[3]);
}
  1. $data = array();
  2. foreach ($x as $value) {
  3.   $data[] = array($value[0], $value[1], $value[2], $value[3]);
  4. }


oh, wait, my bad spork, I see what you are saying now... So keeping the index empty will allow each loop to create a different array "object"?
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

I'm confused; the loop I provided creates the exact structure you indicated.
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

spork wrote:
I'm confused; the loop I provided creates the exact structure you indicated.

yah, I see that now. My bad, and thanks a ton.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 88 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.