Jump to content

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

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.