将特定数组值设置为多个值的数组键

时间:2016-10-26 06:14:10

标签: php arrays

我想将数组特定值设置为它的键,假设我有如下所示的数组:

$temp =  [0] => Array
        (
            [type] => 1
            [hash_id] => 
            [requested_user_amount] => 7250.88
            [requested_user_id] => 9
            [requested_user_rate] => 49800
            [requested_user_qty] => 0.1456

        )

    [1] => Array
        (
            [type] => 1
            [hash_id] => 
            [requested_user_amount] => 64740
            [requested_user_id] => 12
            [requested_user_rate] => 49800
            [requested_user_qty] => 1.3
        )
    [2] => Array
        (
            [type] => 1
            [hash_id] => 
            [requested_user_amount] => 5000
            [requested_user_id] => 17
            [requested_user_rate] => 49800
            [requested_user_qty] => 0.100401606
      )

我想将requested_user_id设置为数组键。

我找到了一个类似下面的解决方案

 array_column($temp, null, 'requested_user_id'); 

输出

[9] => Array
            (
                [type] => 1
                [hash_id] => 
                [requested_user_amount] => 7250.88
                [requested_user_id] => 9
                [requested_user_rate] => 49800
                [requested_user_qty] => 0.1456

            )

        [12] => Array
            (
                [type] => 1
                [hash_id] => 
                [requested_user_amount] => 64740
                [requested_user_id] => 12
                [requested_user_rate] => 49800
                [requested_user_qty] => 1.3
            )
        [17] => Array
            (
                [type] => 1
                [hash_id] => 
                [requested_user_amount] => 5000
                [requested_user_id] => 17
                [requested_user_rate] => 49800
                [requested_user_qty] => 0.100401606
          )

它会给出我想要的确切输出。但问题是函数array_column需要php 5.5而我的服务器是5.4。

通过搜索网页,我发现array_map()array_column()的替代功能,我也尝试了,但它没有提供相同的输出。

array_map代码:

array_map(function($element){return $element;}, $temp,'requested_user_id');

如何在array_map中将值作为键传递,或者是否有可以使用的替代函数?

3 个答案:

答案 0 :(得分:1)

你可以这样做,使用$temp数组创建一个新数组,同时创建 该数组使用requested_user_id作为密钥。

$data = array();

foreach($temp as $row){
    $data[$row['requested_user_id']] = $row;
}

echo "<pre>";
print_r($data);
echo "</pre>";

作为一项功能,

function registeredUserArray($temp){
    $data = array();
    foreach($temp as $row){
        $data[$row['requested_user_id']] = $row;
    }
    return $data;
}

$data = registeredUserArray($temp);

答案 1 :(得分:1)

像这样使用printf

array_map()

这里是演示:

$array = array_map(function($temp){
            return array(
                $temp['requested_user_id'] => $temp
            );
        }, $temp);

和输出:

<?php
$temp =  array(array
        (
            'type' => 1,
            'hash_id' =>null,
            'requested_user_amount' => 7250.88,
            'requested_user_id' => 9,
            'requested_user_rate' => 49800,
            'requested_user_qty' => 0.1456

        ),
        array
        (
            'type' => 1,
            'hash_id' => null,
            'requested_user_amount' => 64740,
            'requested_user_id' => 12,
            'requested_user_rate' => 49800,
            'requested_user_qty' => 1.3
        ),
         array
        (
            'type' => 1,
            'hash_id' =>null,
            'requested_user_amount' => 5000,
            'requested_user_id' => 17,
            'requested_user_rate' => 49800,
            'requested_user_qty' => 0.100401606
        )
        );
$array = array_map(function($temp){
            return array(
                $temp['requested_user_id'] => $temp
            );
        }, $temp);
echo json_encode($array);

答案 2 :(得分:0)

是的,最后我得到了解决方案,array_walk()是帮助我看下面代码的函数,它给了我相同的输出,我可以说替代或array_column()特别是我的情况:

代码:

array_walk($temp, function (&$value,$key) use (&$result) {
                            $result[ $value['requested_user_id'] ] = $value;
                        });