-1

I have this array

 $the_posted = Array
            (
    0 => Array
        (
            0 => 1,
            1 => 2,
            2 => 3,
            3 => 4,
        ),

    1 => Array
        (
            0 => 5,
            1 => 6,
            2 => 7,
            3 => 8,
        )

);

whose keys i need to modify.I trying to modify the array keys like

$all_array_keys = array_keys($the_posted);

 foreach ( array_keys($the_posted) as $k=>$v )
{
  $all_array_keys[$k]= rand();
}

  echo '<pre>';
  print_r($all_array_keys);
  echo "<hr/>";
  print_r($the_posted);
  echo '<pre>';

I get this result

Array
(
    [0] => 25642
    [1] => 8731
)

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

    [1] => Array
        (
            [0] => 5
            [1] => 6
            [2] => 7
            [3] => 8
        )

)

The change in keys is not reflected in the final array.How do i make this work?.

  • What does the result you want look like? – JimL Sep 10 '13 at 08:33
  • possible duplicate of [In PHP, how do you change the key of an array element?](http://stackoverflow.com/questions/240660/in-php-how-do-you-change-the-key-of-an-array-element), [PHP Change Array Keys](http://stackoverflow.com/questions/308703/php-change-array-keys). – rid Sep 10 '13 at 08:33
  • 1
    what a name you got there.... – No Idea For Name Sep 10 '13 at 08:36
  • @rid: sorry but you are wrong. It is possible to change a key of an item. Please refer to my message below. – Scalpweb Sep 10 '13 at 08:37
  • I agree that it's not clean, but the final result is exactly what this guy is trying to do, I think... – Scalpweb Sep 10 '13 at 08:40
  • @rid: depends of the situation. Let's say you have a very large array, and only want to change a few keys. Building a completely new array would probably be more time consuming. That's probably a good thing to know how to use both solution. (and I actually do not care about downvote, I'm just trying to help ^^) – Scalpweb Sep 10 '13 at 08:42
  • 3
    @No Idea For Name I ran out of ideas too for a name. – You Know Nothing Jon Snow Sep 10 '13 at 08:48

2 Answers2

1

You can use following code :

foreach ( array_keys($the_posted) as $k=>$v )
{   
  $new_key = rand();
  $new_posted[$new_key] = $the_posted[$v];
  unset($the_posted[$v])
}

Here, we have created a new array $new_posted which will have data with new keys like this :

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

    [23341] => Array
        (
            [0] => 5
            [1] => 6
            [2] => 7
            [3] => 8
        )

)
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
  • And, if and only if you're memory constrained (maybe you don't have a lot of allocated memory for PHP and the array is very big), you can also `unset($the_posted[$k])` at each iteration, which would keep the same memory footprint, at the expense of performance. But if memory is not a concern, it should be significantly faster without the `unset()`. – rid Sep 10 '13 at 08:50
  • @rid : agreed with you. – Nishu Tayal Sep 10 '13 at 08:51
-1

To change the key of an item, do something like this:

$the_posted[$newkey] = $the_posted[$oldkey];
unset($the_posted[$oldkey]);

So in your case:

foreach ( $the_posted as $k=>$v )
{
    $newkey = rand();
    while(isset($the_posted[$newkey]))
       $newkey = rand();
    $the_posted[$newkey] = $the_posted[$k];
    unset($the_posted[$k]);
}
Scalpweb
  • 1,971
  • 1
  • 12
  • 14