Jump to content

splitting and combining a multiarray?


jasonstairs

Recommended Posts

I am a newbie and have been trying really hard to figure this out but am now at a loss.  I have the following array:

 

ARRAY:  Array
(
    [0] => Array
        (
            [Field] => Array
                (
                    [Chemistry] => Array
                        (
                            [Physical Chemistry] => Array
                                (
                                    [Jason Stairs] => Array
                                        (
                                            [0] => time-of-flight mass spectrometry
                                            [1] => computer programming
                                            [2] => 
                                            [3] => 
                                            [4] => 
                                            [5] => 
                                            [6] => 
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [Field] => Array
                (
                    [Materials] => Array
                        (
                            [Materials Chemistry] => Array
                                (
                                    [Roshan Shrestha] => Array
                                        (
                                            [0] => awesome
                                            [1] => cool
                                            [2] => super
                                            [3] => Rock and roll
                                            [4] => 
                                            [5] => 
                                            [6] => 
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [Field] => Array
                (
                    [Physics] => Array
                        (
                            [Nuclear Physics] => Array
                                (
                                    [Laura Stonehill] => Array
                                        (
                                            [0] => Dark matter
                                            [1] => Neutrino
                                            [2] => Low-background
                                            [3] => Pulse-shape analysis
                                            [4] => Detector development
                                            [5] => 
                                            [6] => 
                                        )

                                )

                        )

                )

        )

    [3] => Array
        (
            [Field] => Array
                (
                    [Chemistry] => Array
                        (
                            [inorganic Chemistry] => Array
                                (
                                    [Robert Smith] => Array
                                        (
                                            [0] => 1
                                            [1] => 2
                                            [2] => 3
                                            [3] => 4
                                            [4] => 
                                            [5] => 
                                            [6] => 
                                        )

                                )

                        )

                )

        )

    [4] => Array
        (
            [Field] => Array
                (
                    [Physics] => Array
                        (
                            [Astronomy] => Array
                                (
                                    [Redding Ryan] => Array
                                        (
                                            [0] => 
                                            [1] => 
                                            [2] => 
                                            [3] => 
                                            [4] => 
                                            [5] => 
                                            [6] => 
                                        )

                                )

                        )

                )

        )

    [5] => Array
        (
            [Field] => Array
                (
                    [Chemistry] => Array
                        (
                            [Physical Chemistry] => Array
                                (
                                    [Tome Jones] => Array
                                        (
                                            [0] => 
                                            [1] => 
                                            [2] => 
                                            [3] => 
                                            [4] => 
                                            [5] => 
                                            [6] => 
                                        )

                                )

                        )

                )

        )

)

 

What I would like to do is merge all of the [Field] keys into one so that all of the [chemistry]s and [physics] etc would all appear under [Field] with no overwrites and containing all of their info.  I thought that I could do this by splitting the array into a number of separate arrays (at the [0], [1], etc keys) and then array_merge_recursive, but I don't know how to split the main array into the separate arrays and I don't know if this is the best way to do it anyway...?

 

Ultimately I want to have my php read the data entered into the MySql database (that is how I got the above array) and then use a funtion similar to this:

 

function CDwriter($FieldList, $Name)
{

foreach ($FieldList as $OptionKey => $Option) //$OptionKey is Field?  $Option is disc.?
{
	if (is_array($Option))
	{
		$str .= "<li><a href=\"#\">$OptionKey</a>\n\t<ul>\n";  // write Field html
		foreach ($Option as $OptionValue) //for each of the disc's
		{
			$str .= "\t\t<li><a href=\"#\">$OptionValue</a>\n\t\t\t<ul>\n"; // write disc line
			foreach ($Name as $NameValue) // for each name in that disc
			{
				$str .= "\t\t\t\t<li><a href=\"#\">$NameValue</a></li>\n"; //write name html
			}
			$str .= "\t\t\t</ul>\n\t\t</li>\n"; // close name <ul> and disc <li>
		}
		$str .= "\t</ul>\n</li>\n"; // close disc <li>
	}
}
$str .= "</ul>\n"; //close field <ul> and <li>
return $str;
}

 

where $FieldList is my manipulated array, in order to create html code that looks similar to this:

 

<li><a href="#">Chemistry</a>
<ul>
	<li><a href="#">Physical Chemistry</a>

		<ul>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
		</ul>
	</li>

	<li><a href="#">Inorganic Chemistry</a>
		<ul>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>

		</ul>
	</li>
</ul>
</li>
<li><a href="#">Materials</a>
<ul>
	<li><a href="#">Materials Chemistry</a>
		<ul>
			<li><a href="#"></a></li>

			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
		</ul>
	</li>
	<li><a href="#">Analytical Chemistry</a>

		<ul>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
			<li><a href="#"></a></li>
		</ul>
	</li>

</ul>
</li>

etc, where the names of the people in the appropriate fields and disciplines are listed.

 

Any help would be greatly appreciated, particularly with how to manipulate that first array as well as any suggestions on better ways to tackle the problem in general.

 

Thanks!

Link to comment
Share on other sites

try

<?php
$a = Array(
    '0' => Array(
            'Field' => Array(
                    'Chemistry' => Array(
                            'Physical Chemistry' => Array                                (
                                    'Jason Stairs' => Array('time-of-flight mass spectrometry', 'omputer programming', '' ,'' ,'', '', '')
                                )
                        )
                )
        ),
    '1' => Array(
            'Field' => Array(
                    'Materials' => Array(
                            'Materials Chemistry' => Array(
                                    'Roshan Shrestha' => Array('awesome', 'cool', 'super', 'Rock and roll', '', '', '')
                                )
                        )
                )
        ),
    '2' => Array(
            'Field' => Array(
                    'Physics' => Array(
                            'Nuclear Physics' => Array(
                                    'Laura Stonehill' => Array('Dark matter', 'Neutrino', 'Low-background', 'Pulse-shape analysis', 'Detector development', '', '')
                                )
                        )
                )
        ),
    '3' => Array(
            'Field' => Array(
                    'Chemistry' => Array(
                            'Inorganic Chemistry' => Array(
                                    'Robert Smith' => Array( 1, 2, 3, 4, '', '', '')
                                )
                        )
                )
        ),
    '4' => Array(
            'Field' => Array(
                    'Physics' => Array(
                            'Astronomy' => Array(
                                    'Redding Ryan' => Array( '', '', '', '', '', '', '')
                                )
                        )
                )
        ),
    '5' => Array(
            'Field' => Array(
                    'Chemistry' => Array(
                            'Physical Chemistry' => Array(
                                    'Tome Jones' => Array( '', '', '', '', '', '', '')
                                )
                        )
                )
        )
);

$out = array();
foreach ($a as $field) {
foreach ($field['Field'] as $dep => $subdep){
	if (isset($out[$dep])) $out[$dep] = array_merge_recursive($out[$dep],$subdep) ;
	else $out[$dep] = $subdep;
}
}
print_r($out);
?>

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.