convert array to dictionary in php

12,262

Solution 1

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

Solution 2

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;
}
Share:
12,262
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin about 2 years

    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?

  • n-dru
    n-dru over 9 years
    And before that loop $result = array();
  • KIKO Software
    KIKO Software over 9 years
    @n-dru: Yes it is good practice to initialize. I will add it. Thank you.
  • Niols
    Niols over 9 years
    The problem with array_map is that it can't work on keys. A foreach loop is often faster anyway.
  • Forien
    Forien over 9 years
    @Niols Thanks, I wasn't sure. I'm not big fan of array_map and array_walk and don't know everything about them.
  • Niols
    Niols over 9 years
    It might be possible with array_walk (stackoverflow.com/questions/13036160/…). But yeah, that wont be an efficient code.