0

I'm having an multi dimensional array. Now I want to insert a new key value combination in this array. But I'm not able to do it. Can you help me in achieving this? For your reference i'm giving my code below alongwith the existing multi dimensional array:

Code: $practice_sheet_set_data /Existing multi dimensional array/ /The code for getting the value which is to be inserted in the above array/

$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);

From tha above code I'm getting an single dimensional array in a variable $practice_sheet_created_staff_data but not able to insert it's value into a above array. I tried array_push and array_merge too. Both the arrays are given below:

$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
)

I want to create a new key value pair in the previous array as [staff_full_name] => Amol Patil

1
  • have you tried $practice_sheet_data[] = $practice_sheet_created_staff_data; Commented Sep 3, 2013 at 6:56

4 Answers 4

0

If you push name, you may have trouble on foreach. Try this one:

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

And you can get it with var_dump($new_array[0]); or var_dump($new_array['name']);

New Output something like this

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
        )

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

Comments

0

If what you want to do is insert a new key-pair value have you tried

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

Comments

0

if you want to insert $practice_sheet_created_staff_data into the array then:

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

to insert in each child array then:

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');

Comments

0

if you wanna change result array, try this.

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

}

you must type & . it can change values inside array.

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.