4

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?

john
  • 59
  • 1
  • 1
  • 3

4 Answers4

5

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);
Forien
  • 2,712
  • 2
  • 13
  • 30
  • The problem with `array_map` is that it can't work on keys. A foreach loop is often faster anyway. – Niols Mar 04 '15 at 14:33
  • @Niols Thanks, I wasn't sure. I'm not big fan of `array_map` and `array_walk` and don't know everything about them. – Forien Mar 04 '15 at 14:35
  • It might be possible with `array_walk` (http://stackoverflow.com/questions/13036160/phps-array-map-including-keys?answertab=votes#tab-top). But yeah, that wont be an efficient code. – Niols Mar 04 '15 at 14:36
3

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;
}
KIKO Software
  • 15,283
  • 3
  • 18
  • 33
1

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.
srob
  • 11
  • 2
0

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