jmenezes
jmenezes

Reputation: 1926

Adding an extra key / value to a PHP array

I have php array that like this:

$categories = array (
    "audio-video" => "Audio & Video",
    "books" => "Books",
    "vehicles" => "Vehicles",
    "watches" => "Watches"
)

How can I add another value to each element in it so it ends up something like this:

$categories = array ( // For example. This isn't the right thing.
    "US01" => "audio-video" => "Audio & Video",
    "US02" => "books" => "Books",
    "US03" => "vehicles" => "Vehicles",
    "US04" => "watches" => "Watches"
)

Is this possible? I'd like to able to access the other values of each element using the first key. So US01 will be Audio & Video or audio-video. Is this possible with php?

Upvotes: 0

Views: 72

Answers (6)

Joran Den Houting
Joran Den Houting

Reputation: 3163

You can not. All you can do is generate a new array inside like this:

$categories = array(
    array(
        "US01",
        "audio-video",
        "Audio & Video"
    ),
    array(
        "US02",
        "books",
        "Books"
    )
);

Or use USxx as a key:

$categories = array(
    "US01" => array(
        "audio-video",
        "Audio & Video"
    ),
    "US02" => array(
        "books",
        "Books"
    ),
);

Upvotes: 1

Olivier Van Bulck
Olivier Van Bulck

Reputation: 786

You can use a 2-dimensional array if you want.

$category = array("US01" => array("audio-video" => "Audio & Video"),
    "US02" => array("books" => "Books"),
    "US03" => array("vehicles" => "Vehicles"),
    "US04" => array("watches" => "Watches")
);

Upvotes: 0

DaFunkyAlex
DaFunkyAlex

Reputation: 1969

No, not this way, but could do it this way:

$categories = array (
    "US01" => array("audio-video" => "Audio & Video"),
    "US02" => array("books" => "Books"),
    "US03" => array("vehicles" => "Vehicles"),
    "US04" => array("watches" => "Watches")
)

Upvotes: 0

Cobra_Fast
Cobra_Fast

Reputation: 16061

How about

$categories = array(
    "US01" => array("audio-video", "Audio & Video"),
    "US02" => array("books", "Books"),
    "US03" => array("vehicles", "Vehicles"),
    "US04" => array("watches", "Watches")
)

access via

$categories['US01'][0]; // audio-video
$categories['US01'][1]; // Audio & Video

Upvotes: 1

webmonkey
webmonkey

Reputation: 1093

This should work:

$categories = array (
    "audio-video" => "Audio & Video",
    "books" => "Books",
    "vehicles" => "Vehicles",
    "watches" => "Watches"
);

$newCategories = Array();

$i = 1;
foreach ($categories as $key => $value) {

    $index = "US" . ($i < 10) ? "0" . $i : $i;
    $newCategorys[$index] = Array('slug'=>$categories[$key], 'value'=>$categories[$value]);

    $i++;

}

So you can reach everything with

$newCategories['US01']['slug'] // for "audio-video "

and

$newCategories['US01']['value'] // for "Audio & Video"

(untested)

Upvotes: 0

MrCode
MrCode

Reputation: 64526

You can make an array of arrays:

$categories = array (
    "US01" => array("name" => "Audio & Video", "alias" => "audio-video"),
    "US02" => array("name" => "Books", "alias" => "books"),
);

Then access it like:

echo $categories['US01']['alias']; // audio-video
echo $categories['US01']['name']; // Audio & Video

echo $categories['US02']['alias']; // books
echo $categories['US02']['name']; // Books

Upvotes: 1

Related Questions