Skip to content Skip to sidebar Skip to footer

Combine Arrays In Php

Trying to learn arrays in PHP. Snippets posted for brevity. HTML form here:

What are your favorite type of cookies?

Solution 1:

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.

Solution 2:

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.

Solution 3:

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];
     }
     elseif(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.

Solution 4:

This script works (was ripped off from a book and modified forthis 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

Post a Comment for "Combine Arrays In Php"