How to insert a new key value pair in array in php?

后端 未结 4 1085
小蘑菇
小蘑菇 2020-12-30 20:17

I\'ve an array as follows named $test_package_data. For the reference I\'m printing first two elements of it:

Array
(
    [0] => Array
               


        
相关标签:
4条回答
  • 2020-12-30 20:43

    To add:

    $arr["key"] = "value";
    

    Then simply return $arr

    Can't return directly like this way return $arr["key"] = "value";

    0 讨论(0)
  • 2020-12-30 21:01
    foreach($test_package_data as $key=>$data ) {
    
       $category_detail_arr = $test_package_data[$key]['category_detail'];
    
       foreach( $category_detail_arr as $i=>$value ) {
         $test_package_data[$key]['category_detail'][$i]['count'] = $some_value;////<----Here
       }
    
    }
    
    0 讨论(0)
  • 2020-12-30 21:02

    If you are creating new array then try this :

    $arr = ['key' => 'value'];

    And if array is already created then try this :

    $arr['key'] = 'value';

    0 讨论(0)
  • 2020-12-30 21:03

    Try this:

    foreach($array as $k => $obj) { 
        $obj->{'newKey'} = "value"; 
    }
    
    0 讨论(0)
提交回复
热议问题