codeinphp Posted February 21, 2016 Share Posted February 21, 2016 Attempting to put multiple arrays together but having difficulty. The array looks like following: $items=array ( 0 => array ( 'start' => '201601221400', 'title' => 'FABLife', 'desc' => 'Mark Cuban (``Shark Tank\'\'); top five must-haves; collectors try to guess the prices of celebrity memorabilia; creating a high-end playroom and eliminating toy clutter without breaking the bank.', ), 1 => array ( 'start' => '201601221400', 'title' => 'The First 48', 'desc' => 'A young man is robbed and killed while meeting up with a girl he met earlier; a man is gunned down outside an annual football game.', ), 2 => array ( 'start' => '201601221400', 'title' => 'Teen Titans Go!', 'desc' => 'Robin makes the other Titans sell their treasured mementos from past adventures.', ), 3 => array ( 'start' => '201601221400', 'title' => 'The Talk', 'desc' => 'Actor William H. Macy; guest co-host Jane Kaczmarek; chef David LeFevre makes ricotta cavatelli pasta with clams and herbed bread crumbs.', ), 17 => array ( 'start' => '201601221500', 'title' => 'General Hospital', 'desc' => 'Jason recovers an important memory; Lulu turns to her mother for advice; Sonny keeps a breakthrough to himself; Nikolas makes a move.', ), 18 => array ( 'start' => '201601221500', 'title' => 'The First 48', 'desc' => 'Two out-of-town brothers get involved in a drug deal gone wrong; forensic evidence must be used to piece together a deadly shooting when people refuse to talk.', ), 19 => array ( 'start' => '201601221500', 'title' => 'Top Gun', 'desc' => 'A hot-shot Navy jet pilot (Tom Cruise) tangles with MiGs and flirts with a civilian astrophysicist (Kelly McGillis).', ), The array continues but I have only pasted part of it. I want to combine arrays based on the start element retaining each title and desc for each array. I anticipate the final array to look like: $items=array ( 0 => array ( 'start' => '201601221400', 'title' => 'FABLife', 'desc' => 'Mark Cuban (``Shark Tank\'\'); top five must-haves; collectors try to guess the prices of celebrity memorabilia; creating a high-end playroom and eliminating toy clutter without breaking the bank.', 'title' => 'The First 48', 'desc' => 'A young man is robbed and killed while meeting up with a girl he met earlier; a man is gunned down outside an annual football game.', 'title' => 'Teen Titans Go!', 'desc' => 'Robin makes the other Titans sell their treasured mementos from past adventures.', ), array ( 'start' => '201601221500', 'title' => 'General Hospital', 'desc' => 'Jason recovers an important memory; Lulu turns to her mother for advice; Sonny keeps a breakthrough to himself; Nikolas makes a move.', 'title' => 'The First 48', 'desc' => 'Two out-of-town brothers get involved in a drug deal gone wrong; forensic evidence must be used to piece together a deadly shooting when people refuse to talk. 'title' => 'Top Gun', 'desc' => 'A hot-shot Navy jet pilot (Tom Cruise) tangles with MiGs and flirts with a civilian astrophysicist (Kelly McGillis).', ), When I try to put the array together I either end up with an array like the initial array at top of post or one like $items=array ( 0 => array ( 'start' => '201601221400', ), 1 => array ( 'title' => 'FABLife', 'desc' => 'Mark Cuban (``Shark Tank\'\'); top five must-haves; collectors try to guess the prices of celebrity memorabilia; creating a high-end playroom and eliminating toy clutter without breaking the bank.', ), 2 => array ( 'title' => 'The First 48', 'desc' => 'A young man is robbed and killed while meeting up with a girl he met earlier; a man is gunned down outside an annual football game.', ), The above would be ok except the title and desc are in separate arrays from the start. Is there a way to combine the start and each title/desc into single array based on start? Quote Link to comment Share on other sites More sharing options...
requinix Posted February 21, 2016 Share Posted February 21, 2016 First there's a problem: 'title' => 'FABLife', 'desc' => 'Mark Cuban (``Shark Tank\'\'); top five must-haves; collectors try to guess the prices of celebrity memorabilia; creating a high-end playroom and eliminating toy clutter without breaking the bank.', 'title' => 'The First 48', 'desc' => 'A young man is robbed and killed while meeting up with a girl he met earlier; a man is gunned down outside an annual football game.', 'title' => 'Teen Titans Go!', 'desc' => 'Robin makes the other Titans sell their treasured mementos from past adventures.',Can't do that. There can only be one "title" and "desc" in the array. You need to come up with a different way of representing everything. I suggest 'info' => array( array( 'title' => 'FABLife', 'desc' => 'Mark Cuban (``Shark Tank\'\'); top five must-haves; collectors try to guess the prices of celebrity memorabilia; creating a high-end playroom and eliminating toy clutter without breaking the bank.' ), array( 'title' => 'The First 48', 'desc' => 'A young man is robbed and killed while meeting up with a girl he met earlier; a man is gunned down outside an annual football game.' ), array( 'title' => 'Teen Titans Go!', 'desc' => 'Robin makes the other Titans sell their treasured mementos from past adventures.' ) ) Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted February 21, 2016 Solution Share Posted February 21, 2016 (edited) array keys/indexes must be unique, so your anticipated result is not possible. not knowing why are you doing this, what you are ultimately using the data for, which would produce the best result, i would recommend making an array with the start value as the main array key and the (start)/title/description data as sub-arrays under any start value. it would look like - $data['201601221400'][0] = array('start' => '201601221400', 'title' => 'FABLife', 'desc' => 'Mark Cuban (``Shark Tank\'\'); top five must-haves; collectors try to guess the prices of celebrity memorabilia; creating a high-end playroom and eliminating toy clutter without breaking the bank.' ); $data['201601221400'][1] = array('start' => '201601221400', 'title' => 'The First 48', 'desc' => 'A young man is robbed and killed while meeting up with a girl he met earlier; a man is gunned down outside an annual football game.' ); $data['201601221400'][2] = array('start' => '201601221400', 'title' => 'Teen Titans Go!', 'desc' => 'Robin makes the other Titans sell their treasured mementos from past adventures.' ); you would produce this by looping through the data, using the 'start' value as the main array key and appending an array consisting of the start, title and desc - $data = array(); foreach($items as $arr){ $data[$arr['start']][] = $arr; } echo '<pre>'; print_r($data); Edited February 21, 2016 by mac_gyver 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.