Add a key/value to an array

I have

Array
(
    [0] => Array
        (
            [name] => 
            [id] => 1
            [manufacturer] => Dell
            [model] => 114T
            [rack_name] => hojik
            [bay_name] => O
            [row_name] => 4
            [room_id] => 3
        )

    [1] => Array
        (
            [name] => 
            [id] => 2
            [manufacturer] => Baydel
            [model] => DAR3
            [rack_name] => hojik
            [bay_name] => O
            [row_name] => 4
            [room_id] => 3
        )
)

How can I change the array so it becomes

Array
(
    [0] => Array
        (
            [name] => 
            [id] => 1
            [manufacturer] => Dell
            [model] => 114T
            [rack_name] => hojik
            [bay_name] => O
            [row_name] => 4
            [room_id] => 3
            [AssetType] => Peripheral Storage
        )

    [1] => Array
        (
            [name] => 
            [id] => 2
            [manufacturer] => Baydel
            [model] => DAR3
            [rack_name] => hojik
            [bay_name] => O
            [row_name] => 4
            [room_id] => 3
            [AssetType] => Peripheral Storage
        )

)

Is this array from a database query result?

Where does your AssetType field come from? Is it always “Peripheral Storage”? If it can be derived from any other field in your query, then add it in to the query. If it can’t, then how do you know what to put in it?

yes, the array is from

  $sql = 'SELECT periphial_storages.name AS name,periphial_storage_id AS id,manufacturer,model,racks.name as rack_name,bays.name as bay_name,columns.name as row_name,room_id 
  FROM periphial_storages 
  INNER JOIN periphial_storage_types ON periphial_storages.periphial_storage_type_id = periphial_storage_types.periphial_storage_type_id
  INNER JOIN racks ON periphial_storages.rack_id = racks.rack_id
  INNER JOIN bays ON racks.bay_id = bays.bay_id
  INNER JOIN columns ON bays.column_id = columns.column_id
  WHERE periphial_storages.active = 0';

$result = $pdo->query($sql);

$storageArray = $result->fetchAll();

I tried to add the key/value from the query, but didnt know if it was possible…
Is it?

I wanted to add the AssetType key into the array and it will always be that value “Peripheral Storage”

@lurtnowski,

When you are getting your data from a DB query you need to say so. Starting with “I have an array” is second level and will waste peoples time trying to help you as the problem is most likely at the first level, the query. I just happened to know you were calling a DB from trying to help you long ago and going through this same thing.

Here is a good read that will help you to help us help you (mouthfull)
http://catb.org/~esr/faqs/smart-questions.html

4 Likes

If it’s always the same value, why put it in the array? Just echo it directly when you output the array.

oh, i see.

Speaking of which …

@lurtnowski please use var_export instead of var_dump to get arrays from PHP and post them here on the forums. At least people can then directly copy/paste them to an editor to play with them without having to reformat anything.

Help us help you.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.