Jump to content

Breaking php array into smaller arrays


Moocowmedia

Recommended Posts

Hello, I have an array that is looping a list of table values for example from a feed I am using

 

name.  Age. Weight

chris.  23. 12stone

paul.  28. 11stone

 

The array is outputting (Chris, 23, 12stone, Paul, 28, 11stone)

i am am trying to break the long array into 3 separate arrays. An array for name an array for age, an array for weight.

The data is from a feed that I can’t change

any help will be great 

Chris 

 

 

Link to comment
Share on other sites

use array_column()

$data = [['name' => 'aaa', 'age' => 23, 'weight'=> 50.6],
         ['name' => 'ccc', 'age' => 19, 'weight' => 67.6] 
         ];
$names = array_column($data, 'name');
$ages = array_column($data,'age');
$weights = array_column($data,'weight');

EDIT:

Why would you want to  do that?

Link to comment
Share on other sites

 

What is the benefit of having 3 separate arrays?

Here is some code to convert this into an array that would allow you to utilize Barand's solution although I really see no benefit in having 3 uncollated arrays.

<?php
$keys = array('name', 'age', 'weight');

$original = array('Chris', 23, '12stone', 'Paul', 28, '11stone');
foreach(array_chunk($original, 3) as $values) {
    $new[] = array_combine($keys, $values);
}

var_dump($new);

 

Output:

array(2) {
  [0]=>
  array(3) {
    ["name"]=>
    string(5) "Chris"
    ["age"]=>
    int(23)
    ["weight"]=>
    string(7) "12stone"
  }
  [1]=>
  array(3) {
    ["name"]=>
    string(4) "Paul"
    ["age"]=>
    int(28)
    ["weight"]=>
    string(7) "11stone"
  }
}

 

Link to comment
Share on other sites

And therein is the REAL problem which is not even a problem in this case. Same still applies, just use the data you already have.

On another note, I would like to point something out for your future benefit. Your original post is what is known as an XY Problem. In short, you asked how to solve X (your attempt) when you really should have been asking about Y (the real issue).

Link to comment
Share on other sites

1 minute ago, Moocowmedia said:

Hi, im only trying to create separate arrays so I can use in charts.js ..

 

thanks for for your help 

 

I agree with benanamen on this one.  It would have been helpful if you had stated that you needed to take the input data, which was described well, in order to feed it into one or more graphs you are going to render using Graph.js.  The specific graphs would be helpful to know as well.  

This would certainly have allowed people to provide you better and more specific advice.  Hopefully, this advice will aid you in the future as to how to ask more precise questions in the future!  ;)

To be clear though, given your described input, combine my solution with Barand's and you have the 3 arrays you are looking for on the serverside.  It might be of interest to other people if you can report back with the code you came up with to render the graphs.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.