如何使用 PHP 中子数组 属性 值的子数组更改数组中的索引

How to change indexes in array with sub arrays with subarray property value in PHP

我需要帮助。我有一系列这样的项目:

[7646] => Array
    (
        [0] => Array
            (
                [id] => 156153
                [tmplvarid] => 5
                [value] => 2
            )

        [1] => Array
            (
                [id] => 56795
                [tmplvarid] => 7
                [value] => 430
            )

        [2] => Array
            (
                [id] => 56798
                [tmplvarid] => 19
                [value] => rate_08
            )
    ),
[7647] => Array ()

我需要将子数组中的数组索引更改为 属性 tmplvarid 的值以像这样转换数组:

`[7646] => Array
    (
        [5] => Array
            (
                [id] => 156153
                [tmplvarid] => 5
                [value] => 2
            )

        [7] => Array
            (
                [id] => 56795
                [tmplvarid] => 7
                [value] => 430
            )

        [19] => Array
            (
                [id] => 56798
                [tmplvarid] => 19
                [value] => rate_08
            )
    ) 

如何将其转换为关联数组?

使用foreach loop

从数组值设置索引值

代码

<?PHP
  $arr = [
    "7646" => array
    (
        [
           
                "id"=> 156153,
                "tmplvarid" => 5,
                "value" => 2
            
        ],
        [
           
                "id"=> 56795,
                "tmplvarid" => 7,
                "value" => 430
            
        ],
        [
           
                "id"=> 56798,
                "tmplvarid" => 19,
                "value" => "rate_08"
            
        ]
    )
];
echo "<pre>";
print_r($arr);
$newarr= [];
foreach($arr as $key => $value)
{
    foreach($value as $key1 => $value1)
    {
        $newarr[$key][$value1['tmplvarid']] = $value1;
    }
}
print_r($newarr);
?>

输出

Array
(
    [7646] => Array(
        [5] => Array
        (
            [id] => 156153
            [tmplvarid] => 5
            [value] => 2
        )
        [7] => Array
        (
            [id] => 56795
            [tmplvarid] => 7
            [value] => 430
        )
        [19] => Array
        (
            [id] => 56798
            [tmplvarid] => 19
            [value] => rate_08
        )
    )
)