7

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
}`
5
  • 5
    You can't do that. You can't have an array with multiple values with the same key. Commented May 16, 2019 at 9:37
  • 1
    You cannot have indexes with the same value. You have multiple positions with the same value - they are not unique, and can therefor not be used as indexes in the array. Commented May 16, 2019 at 9:38
  • 1
    Nice catch Nick! Didn't see that at first glance Commented May 16, 2019 at 9:38
  • 1
    @Nick yes it's a typo. Commented May 16, 2019 at 9:41
  • 1
    So what will happen if two have the same position then? Commented May 16, 2019 at 9:42

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Your working demo is not working since it has removed two items
@Andreas It's a Typo.
3

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()

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.