Search code examples
phparrayslaravelstringlaravel-7

How to convert array keys into random string in php laravel


I need to convert Array keys into a random string, how is it possible ?

This is how my array displays with the keys

enter image description here

I need to display as below by using str_random(5)

array:2 [▼
  bataj => 229
  akgrt => 228
]

Here's the codes to dump the array

 $files = $this->filesRepo->getByUuids($request->photos);

 $fileId = $files->pluck('id')->toArray();

 dd($fileId);

Solution

  • $keys = array_map(function() {
        return str_random(5);
    }, $fileId);
    
    $fileId = array_combine($keys, array_values($fileId));
    
    dd($fileId);