如何在PHP中的数组中创建新的键值对? [关闭]

I'm relatively new to PHP arrays. So getting few difficulties in achieving the desired array. I'm declaring an array as follows :

if (is_array($cs_data)) { 
              -----------------------------
          } else { 
              $cs_data = array();
              -----------------------------
          }

First I'm checking whether the array is already declared or it has to be declared. Depending on it I'll execute some code. Now when I declare an array a single on dimension array will get create. Upon creation of array when I insert a new values in it using array_push(), the key of values starts from 0 and so on. But my requirement is there should be keys with the names given by me(i.e. key names should be defined by me as shown in below array) and they should have values I set to them. For your understanding I'm giving the expected array which is supposed to look like the one given below :

array(
  [cs_map_id] => 3
  [subject_name] => Maths
  [subject_checked] => 1
  [teacher_cs_id] => 34
)

Can anyone help me to achieve such array output? Thanks in advance.

$cs_data = array('cs_map_id' => 3, 'subject_name' => 'Maths');

etc.

Instead of array_push try this

     if (is_array($cs_data)) { 
         $cs_data['yournewkey'] = 'yournewvalue';
      } else { 
          $cs_data = array();
           $cs_data['cs_map_id'] = 3;
           $cs_data['subject_name'] = 'Maths';
           $cs_data['subject_checked'] = 1;
           $cs_data['teacher_cs_id'] = 34;
      }

You can Try Following::

$cs_data=null;
     if (is_array($cs_data))
     { 
         $cs_data['cs_map_id']=3;//what ever you need
         $cs_data['subject_name']='Maths';//what ever you need
         $cs_data['subject_checked']=1;//what ever you need
         $cs_data['teacher_cs_id']=34;//what ever you need
    }
    else
    { 
        $cs_data = array();
        $cs_data['cs_map_id']=3;//what ever you need
        $cs_data['subject_name']='Maths';//what ever you need
        $cs_data['subject_checked']=1;//what ever you need
        $cs_data['teacher_cs_id']=34;  //what ever you need 
    }
    foreach($cs_data as $key=>$val)
    {
        var_dump($key."--->".$val);//what ever you need
    }