【问题标题】:What's the issue in inserting a new array element in existing array in PHP?在 PHP 中的现有数组中插入新数组元素有什么问题?
【发布时间】:2013-09-03 15:10:03
【问题描述】:

我有一个多维数组。现在我想在这个数组中插入一个新的键值组合。但我做不到。你能帮我实现这个目标吗?供您参考,我在下面提供我的代码以及现有的多维数组:

代码: $practice_sheet_set_data /已有的多维数组/ /获取要插入到上述数组中的值的代码/

$sql  = " SELECT st.staff_full_name FROM ".TBL_STAFF." AS st JOIN ".TBL_PRACTICE_SHEET." AS ps ";
            $sql .= " ON st.staff_id=ps.practice_sheet_created_staff_id WHERE ps.practice_sheet_id= ".$practice_sheet_id;
            $this->mDb->Query( $sql);
            $practice_sheet_created_staff_data = $this->mDb->FetchArray(MYSQL_FETCH_SINGLE);

从上面的代码中,我得到一个变量$practice_sheet_created_staff_data 中的一维数组,但无法将它的值插入到上面的数组中。我也尝试了 array_push 和 array_merge。两个数组都在下面给出:

$practice_sheet_data

Array
(
    [0] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 505
            [topic_name] => Ratio and Proportion
        )

    [1] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 508
            [topic_name] => Inequalities
        )

    [2] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 509
            [topic_name] => Simple and Compound Interest including Annuity - Applications
        )

    [3] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 511
            [topic_name] => Sequence and Series - Arithmatic and Geomatric Progression
        )

    [4] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 517
            [topic_name] => Statistical Description of Data
        )

    [5] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 519
            [topic_name] => Correlation and Regression
        )

    [6] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 520
            [topic_name] => Probability & Mathematical Expectation
        )

    [7] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 521
            [topic_name] => Theoritical Distributions
        )

)

$practice_sheet_created_staff_data


Array
(
    [staff_full_name] => Amol Patil
)

我想在之前的数组中创建一个新的键值对为[staff_full_name] => Amol Patil

【问题讨论】:

  • 你试过$practice_sheet_data[] = $practice_sheet_created_staff_data;

标签: php arrays multidimensional-array


【解决方案1】:

如果你推送名字,你可能会在foreach上遇到麻烦。试试这个:

$new_array = array($practice_sheet_created_staff_data['staff_full_name'] => $practice_sheet_data);

您可以通过var_dump($new_array[0]);var_dump($new_array['name']); 获得它

类似这样的新输出

Array (

[Amol Patil] => Array

    [0] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 505
            [topic_name] => Ratio and Proportion
        )

    [1] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 508
            [topic_name] => Inequalities
        )

    [2] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 509
            [topic_name] => Simple and Compound Interest including Annuity - Applications
        )

    [3] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 511
            [topic_name] => Sequence and Series - Arithmatic and Geomatric Progression
        )

    [4] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 517
            [topic_name] => Statistical Description of Data
        )

    [5] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 519
            [topic_name] => Correlation and Regression
        )

    [6] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 520
            [topic_name] => Probability & Mathematical Expectation
        )

    [7] => Array
        (
            [topic_no_questions] => 5
            [subject_id] => 21
            [subject_name] => CPT Quantitative Aptitude
            [topic_id] => 521
            [topic_name] => Theoritical Distributions
        )

)

【讨论】:

    【解决方案2】:

    如果您想做的是插入一个新的密钥对值,您尝试过吗

    foreach($practice_sheet_created_staff_data as $key => $value)
     {
      $practice_sheet_created_staff_data[$key][staff_full_name] = 'Amol Patil'
     }
    

    【讨论】:

      【解决方案3】:

      如果你想在数组中插入$practice_sheet_created_staff_data 那么:

      $practice_sheet_data[] = $practice_sheet_created_staff_data;
      // will give you
      // $practice_sheet_data[8] = array('staff_full_name' => 'Amol Patil');
      

      然后插入每个子数组:

      foreach ($practice_sheet_data as $key => $array) {
        $practice_sheet_data[$key]['staff_full_name'] = $practice_sheet_created_staff_data['staff_full_name'];
      }
      // will give you 
      // $practice_sheet_data[0] = array(... your other elements..., 'staff_full_name' => 'Amol Patil');
      

      【讨论】:

        【解决方案4】:

        如果你想改变结果数组,试试这个。

        foreach($practice_sheet_created_staff_data as &$p) {
            $p['staff_full_name'] = 'Amol Patil';
            // $p['topic_id'] = 0; // if you wanna the other value.
        
        }
        

        您必须输入 & 。 它可以改变数组内的值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-03-08
          • 2021-11-26
          • 1970-01-01
          • 2020-09-30
          • 1970-01-01
          • 1970-01-01
          • 2013-06-01
          • 2016-12-12
          相关资源
          最近更新 更多