开发者

Combine arrays in PHP

开发者 https://www.devze.com 2023-02-06 20:55 出处:网络
Trying to learn arrays in PHP. Snippets posted for brevity. HTML form here: <p>What are your favorite type of cookies?</p>

Trying to learn arrays in PHP. Snippets posted for brevity.

HTML form here:

<p>What are your favorite type of cookies?</p> 
<input type="checkbox" name="cookies[]" value="Oreos" />Oreos<br />
<input type="checkbox" name="cookies[]" value="Chocolate chip" />Chocolate chip<br />
<input type="checkbox" name="cookies[]" value="Sugar" />Sugar<br />
<input type="checkbox" name="cookies[]" value="Vanilla Mocha" />Vanilla Mocha<br /> 

<p>What are your favorite type of drinks?</p>
<input type="checkbox" name="drinks[]" value="Soda" />Soda<br />
<input type="checkbox" name="drinks[]" value="Wine" />Wine<br />
<input type="checkbox" name="drinks[]" value="Milk" />Milk<br />
<input type="checkbox" name="drinks[]" value="Water" />Water<br /> 

PHP page here:

foreach ($drinks as $d) {
    echo "Your favorite drink(s) are: " . $d . "<br />"; 
}

foreach ($cookies as $cookie) {
    echo "Your favorite cookies are: " . $cookie . "<br />"; 
} 

$experimentalArray = array($cookie => $d); 

foreach ($experimentalArray as $key => $value) { 
    echo "Cookie - " . $key . " Drink - " . $value . "<br /><br />"; 
} 

Both cookies and drinks are multi-choice questions, so you can select more than one answer.

However, the experimentalArray only shows the last answer chosen in both drink and cookie question.

For example, I choose Oreos and Chocolate Chip in cookies, and Soda and Wine in drinks.

The answer comes out as: "Cookie - Chocolate chip Drink - Wine"

Why is it not displaying all values?

Edited for a multi-dimensional script

<?php 

$drinks = $_POST['drinks']; 
$cookies = $_POST['cookies']; 

    $combinedArray = array( 'Cookies' => $cookies, 'Drinks' => $drinks); 

    foreach($combinedArray as $snackType => $snack) {
    print "<h2>$snackType</h2>";
    foreach ($snack as $number => $snackChosen) {
        print " $number is $snackChosen<br />";
    }

} ?>

Ok, so tried to do a multi-dimensional array script instead since the previous script wasn't going to obtain all the values as per the HTML form.

This script works (was ripped off from a book and modified for this code here), however,开发者_运维百科 $number value starts at 0. How do I modify that so that it starts at 1 instead?

Also, is this proper form for doing multi-dimensional array? Could it have been rewritten in a better way?

And again, thank you for all responses. Even if I don't quite understand them! :) So, thank you for your patience as well.


No idea which php tutorial you are using, but stop using it, it's probably horribly outdated!

You'll have to use the $_POST / $_GET / $_REQUEST (contains POST, GET and - depending on the config - Cookie values) arrays. register_globals is deprecated and a potential security hole if enabled.

For example, use $drinks = isset($_POST['drinks']) ? $_POST['drinks'] : array(); to get your $drinks array. Same for the $cookies one.


About your array issue. It looks like you want the keys from the drinks array and the values from hte cookies array. If yes, have a look at array_combine(). Note that it requires both arrays to have the same amount of elements though so feeding it with user-generated arrays where the length can vary is not a very good idea.

Fyi, $experimentalArray = array($cookie => $d); maps the last element from $cookies to the last element of $drinks because PHP has no block scope and thus $cookie and $d still point to the last array elements you got in your foreach loops.


There are a few problems here.

  1. When you iterate with foreach and you would like to perform actions for each element, the code need to be between the foreach's brakets.
  2. There is always a chance that the user will select a different number of cookies than drinks, which would cause problems because one food item wouldn't have a pair.

The solution that I propose is the array_combine() function to pair cookies with drinks after padding each of the arrays to the same length:

<?php
//$cookies = array_pad( $cookies , count( $drinks ), '(none)' ); Doesn't work, thanks for pointing this out @Phoenix
$drinks = array_pad( $drinks, count( $cookies ), '(none)' ); //If more cookies than drinks, add (none) drinks to account for extra cookies
$combined = array_combine( $cookies, $drinks ); //Combine arrays and cookies
?>

Of course this may seem a little complicated so let me explain. Let's say the user choses Sugar and Chocolate Chip Cookies with Milk and Water. These obviously aren't the tastiest choices, but they are the most optimal in this case. Since there are an equal number of cookies as there are drinks, PHP will simply pair up the choices with the array_combine() function. This function accepts an array of keys (cookies) and values ($drinks) and combines them into one. Ex.

array_combine( array( 'one', 'two' ), array( 1, 2 ) ) == array( 'one' => 1, 'two' => 2 );

We run into problems when the user chooses an unequal number of favorite snacks. This is when the first two lines come into play. The first line will add (none) to the cookie array for each extra drink. Ex.

array_pad( array( 'one' ), 2, '(none)' ) == array( 'one', '(none)' );

The next line does the same, but instead equals the drinks with the cookies (in the case there are more cookies selected than drinks).

Let me know if you need more examples.


What about the previous two? Do they work appropriately? I believe that the problem is here:

$experimentalArray = array($cookie => $d);

Which should be

$experimentalArray = array($cookies => $drinks);

I don't think I've ever made an array in that fashion though, not sure if the values of $cookies will automatically be the keys of $drinks.

If not, a method of generating such an array would be something like:

if(count($drinks) >= count($cookies))
{
     $count = count($drinks);
}
else
{
     $count = count($cookies);
}

for($i=1;$i<=$count;$i++)
{
     if(isset($drinks[$i]) && isset($cookies[$i]))
     {
          $experimentalArray[$cookies[$i]] = $drinks[$i];
     }
     else if(isset($drinks[$i]) && !isset($cookies[$i]))
     {
          $experimentalArray['None' . $i] = $drinks[$i];
     }
     else
     {
          $experimentalArray[$cookies[$i]] = 'None' . $i;
     }
}   

Or something like that, which accounts for if there are fewer of one type chosen than the other.


This script works (was ripped off from a book and modified for this code here), however, $number value starts at 0. How do I modify that so that it starts at 1 instead?

PHP always starts the Count at 0. You can read more about that at: http://php.net/manual/de/language.types.array.php

0

精彩评论

暂无评论...
验证码 换一张
取 消