1

Anyone can help me about multi dimensional arrays? I have an array that wanted to random arrays inside it, so therefore its hierarchy or there index value will be changed and different from the original arrangement of arrays, like:

This is the original arrays

Array(
    [0]=>Array(
             [title]       => 'Title 1'
             [description] => 'description here'
         )
    [1]=>Array(
             [title]       => 'Title 2'
             [description] => 'another description here'           
         )
    [2]=>Array(
             [title]       => 'Title Here Again'
             [description] => 'description here again'           
         )
)

That will be the original structure of the array above, and if you random it, let say this will be the outcome

This is the random-ed arrays

 Array(
        [0]=>Array(
                 [title]       => 'Title 2'
                 [description] => 'another description here'
             )
        [1]=>Array(
                 [title]       => 'Title 3'
                 [description] => 'another description again'           
             )
        [2]=>Array(
                 [title]       => 'Title 1'
                 [description] => 'description here'          
             )
    )

As you can see, values inside the arrays are being randomize at different positions, now the problem is i can't get the exact logic on how to get the original array index like this -> ( [0] ) from randomized arrays. Like the value 'Title 1' its original index is [0], and after it was random-ed it became [2] but still i wanted 'Title 1' being assigned to index [0]. Here's a short php code on how i randomized the arrays:

foreach (shuffleThis($rss->getItems()) as $item) {

    foreach($item as $key=>$value){
        if($key=='title'){
                $title=$value;
        }
        if($key=='description'){
            $description=$value;    
        }
    }
}
function shuffleThis($list) { 
  if (!is_array($list)) return $list; 

  $keys = array_keys($list); 
  shuffle($keys); 
  $random = array(); 
  foreach ($keys as $key) { 
    $random[] = $list[$key]; 
  }
  return $random; 
}

Just wanted to get they keys original array index from being random-ed.

Thanks!

2 Answers 2

2

If I understand you correctly, you want to change the order of the elements but preserve the keys. You can do that with a small modification:

function shuffleThis($list) { 
    if (!is_array($list)) return $list; 

    $keys = array_keys($list); 
    shuffle($keys); 
    $random = array(); 
    foreach ($keys as $key) { 
        $random[$key] = $list[$key]; // CHANGE HERE that preserves the keys
    }
    return $random; 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! this is a great answer, when i tried it, i can see that the elements are changing in position but not the array keys. But how would i able to get the key index number like this [0], the zero inside?
Not sure what you are asking. How to get which key, and given what information?
1

Use strings as keys and function 'shuffle'.
Example of shuffling: http://www.php.net/manual/en/function.shuffle.php#104430

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.