Solved

Multidimensional array & grouping

Posted on 2008-10-21
3
347 Views
Last Modified: 2008-10-21
I'm trying to output the following strings based on the array below, but can't figure out how to group the values together:

'Option Number = 465, Option Name = CHTZMO / DYUIOP';
'Option Number = 568, Option Name = CHTZMO / PLOIYU';

Array

(

    [23] => Array

        (

            [aid] => Item A

            [gid] => 1

            [po] => Array

                (

                    [465] => Array

                        (

                            [0] => Array

                                (

                                    [itemid] => 884

                                    [value] => CHTZMO

                                    [identifier] => 98765

                                    [gp] => 187

                                    [type] => select

                                    [attid] => 1086

                                )

 

                            [1] => Array

                                (

                                    [itemid] => 884

                                    [value] => DYUIOP

                                    [name] => 98765

                                    [gp] => 187

                                    [type] => select

                                    [attid] => 1497

                                )

 

                        )

						

		[568] => Array

                        (

                            [0] => Array

                                (

                                    [itemid] => 884

                                    [value] => CHTZMO

                                    [identifier] => 98765

                                    [gp] => 187

                                    [type] => select

                                    [attid] => 1086

                                )

 

                            [1] => Array

                                (

                                    [itemid] => 884

                                    [value] => PLOIYU

                                    [name] => 98769

                                    [gp] => 187

                                    [type] => select

                                    [attid] => 1765

                                )

 

                        )

				)

		)

)

Open in new window

0
Comment
Question by:n00b0101
3 Comments
 
LVL 10

Expert Comment

by:wildzero
ID: 22765706
Are all the items going to be under the elemnt
[po] => Array ?

So...

Foreach ($myarray['23']['po'] as $key=>$details) {
  echo "Option Number = " . $key . "  Option Name = " . $details[0]['value'] . "/" . $details[0]['value'] . "\n<br />";
}

Are you able to display more of the array? I don't see a 568...
0
 

Author Comment

by:n00b0101
ID: 22765798
The thing is though, the values in '23' will change, and there very well could be more than 2 iterations within that 465.  So, in the case of the example below, the first string would like this:

'Option Number = 465, Option Name = CHTZMO / DYUIOP / YTRION'

And the second:

'Option Number = 568, Option Name = CHTZMO / PLOIYU'

I'm trying to output the following strings based on the array below, but can't figure out how to group the values together:
 

'Option Number = 465, Option Name = CHTZMO / DYUIOP';

'Option Number = 465, Option Name = CHTZMO / PLOIYU';
 

Array

(

    [23] => Array

        (

            [aid] => Item A

            [gid] => 1

            [po] => Array

                (

                    [465] => Array

                        (

                            [0] => Array

                                (

                                    [itemid] => 884

                                    [value] => CHTZMO

                                    [identifier] => 98765

                                    [gp] => 187

                                    [type] => select

                                    [attid] => 1086

                                )
 

                            [1] => Array

                                (

                                    [itemid] => 884

                                    [value] => DYUIOP

                                    [name] => 98765

                                    [gp] => 187

                                    [type] => select

                                    [attid] => 1497

                                )

							

							[2] => Array

                                (

                                    [itemid] => 884

                                    [value] => YTRION

                                    [name] => 98791

                                    [gp] => 187

                                    [type] => select

                                    [attid] => 1797

                                )

								

							[3] => Array

                                (

                                    [itemid] => 884

                                    [value] => FYNOPO

                                    [name] => 98891

                                    [gp] => 187

                                    [type] => select

                                    [attid] => 8797

                                )
 

                        )

						

		[568] => Array

                        (

                            [0] => Array

                                (

                                    [itemid] => 884

                                    [value] => CHTZMO

                                    [identifier] => 98765

                                    [gp] => 187

                                    [type] => select

                                    [attid] => 1086

                                )
 

                            [1] => Array

                                (

                                    [itemid] => 884

                                    [value] => PLOIYU

                                    [name] => 98769

                                    [gp] => 187

                                    [type] => select

                                    [attid] => 1765

                                )
 

                        )

		)

	)

)

Open in new window

0
 
LVL 9

Accepted Solution

by:
absx earned 500 total points
ID: 22766109
Hi,

The example code outputs:

Handling foo number 23:
'Option number = 465, CHTZMO / DYUIOP';
'Option number = 568, CHTZMO / PLOIYU';
<?

$foo = array(

	23 => array(

		"aid" => "Item A",

		"gid" => 1, 

		"po" => array(

			465 => array(

				0 => array(

					"itemid" => 884,

					"value" => "CHTZMO", 

					"identifier" => 98765,

					"gp" => 187, 

					"type" => "select", 

					"attid" => 1086

				), 

				1 => array(

					"itemid" => 884,

					"value" => "DYUIOP", 

					"identifier" => 98765,

					"gp" => 187, 

					"type" => "select", 

					"attid" => 1086

				)

			),

			568 => array(

				0 => array(

					"itemid" => 884,

					"value" => "CHTZMO", 

					"identifier" => 98765,

					"gp" => 187, 

					"type" => "select", 

					"attid" => 1086

				), 

				1 => array(

					"itemid" => 884,

					"value" => "PLOIYU", 

					"identifier" => 98769,

					"gp" => 187, 

					"type" => "select", 

					"attid" => 1765

				)

			)

		)

	)

);
 

foreach($foo as $foonum => $subfoo){

	echo "Handling foo number $foonum: \n";

	foreach($subfoo["po"] as $option => $po){

		$output =  "'Option number = $option, ";

		foreach($po as $subpo) {

			$output .= $subpo["value"]." / ";

		}

		$output = substr($output,0,-3);

		echo $output."';\n";

	}

}

?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now