1

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!

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
        )

1 Answer 1

3

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.

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

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.