john
john

Reputation: 69

convert array to dictionary in php

I have array (returns from database), looks like this:

response = {
    0 = {
        id = "12312132",
        title = "title1",
        ....
        createDT = "2015-03-03 22:53:17"
        }
    1 = {
        id = "456456456",
        title = "title2",
        ....
        createDT = "2015-03-03 22:53:17"
        }
    2 = {
        id = "789789789",
        title = "title3",
        ....
        createDT = "2015-03-03 22:53:17"
        }
    }

I need to convert this in dictionary use php like this:

response = {
    "12312132" = {
        title = "title1",
        ....
        createDT = "2015-03-03 22:53:17"
        }
    "456456456" = {
        title = "title2",
        ....
        createDT = "2015-03-03 22:53:17"
        }
    "789789789" = {
        title = "title3",
        ....
        createDT = "2015-03-03 22:53:17"
        }
    }

i.e. key is id. Perhaps there is some function in php, for do it easy?

Upvotes: 5

Views: 17496

Answers (4)

srob
srob

Reputation: 31

You can achieve this using array_combine and array_column, like this :

$result = array_combine(array_column($response, 'id'), $response);

Basically, this :

  • extracts an array of the values of ids
  • use this list as key for a new array, using $response as values.

Upvotes: 3

user2560539
user2560539

Reputation:

Loop through the results to populate the array again in new format

    $count = count($response) // count the items of your initial array
    $i=0;
    while($i<$count) { //start a loop to populate new array
    $key = $response[i]['id'];
    $new_response[$key] = array('title' =>  $response[i]['title'], ... ,'createDT' => $response[i]['createDT']);
    $i++;
    } // end loop

print_r($new_response);

Upvotes: 0

Forien
Forien

Reputation: 2763

There is nothing of dictionary term in PHP. What you actually mean is an associative array, also commonly known as a hash. The same thing though, but this can make it easier to google up in future.

You can do it in several ways, I will give you classic foreach() one. I think array_map() approach would be possible too.

$response = ...;        // your database response
$converted = array();   // declaring some clean array, just to be sure

foreach ($response as $row) {
    $converted[$row['id']] = $row;        // entire row (for example $response[1]) is copied 
    unset($converted[$row['id']]['id']);  // deleting element with key 'id' as we don't need it anymore inside
}
print_r($converted);

Upvotes: 5

KIKO Software
KIKO Software

Reputation: 16688

No, but you can write a little program in PHP:

$result = array();
foreach ($response as $row)
{
  $id = $row['id'];
  unset($row['id']);
  $result[$id] = $row;
}

echo '<pre>';
print_r($result);
echo '</pre>';

And even make that into your own function:

function dictonary($response) 
{
  $result = array();
  foreach ($response as $row)
  { 
    $id = $row['id'];
    unset($row['id']);
    $result[$id] = $row;
  }
  return $result;
}

Upvotes: 3

Related Questions