1
$peter=array('cs204'=>array(10,10,10));
$peter=array('cs366'=>array(7,8,9));

echo '<pre>';
print_r($peter);
echo '</pre>';

I am trying to insert values into an array $peter with course number(cs204,cs366) as keys. Instead of adding to second element to the array it is rewriting the data. This is the current output(Actual):

Array
(
    [cs366] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

)

But I need this one as output(Expected):

Array
(
    [cs204] => Array
        (
            [0] => 10
            [1] => 10
            [2] => 10
        )

    [cs366] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

)

Please help me. I want to insert second element as: $peter=array('cs366'=>array(7,8,9)); but not like this: $peter['cs366']=array(7,8,9);

5
  • 1
    How are you getting these arrays? Also, why this $peter['cs366']=array(7,8,9); is not suitable for your purpose? Commented Jun 16, 2016 at 20:51
  • How am I getting these arrays? I didn't quite get you. Commented Jun 16, 2016 at 20:57
  • I mean, from where you getting these arrays i.e. array('cs204'=>array(10,10,10)) and array('cs366'=>array(7,8,9))? Without any proper explanation, it will be all guesswork. Commented Jun 16, 2016 at 21:00
  • I have initialized an array named $peter with index(key) cs204 and value array(10,10,10). And now I am trying to push another element to the array with key cs366 and value array(7,8,9) Commented Jun 16, 2016 at 21:05
  • So $peter['cs366']=array(7,8,9); suits well with your code, isn't it? Why you don't want to use it? Commented Jun 16, 2016 at 21:09

4 Answers 4

1

Try this:

$peter=array('cs204'=>array(10,10,10));
$peter=array_merge($peter, array('cs366'=>array(7,8,9)));
Sign up to request clarification or add additional context in comments.

4 Comments

While this would do it, it's over-thought / engineered when you can just have $peter['cs366'] = ....
Depends on context, if you absolutely know the key will be cs366 sure, but if not, you would be in trouble. Besides, I don't think merging two arrays with array_merge is over-thought at all, quite the contrary :-)
@oli Thank you. you gave me a satisfactory answer. If you don't mind, could you please tell me why its not working with $peter=array_push($peter, array('cs366'=>array(7,8,9))); ?
just remove $peter=, as array_push will add to $peter
0

Try

 $peter['cs204']=array(10,10,10);
 $peter['cs366']=array(7,8,9);

 echo '<pre>';
 print_r($peter);
 echo '</pre>';

Comments

0

Why are you not just declaring both sub-arrays at the same time?

$peter = array('cs204' => array(10, 10, 10), 'cs366' => array(7, 8, 9));

Or if you prefer multi-line:

$peter = array(
    'cs204' => array(10, 10, 10),
    'cs366' => array(7, 8, 9)
);

If you must achieve the result in two statements:

$peter = array('cs204' => array(10, 10, 10));
$peter['cs366'] => array(7, 8, 9);

Comments

0

Think this is what your going for:

$peter = array();
$peter['cs204']=array(10,10,10);
$peter['cs366']=array(7,8,9);

3 Comments

Nope, consider what that would give an compare to what op is after.
That would give an array index staring from 0, which is a three dimensional array.
I edited it. if this is not what your looking for ill just delete my awnser.

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.