Jump to content

Combine Arrays-Not Merger


codeinphp
Go to solution Solved by mac_gyver,

Recommended Posts

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?

Link to comment
Share on other sites

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.'
	)
)
Link to comment
Share on other sites

  • Solution

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 by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.