Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding values to a multidimensional array PHP
    text
    copied!<p>I have tried this now about 20 different ways, set it down and picked it back up several times and I am at a loss for what I am doing wrong. I am trying to create an array of arrays from three separate arrays. The first array I iterate through is a list of dates. Then I iterate through a list of cabins (this is a simple camp scheduling program), and then I randomly assign an activity to each of the cabins from a third array.</p> <p>The three arrays are <code>$cabins</code>, <code>$activities</code>, and <code>$checkeddates</code></p> <pre><code>//swaps key-value so cabin names are keys $cabins = array_flip( $cabins ); //sets each key value pair to be cabinname=&gt; null erase_val( $cabins ); foreach ( $checkeddates as $dates ) { $uberarray[] = $dates; } $uberarray = array_flip( $uberarray ); foreach ( $uberarray as $k =&gt; $v ) { $uberarray[$k] = $cabins; } </code></pre> <p>At this point, a var_dump shows that I have an array of dates each containing an array of cabins names with null values, from this point I have tried a dozen things and this is just where I left it, trying a process of elimination to figure out what it isn't.</p> <pre><code>foreach ( $uberarray as $k =&gt; $v ) { shuffle( $activities ); $s = $activities[0]; $uberarray[] = $s; } </code></pre> <p>In the end, I was thinking to build the large array (with some additional rules not yet written in to prevent multiple assignments of the same thing and such. And then as one function I would iterate through the established combinations in the array to write it to the database. </p> <p><strong>Edit</strong>: Request for how I want it to look in the end:</p> <pre><code>array 'ThuJan1st' =&gt; array 'cabin 1' =&gt; randomactivity1 'cabin 2' =&gt; randomactivity2 'cabin 3' =&gt; randomactivity3 </code></pre> <p>Currently I have 2 out of 3, but I can not seem to randomly add the activities as values.</p> <p><strong>Edit</strong>: This is what @kunal 's suggestion produced: </p> <pre><code>array 'ThuJan1st' =&gt; array 'cabin 1' =&gt; randomactivity1 'cabin 2' =&gt; randomactivity2 'cabin 3' =&gt; randomactivity3 array 'FriJan2nd' =&gt; array 'cabin 1' =&gt; randomactivity4 'cabin 2' =&gt; Null 'cabin 3' =&gt; Null </code></pre> <p>Once it iterated through activities a single time, nothing else was assigned a value.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload