Jump to content

[SOLVED] Combining multidimensional arrays


Schlo_50

Recommended Posts

Hello,

 

I have 2 arrays; $carList which contains 3 cars and their attributes, and $playerSelected which contains details of 1 car belonging to a human player.

 

Arrays:

<?php

$carList = array( 
    array( 
        'manufacturer' => 'Mitsubishi', 
        'model' => 'Colt', 
        'modexp' => '1200',
        'traction' => '500',
        'aerodynamics' => '3000',
        'acceleration' => '1500',
        'styling' => '6000' 
    ), 
    array( 
        'manufacturer' => 'Mitsubishi', 
        'model' => 'Evolution', 
        'modexp' => '45000',
        'traction' => '500',
        'aerodynamics' => '3000',
        'acceleration' => '1500',
        'styling' => '6000'
    ), 
    array( 
        'manufacturer' => 'Toyota', 
        'model' => 'MR2', 
        'modexp' => '25000',
        'traction' => '500',
        'aerodynamics' => '3000',
        'acceleration' => '1500',
        'styling' => '6000'
    )
);

$playerSelected = array( 
        'manufacturer' => 'Toyota', 
        'model' => 'Celica', 
        'modexp' => '1200000',
        'traction' => '500000',
        'aerodynamics' => '3000000',
        'acceleration' => '1500000',
        'styling' => '6000000' 
);

/>

 

In my code there is a function to randomly pick 2 cars from the $carList array. What I want to do is after the random pair of cars are generated, combine my $playerSelected array into that. I've tried array_push and array_merge but can't quite get it right.

 

Function:

<?php

function getRandomSubset($carList, $n) 
{ 
    /* 
     * If $n isn't greater than the number of entries, 
     * reset it to be the number of entries available. 
     */ 
    $n = (count($carList)<$n) ? count($carList) : $n; 
  
    $randList = array();

    // Pull the desired number of entry keys at random 
    $keys = array_rand($carList, $n);

    // Loop through the keys 
    foreach($keys as $key) { 

        // Add the value to end of the return array 
        array_push($randList, $carList[$key]); 

    } 

       //My not so great attempt on the next line..
array_push($randList, $playerSelected);

    return $randList; 

}

echo '<pre>';
//This just displays the two randomly generated cars
$randomSet = getRandomSubset($carList, 2);
echo '</pre>'; 

/>

 

Is there anyone who can help me please?

If you need more info just say!

 

Thanks

Link to comment
Share on other sites

What do you mean by 'combine'? Do you with your player array to contain two cars afterwards? If so, you need to make your player array a two dimensional one. I.e:

 

$playerSelected = array();
$playerSelected[] = array( 
        'manufacturer' => 'Toyota', 
        'model' => 'Celica', 
        'modexp' => '1200000',
        'traction' => '500000',
        'aerodynamics' => '3000000',
        'acceleration' => '1500000',
        'styling' => '6000000' 
);

 

 

You can then add a new car in the same vain as above;

 

$playerSelected[] = $carList[$key];

 

Link to comment
Share on other sites

Well, my function will output 2 random cars from the initial 3 like this:

 

Array
(
    [0] => Array
        (
            [manufacturer] => Toyota
            [model] => Aygo
            [modexp] => 950
            [traction] => 500
            [aerodynamics] => 3000
            [acceleration] => 1500
            [styling] => 6000
        )

    [1] => Array
        (
            [manufacturer] => Nissan
            [model] => 350Z
            [modexp] => 30000
            [traction] => 500
            [aerodynamics] => 3000
            [acceleration] => 1500
            [styling] => 6000
        )

)

 

I want to add my players car to the array above to end up with this:

 

Array
(
    [0] => Array
        (
            [manufacturer] => Toyota
            [model] => Aygo
            [modexp] => 950
            [traction] => 500
            [aerodynamics] => 3000
            [acceleration] => 1500
            [styling] => 6000
        )

    [1] => Array
        (
            [manufacturer] => Nissan
            [model] => 350Z
            [modexp] => 30000
            [traction] => 500
            [aerodynamics] => 3000
            [acceleration] => 1500
            [styling] => 6000
        )

    [2] => Array
        (
            [manufacturer] => Toyota
            [model] => Celica
            [modexp] => 1200000
            [traction] => 5000000
            [aerodynamics] => 3000000
            [acceleration] => 15000000
            [styling] => 6000000
        )


)

Link to comment
Share on other sites

<?php

function getRandomSubset($carList, $playerSelected, $n) 
{ 
    /* 
     * If $n is greater than the number of values in the array, 
     * reset it to be the number of entries available. 
     */ 
    $i = count($carList) - 1;
    $n = ($i < $n) ? $i : $n; 
  
    $randList = array();

    // Pull the desired number of entry keys at random 
    $keys = array_rand($carList, $n);

    // Loop through the keys 
    foreach($keys as $key) { 

        // Add the value to end of the return array 
        array_push($randList, $carList[$key]); 

    } 

   //Push the $playerSelected array on to the generated array
   array_push($randList, $playerSelected);

    return $randList; 

}
// Set arrays of cars
$carList = array( 
    array( 
        'manufacturer' => 'Mitsubishi', 
        'model' => 'Colt', 
        'modexp' => '1200',
        'traction' => '500',
        'aerodynamics' => '3000',
        'acceleration' => '1500',
        'styling' => '6000' 
    ), 
    array( 
        'manufacturer' => 'Mitsubishi', 
        'model' => 'Evolution', 
        'modexp' => '45000',
        'traction' => '500',
        'aerodynamics' => '3000',
        'acceleration' => '1500',
        'styling' => '6000'
    ), 
    array( 
        'manufacturer' => 'Toyota', 
        'model' => 'MR2', 
        'modexp' => '25000',
        'traction' => '500',
        'aerodynamics' => '3000',
        'acceleration' => '1500',
        'styling' => '6000'
    )
);

$playerSelected = array( 
        'manufacturer' => 'Toyota', 
        'model' => 'Celica', 
        'modexp' => '1200000',
        'traction' => '500000',
        'aerodynamics' => '3000000',
        'acceleration' => '1500000',
        'styling' => '6000000' 
);

// Generate car array
$randomSet = getRandomSubset($carList, $playerSelected, 2);

echo '<pre>';
/*
   This just displays the two randomly generated cars
   And the selected players car...
*/
print_r($randomSet);
echo '</pre>'; 

?>

Link to comment
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.