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
https://forums.phpfreaks.com/topic/74270-solved-php-arrays/
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
https://forums.phpfreaks.com/topic/74270-solved-php-arrays/#findComment-375265
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
https://forums.phpfreaks.com/topic/74270-solved-php-arrays/#findComment-375304
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
https://forums.phpfreaks.com/topic/74270-solved-php-arrays/#findComment-375309
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.