Jump to content

[SOLVED] foreach array


Bifter

Recommended Posts

I cannot figure out why this wont work - am I missing something?

 

<?php
$products = array();
foreach ($L['products'] as $array) {



$products[$array['product-id']['value']] = $array['product-name']['value'] = $array['technology-type']['value'];
}
foreach ($products as $id => $name => $ttype) {



echo '<input type="radio" name="product" value="' . $id . '" /><span>' . $name . '</span> <span>' . $ttype . '</span><br />';
}
?>

 

Thanks for your time.

Link to comment
https://forums.phpfreaks.com/topic/159381-solved-foreach-array/
Share on other sites

$L stores the array:

 

[products] => Array
        (
            [0] => Array
                (
                    [product-id] => Array
                        (
                            [format] => counting
                            [value] => 1235
                        )

                    [product-name] => Array
                        (
                            [format] => text
                            [value] => Home 512k Classic
                        )

                    [technology-type] => Array
                        (
                            [format] => text
                            [value] => adsl
                        )

                    [capped] => Array
                        (
                            [format] => yesno
                            [value] => Y
                        )

                    [service-speed] => Array
                        (
                            [format] => counting
                            [value] => 524288
                        )

                    [contention] => Array
                        (
                            [format] => counting
                            [value] => 50
                        )

                    [care-availability] => Array
                        (
                            [format] => yesno
                            [value] => Y
                        )

                    [care-level] => Array
                        (
                            [format] => text
                            [value] => standard
                        )

                    [realms] => Array
                        (
                            [0] => Array
                                (
                                    [realm] => Array
                                        (
                                            [format] => text
                                            [value] => -
                                        )

                                )

                        )

                )

 

Parse error: syntax error, unexpected T_DOUBLE_ARROW, expecting ')' in /home/ipendpoi/public_html/connect/xml.php on line 106

Link to comment
https://forums.phpfreaks.com/topic/159381-solved-foreach-array/#findComment-840683
Share on other sites

You should probably leave a link to your last topic, so people can get an idea of what you're trying to do.

 

You've misunderstood how to use the foreach construct. $id and $name are just holders for the key and value of each element in the array $products. To access the technology-type value from your original array, you'll have to add it to $products in the first foreach construct. Here's a way to do it:

 

<?php
$products = array();
foreach ($L['products'] as $array) {
//add needed info to our new array
$products[] = array(
	'id' => $array['product-id']['value'],
	'name' => $array['product-name']['value'],
	'ttype' => $array['technology-type']['value']
);
}
//output info stored in arrays in $products
foreach ($products as $array) {
echo '<input type="radio" name="product" value="' . $array['id'] . '" /><span>' . $array['name'] . ' ' . $array['ttype'] . '</span><br />';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/159381-solved-foreach-array/#findComment-840685
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.