0

I have array like this.

A=array();
A[0]=>name = "John";
A[0]=>lastname = "Blabla";
A[0]=>genre = "Male";
A[1]=>name = "Cheryl";
A[1]=>lastname = "Blabla";
A[1]=>genre = "Female";

I want to shuffle this array with preserving key-value pairs and without mixing every key. So basicly A[0] will be A[1](there are more than only 2 index just example it should be random) with all of child keys values etc.

How can i do this? Thanks

4
  • 2
    Can you give this a language tag too? Commented Jun 6, 2016 at 15:50
  • Sorry forgot that it is PHP Commented Jun 6, 2016 at 15:53
  • Is there a reason why you can't use array_rand? Commented Jun 6, 2016 at 15:58
  • I think you are asking for a the implementation of specific solution to a more general problem. You aren't actually sharing what your problem is. So whatever solution you pick here is based on your own possibly erroneous guidance on the solution. If you share your actual problem, you might get a much better solution. Just a suggestion. Commented Jun 6, 2016 at 16:23

3 Answers 3

1

You can loop through the array and then randomly exchange the values in it.

for($x=0;$x<count($array);$x++){
    $temp=$array[$x];
    $index=rand(0,count($array)-1);
    $array[x]=$array[$index];
    $array[$index]=$temp;
}

Working example below.

<?php
$array=array();
$array[0]['name'] = "John";
$array[0]['lastname'] = "Blabla";
$array[0]['genre'] = "Male";
$array[1]['name'] = "Cheryl";
$array[1]['lastname'] = "Blabla";
$array[1]['genre'] = "Female";
$array[2]['name'] = "Amy";
$array[2]['lastname'] = "Blabla";
$array[2]['genre'] = "Female";
$array[3]['name'] = "Adam";
$array[3]['lastname'] = "Blabla";
$array[3]['genre'] = "Female";
$array[4]['name'] = "Hitan";
$array[4]['lastname'] = "Blabla";
$array[4]['genre'] = "Male";
$array[5]['name'] = "Mary";
$array[5]['lastname'] = "Blabla";
$array[5]['genre'] = "Female";

for($x=0;$x<count($array);$x++){
    $temp=$array[$x];
    $index=rand(0,count($array)-1);
    $array[$x]=$array[$index];
    $array[$index]=$temp;
}

var_dump($array);
Sign up to request clarification or add additional context in comments.

2 Comments

Is it differ with shuffle($array)? As I can see it just N random swaps? May be I just can't to understand original issue.
I wasn't aware of shuffle function up to now. Yes, you can consider it so.
1

Just randomize the index of the array, no need to care about the specific field of object stored as array element.

public function randomObj(A)
{
    $index = rand(0, 1);
    return A[$index];
}

Add here as an extra explanation: shuffle means you swap certain amount of objects(which change the index of the object as an element in the array), while randomize means you get an object randomly(no need to change the index of object, but when you want to get one, it returns a random object).

7 Comments

php.net/manual/tr/function.shuffle.php From here people are posted for preserving key value pairs so i thought it randomize everything.
@SüleymanKenar I mean keep the element of object, while randomize the retrieval index.
I know i can random a number between 0 to array count and select random element but i need to randomize whole array. Or maybe i didn't understand what you mean sorry
@SüleymanKenar that's what i mean, maybe not what you ask for.
Oh sorry i didnt know difference between shuffle and randomize my bad english correcting question.
|
1

This function returns same array with shuffled iteration order:

function kshuffle($a) {
  $k = array_keys($a);
  shuffle($k);
  $res = [];
  foreach($k as $index) $res[$index] = $a[$index];
  return $res;
}

print_r(kshuffle(['a', 'b', 'c', 'd', 'e', 'f', 'g']));

Array
(
    [4] => e
    [1] => b
    [3] => d
    [0] => a
    [6] => g
    [5] => f
    [2] => c
)

So, it will produce shuffled sequence, while we iterate result with foreach.
It also will generate hashtable/object if you want to json_encode result: {"1": "b", "0": "a"}. But be careful, if random returns unshuffled sequence(there is always probability for this event) it will plain array: ['a', 'b'] without JSON_FORCE_OBJECT flag.

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.