Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP – Add a new element into each multidimensional array

I have an array that looks like this:

Array
(
    [0] => stdClass Object
        (
            [quiz_id] => 1033
            [quiz_venue] => 6
            [quiz_host] => 46
            [quiz_golden_question] => 100
            [quiz_golden_question_outcome] => 0
            [quiz_running] => 1
            [quiz_status] => 100
            [quiz_trainee] => 0
        )

    [1] => stdClass Object
        (
            [quiz_id] => 985
            [quiz_venue] => 57
            [quiz_host] => 21
            [quiz_golden_question] => 0
            [quiz_golden_question_outcome] => 0
            [quiz_running] => 1
            [quiz_status] => 310
            [quiz_trainee] => 0
        )

I want to go through each array, and insert a new value (quiz_venue_name), this is what I have;

$quizzes = $wpdb->get_results( $prepared );

        foreach ($quizzes as $quiz => $item) { 
            $venuetitle = get_the_title($item->quiz_venue);
            $quizzes['quiz_venue_name'] = $venuetitle;
        }

        return $quizzes;

However, all it does is add the new values to the very end of the multidimensional array as new arrays – rather than adding them into each!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I feel like I’m doing something obvious wrong, so any help is much appreciated!

The end result I need would be;

Array
(
    [0] => stdClass Object
        (
            [quiz_id] => 1033
            [quiz_venue] => 6
            [quiz_host] => 46
            [quiz_golden_question] => 100
            [quiz_golden_question_outcome] => 0
            [quiz_running] => 1
            [quiz_status] => 100
            [quiz_trainee] => 0
[quiz_venue_name] => NEW VALUE
        )

    [1] => stdClass Object
        (
            [quiz_id] => 985
            [quiz_venue] => 57
            [quiz_host] => 21
            [quiz_golden_question] => 0
            [quiz_golden_question_outcome] => 0
            [quiz_running] => 1
            [quiz_status] => 310
            [quiz_trainee] => 0
[quiz_venue_name] => NEW VALUE
        )

>Solution :

Your $quizzes variable is an array of objects (Instance of stdClass), so you should use it attribute to set value in each iteration ($item->quiz_venue_name instead of $quizzes['quiz_venue_name']):

...
foreach ($quizzes as $quiz => $item) { 
    $venuetitle = get_the_title($item->quiz_venue);
    $item->quiz_venue_name = $venuetitle;
}
...

In fact the $quizzes['quiz_venue_name']=... code will be set a value for index quiz_venue_name in root of $quizzes array.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading