Moocowmedia Posted June 25, 2018 Share Posted June 25, 2018 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted June 25, 2018 Share Posted June 25, 2018 (edited) 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? Edited June 25, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 25, 2018 Share Posted June 25, 2018 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" } } Quote Link to comment Share on other sites More sharing options...
benanamen Posted June 25, 2018 Share Posted June 25, 2018 There is no point to creating separate arrays. You already have the data. Just use it. Quote Link to comment Share on other sites More sharing options...
Moocowmedia Posted June 25, 2018 Author Share Posted June 25, 2018 Hi, im only trying to create separate arrays so I can use in charts.js .. thanks for for your help Quote Link to comment Share on other sites More sharing options...
benanamen Posted June 25, 2018 Share Posted June 25, 2018 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). Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 26, 2018 Share Posted June 26, 2018 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.