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
}`