0

I have a array like this-

$array = array('o' => 'one', 't' => 'three', 'f' => 'four');

I want to add a new element in the 't' key of the array. The result will be like the following:

$array = array('o' => 'one', 't' => 'three','six', 'f' => 'four');

How can i do this?

3
  • Then there will be subarray in t key Commented Jul 8, 2017 at 16:28
  • 2
    And how would access it? you can use array inside array instead. Commented Jul 8, 2017 at 16:29
  • $array['t'] .= ',six'; ? Commented Jul 8, 2017 at 16:29

2 Answers 2

1

You can't do it like that, For your target You have to use 2 dimension array, from other words array of array,

$array['t']=array('three','six');

and you can see the results like this,

Array ( [o] => one [t] => Array ( [0] => three [1] => six ) [f] => four );

You can easily access anything inside array using keys, if you want to access key "t",

$array['t'][0] = three and $array['t'][1] = six

This is the right way to solve your question.

Sign up to request clarification or add additional context in comments.

Comments

0

There is how i do it.

$array_for_t_key = ['three'];

$array = array('o' => 'one', 't' => $array_for_t_key, 'f' => 'four');

array_push($array_for_t_key,"six");

//Redeclare the array

$array = array('o' => 'one', 't' => $array_for_t_key, 'f' => 'four');

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.