0

I want to set array particular value to it's key, suppose I am having array like below :

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

And I want to set requested_user_id to array key.

I found one solution like below

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

Output :

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

And it'll give exact output which I want. but problem is function array_column requires php 5.5 and my server is 5.4.

From searching the web I found array_map() is the alternative function of array_column(), and I tried also but it does not give same output.

array_map code :

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

How can I pass value as key in array_map, or is there an alternative function I can use?

1
  • Just throwing it out there, but would it not be possible to upgrade to a newer version of php? Commented Oct 26, 2016 at 6:23

3 Answers 3

1

You can do this way, Created a new array using $temp array, While creating that array, use the requested_user_id as key.

$data = array();

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

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

As a function,

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

$data = registeredUserArray($temp);
Sign up to request clarification or add additional context in comments.

1 Comment

Thansk for the answer but I want it using function if possible
1

use array_map() like this:

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

here the the demo:

<?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);

and the output:

[{"9":{"type":1,"hash_id":null,"requested_user_amount":7250.88,"requested_user_id":9,"requested_user_rate":49800,"requested_user_qty":0.1456}},{"12":{"type":1,"hash_id":null,"requested_user_amount":64740,"requested_user_id":12,"requested_user_rate":49800,"requested_user_qty":1.3}},{"17":{"type":1,"hash_id":null,"requested_user_amount":5000,"requested_user_id":17,"requested_user_rate":49800,"requested_user_qty":0.100401606}}]

6 Comments

@Codebrekers you need change the $temp to your format
Sorry it's not working, the output will be totally different then yours
@Codebrekers I make the demo test.php and run php test.php, it works.
Kindly provide jsfiddle link
@Codebrekers just save the code as test.php, and run it with php test.php command. I cannot make the php runable in the jsfiddle.
|
0

Yes, Finally I got solution, array_walk() is the function which help me out see below code which give me same output and I can say alternative or array_column() in particular my case :

Code :

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.