0

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?

5
  • "US01" => "audio-video" => "Audio & Video" --- what does this mean? Commented May 20, 2014 at 12:12
  • it will be invalid array Commented May 20, 2014 at 12:13
  • It's the extra key/value I'd like to add. Commented May 20, 2014 at 12:13
  • @jmenezes: what does "the extra key/value" mean? Commented May 20, 2014 at 12:14
  • array key is always unique, this is looks wrong format you should create another array with same values with another key or use multi level array Commented May 20, 2014 at 12:16

6 Answers 6

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Can I run array_key_exists('US01',$categories) on an array like this? Because I need to check if the key exists before I do any more processing. It does work, but I'm asking just to be sure.
@jmenezes Yes, you can.
1

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

Comments

1

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

2 Comments

In you second array, how do I get the values of the array inside? For example, if provide US01 how can I get audio-video or Audio & Video whichever of the two I want? Also, I check for the existence of an array key using array_key_exists('US01',$categories). Is it safe to do so on the second kind of array?
Is echo $categories['US01'][0]; echo $categories['US01'][1]; the way?
0

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)

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.