Iftikhar uddin
Iftikhar uddin

Reputation: 3182

How to change keys of a array in PHP?

I have the below array:

array(10) {
  [0]=>
  array(2) {
    ["id"]=>
    string(2) "73"
    ["position"]=>
    string(1) "1"
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(2) "58"
    ["position"]=>
    string(1) "2"
  }
  [2]=>
  array(2) {
    ["id"]=>
    string(2) "82"
    ["position"]=>
    string(1) "3"
  }
  [3]=>
  array(2) {
    ["id"]=>
    string(2) "84"
    ["position"]=>
    string(1) "4"
  }
  [4]=>
  array(2) {
    ["id"]=>
    string(2) "74"
    ["position"]=>
    string(1) "5"
  }
  [5]=>
  array(2) {
    ["id"]=>
    string(2) "59"
    ["position"]=>
    string(1) "6"
  }
  [6]=>
  array(2) {
    ["id"]=>
    string(2) "72"
    ["position"]=>
    string(1) "7"
  }
  [7]=>
  array(2) {
    ["id"]=>
    string(2) "78"
    ["position"]=>
    string(1) "7"
  }
  [8]=>
  array(2) {
    ["id"]=>
    string(2) "77"
    ["position"]=>
    string(1) "8"
  }
  [9]=>
  array(2) {
    ["id"]=>
    string(2) "71"
    ["position"]=>
    string(1) "8"
  }
}

I want the keys indexes of array to be replaced with position values. The output should be like below:

array(10) {
  [1]=>
  array(2) {
    ["id"]=>
    string(2) "73"
    ["position"]=>
    string(1) "1"
  }
  [2]=>
  array(2) {
    ["id"]=>
    string(2) "58"
    ["position"]=>
    string(1) "2"
  }
  [3]=>
  array(2) {
    ["id"]=>
    string(2) "82"
    ["position"]=>
    string(1) "3"
  }
  [4]=>
  array(2) {
    ["id"]=>
    string(2) "84"
    ["position"]=>
    string(1) "4"
  }
  [5]=>
  array(2) {
    ["id"]=>
    string(2) "74"
    ["position"]=>
    string(1) "5"
  }
  [6]=>
  array(2) {
    ["id"]=>
    string(2) "59"
    ["position"]=>
    string(1) "6"
  }
  [7]=>
  array(2) {
    ["id"]=>
    string(2) "72"
    ["position"]=>
    string(1) "7"
  }
  [7]=>
  array(2) {
    ["id"]=>
    string(2) "78"
    ["position"]=>
    string(1) "7"
  }
  [8]=>
  array(2) {
    ["id"]=>
    string(2) "77"
    ["position"]=>
    string(1) "8"
  }
  [8]=>
  array(2) {
    ["id"]=>
    string(2) "71"
    ["position"]=>
    string(1) "8"
  }
}

I tried the below code but it just prints one element in array:

$newarr = array();

$values = $this->request->get( 'values', null );

foreach ($values as $oldkey => $value) {
    $position = $value["position"];
    $newarr[$position] = $values[$oldkey];
    $values=$newarr;
    unset($newarr);
}
var_dump($values);exit;

var_dump result of $values is

<br />
<b>Notice</b>:  Undefined offset: 8 in ResourcesController.php</b> 
<b>Notice</b>:  Undefined offset: 9 in ResourcesController.php</b> on line <b>367</b><br />
array(1) {
  [8]=>
  NULL
}`

Upvotes: 6

Views: 7299

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

You can use array_combine() along with range() and count()

$array = array_combine(range(1, count($arr)), $arr);

Output:- https://3v4l.org/k0XTj

Reference:

array_combine()

range()

count()

Upvotes: 3

Rahul
Rahul

Reputation: 18557

array_column will be enough to help you with.

$result = array_column($yourarray, null, 'position');

column_key The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be NULL to return complete arrays or objects (this is useful together with index_key to reindex the array).

Syntax

array_column ( array $input , mixed $column_key [, mixed $index_key = NULL ] ) : array

Working demo.

Upvotes: 5

Related Questions