Jump to content

delete values from an array and place those values in another array


kalster

Recommended Posts

using php, how to delete values from an array and place those values in another array.
Is it possible to take an array like this...
Array ( [0] => Array ( [0] => var1 [1] => 1 [2] => var2 [3] => 2 [4] => 3 ) )

and make two arrays like this...
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )
Array ( [0] => Array ( [0] => var1 [1] => var2 ) )

The word "var" inside of the array does not change value but numbers can change values.

If var1 and var2 never change then you can pre-construct that array then create the third array from the difference

$arr = array (
            array('var1', 1, 'var2', 2, 3)
        );
$result[0] = array ('var1', 'var2');    //pre-construct

$result[1] = array_values(array_diff($arr[0], $result[0]));

echo '<pre>',print_r($result, true),'</pre>';

gives

Array
(
    [0] => Array
        (
            [0] => var1
            [1] => var2
        )

    [1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.