Jump to content

[SOLVED] PHP Array's


ShibSta

Recommended Posts

I have a small question... If I have the 2 following array's how can I make them into one?

 

		$temp_data = array ('2007' => array (
						'10' => array (
							'20' => array (
								"Uniques" => $temp_uniques
								),
							),
						),
					);
	$temp_data2 = array ('2007' => array (
						'10' => array (
							'21' => array (
								"Uniques" => $temp_uniques
								),
							),
						),
					);

 

I want it to become:

		$temp_data = array ('2007' => array (
						'10' => array (
							'20' => array (
								"Uniques" => $temp_uniques
								),
							),
							'21' => array (
								"Uniques" => $temp_uniques
								),
							),
						),
					);

 

(The array's will be generated at separate times so It's not static code... Otherwise I'd put them in 1 array to being with... However, I am not sure what array function I would need to use...

 

Thanks,

- ShibSta

Link to comment
Share on other sites

I think you want to do an array_merge($temp_data['2007']['10'], $temp_data2['2007']['10']);

 

Problem is the array is dynamically created and will variate..

 

So it will be like:

		$temp_data = array ('2007' => array (
						'10' => array (
							'30' => array (
								"Uniques" => $temp_uniques
								),
							),
						),
					);
	$temp_data2 = array ('2007' => array (
						'10' => array (
							'31' => array (
								"Uniques" => $temp_uniques
								),
							),
						),
					);
	$temp_data3 = array ('2007' => array (
						'11' => array (
							'01' => array (
								"Uniques" => $temp_uniques
								),
							),
						),
					);
	$temp_data3 = array ('2008' => array (
						'01' => array (
							'01' => array (
								"Uniques" => $temp_uniques
								),
							),
						),
					);

 

I would need this to become:

		$temp_data = array ('2007' => array (
						'10' => array (
							'30' => array (
								"Uniques" => $temp_uniques
								),
							),
							'31' => array (
								"Uniques" => $temp_uniques
								),
							),
						'11' => array (
							'01' => array (
								"Uniques" => $temp_uniques
								),
							),
						),
					'2008' => array (
						'01' => array (
							'01' => array (
								"Uniques" => $temp_uniques
								),
							),
					);

 

So I need it to merge the entire base array if possible...

 

Link to comment
Share on other sites

array_merge($temp_data, $temp_data2);

 

Thanks, but that is 1) renaming the keys and 2) not merging them the way I want... It outputs:

Array
(
    [0] => Array
        (
            [10] => Array
                (
                    [01] => Array
                        (
                            [uniques] => 449.0
                        )

                )

        )

    [1] => Array
        (
            [10] => Array
                (
                    [02] => Array
                        (
                            [uniques] => 771.0
                        )

                )

        )
)

 

I need it to output:

Array
(
    [2007] => Array
        (
            [10] => Array
                (
                    [01] => Array
                        (
                            [uniques] => 449.0
                        )

                )

            [10] => Array
                (
                    [02] => Array
                        (
                            [uniques] => 771.0
                        )

                )

        )
)

 

(Ignore the Uniques Value...)

 

Grats on your 11,500th post...

Link to comment
Share on other sites

try

<?php
$temp_data = array ('2007' => array (
						'10' => array (
							'30' => array (
								"Uniques" => $temp_uniques
								),
							),
						),
					);
$temp_data2 = array ('2007' => array (
				'10' => array (
					'31' => array (
						"Uniques" => $temp_uniques
						),
					),
				),
			);
$temp_data3 = array ('2007' => array (
				'11' => array (
					'01' => array (
						"Uniques" => $temp_uniques
						),
					),
				),
			);
$temp_data4 = array ('2008' => array (
				'01' => array (
					'01' => array (
						"Uniques" => $temp_uniques
						),
					),
				),
			);

function my_merge (&$data, &$data2)
{
    foreach ($data2 as $y => $ydata)
    {
        foreach ($ydata as $m => $mdata)
        {
            foreach ($mdata as $d => $ddata)
            {
                $data[$y][$m][$d] = $ddata;
            }
        }
    }
}                

my_merge ($temp_data, $temp_data2);
my_merge ($temp_data, $temp_data3);
my_merge ($temp_data, $temp_data4);

echo '<pre>', print_r($temp_data, true), '</pre>';                
?>

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.