Change the array KEY to a value from sub array

This is the set of result from my database

print_r($plan);
Array
(
    [0] => Array
        (
            [id] => 2
            [subscr_unit] => D
            [subscr_period] => 
            [subscr_fee] => 
        )

    [1] => Array
        (
            [id] => 3
            [subscr_unit] => M,Y
            [subscr_period] => 1,1
            [subscr_fee] => 90,1000
        )

    [2] => Array
        (
            [id] => 32
            [subscr_unit] => M,Y
            [subscr_period] => 1,1
            [subscr_fee] => 150,1500
        )

)

How can I change the $plan[0] to $plan[value_of_id]

Thank You.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

This won’t do it in-place, but:

$new_plan = array();
foreach ($plan as $item)
{
  $new_plan[$item['id']] = $item;
}

Solution 2

This may be a bit late but I’ve been looking for a solution to the same problem. But since all of the other answers involve loops and are too complicated imho, I’ve been trying some stuff myself.

The outcome

$items = array_combine(array_column($items, 'id'), $items);

It’s as simple as that.

Solution 3

You could also use array_reduce which is generally used for, well, reducing an array. That said it can be used to achieve an array format like you want by simple returning the same items as in the input array but with the required keys.

// Note: Uses anonymous function syntax only available as of PHP 5.3.0
//       Could use create_function() or callback to a named function
$plan = array_reduce($plan, function($reduced, $current) {
    $reduced[$current['id']] = $current;
    return $reduced;
});

Note however, if the paragraph above did not make it clear, this approach is overkill for your individual requirements as outlined in the question. It might prove useful however to readers looking to do a little more with the array than simply changing the keys.

Solution 4

Seeing the code you used to assemble $plan would be helpful, but I’m going assume it was something like this

while ($line = $RES->fetch_assoc()) {
    $plan[] = $line;
}

You can simply assign an explicit value while pulling the data from your database, like this:

while ($line = $RES->fetch_assoc()) {
    $plan[$line['id']] = $line;
}

This is assuming $RES is the result set from your database query.

Solution 5

In my opinion, there is no simpler or more expressive technique than array_column() with a null second parameter. The null parameter informs the function to retain all elements in each subarray, the new 1st level keys are derived from the column nominated in the third parameter of array_column().

Code: (Demo)

$plan = array_column($plan, null, 'id');

Note: this technique is also commonly used to ensure that all subarrays contain a unique value within the parent array. This occurs because arrays may not contain duplicate keys on the same level. Consequently, if a duplicate value occurs while using array_column(), then previous subarrays will be overwritten by each subsequent occurrence of the same value to be used as the new key.

Demonstration of “data loss” due to new key collision.

Solution 6

$plans = array();
foreach($plan as $item)
{
    $plans[$item['id']] = $item;
}

$plans contains the associative array.

This is just a simple solution.

Solution 7

$newplan = array();
foreach($plan as $value) {
    $id = $value["id"];
    unset($value["id"]);
    $newplan[$id] = $value;
}

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply