-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?.

7
  • What does the result you want look like? Commented Sep 10, 2013 at 8:33
  • possible duplicate of In PHP, how do you change the key of an array element?, PHP Change Array Keys. Commented Sep 10, 2013 at 8:33
  • 1
    what a name you got there.... Commented Sep 10, 2013 at 8:36
  • @rid: sorry but you are wrong. It is possible to change a key of an item. Please refer to my message below. Commented Sep 10, 2013 at 8:37
  • 3
    @No Idea For Name I ran out of ideas too for a name. Commented Sep 10, 2013 at 8:48

2 Answers 2

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
        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

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 : agreed with you.
-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]);
}

1 Comment

True. I updated the code to fix the issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.