Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7597879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:16:01+00:00 2026-05-30T22:16:01+00:00

I thought this would be simple but I cant seem to get it to

  • 0

I thought this would be simple but I cant seem to get it to work. All I want to do is to add a value into a userdata array. If a value is already in the viewed_news_items array I do not want to replace it.

             $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true,
                'viewed_news_items' => array()
            );

            $this->session->set_userdata($data);

insert value into viewed_news_items array

        $a = array($desk_id => $desk_id);
        $this->session->set_userdata('viewed_news_items', $a);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:16:02+00:00Added an answer on May 30, 2026 at 10:16 pm

    You’re using $desk_id as both the key and value, meaning unless you already have the value of $desk_id, you won’t be able to look it up in the array.

    Instead of this:

    $a = array($desk_id => $desk_id);
    

    You probably wanted to do this:

    $a = array('desk_id' => $desk_id);
    

    That is, use the string 'desk_id' as the key whose corresponding value is $desk_id.


    Is there a way to have the ‘desk_id’ just as an auto number so each time the code is executed another value is added instead of replacing ‘desk_id’?

    You can push items onto the end of an array via array_push($array, $value) or $array[] = $value. PHP will automatically assign the next numeric ID as the index for the new array element.

    In your current scenario, you’ll have to pull the existing list of IDs out of the session, append an ID to it, and then put it back into the session:

    # pull the existing IDs out of the session
    $viewed_news_items =  $this->session->userdata('viewed_news_items');
    
    # on first load, the array may not be initialized
    if (!is_array($viewed_news_items))
      $viewed_news_items = array();
    
    # append $desk_id to the list of viewed items
    $viewed_news_items[] = $desk_id;
    
    # put the new list back into the session
    $this->session->set_userdata('viewed_news_items', $viewed_news_items);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought this would be pretty easy but I'm running into all sorts of
I thought this would be simple, but.... How can I get the backgroundcolor of
I thought this would be simple but I can't seem to find a variable
OK so I thought this would be pretty simple but it's tripping me up..
I thought this would be simple, but its not working .. in both the
I thought this would be fairly easy, but I'm totally baffled. I want one
This is probably a really simple question but I can't seem to get my
I thought this would be a relatively simple task with something like FMOD, but
I thought it would be as simple as this... $('#yourSelect').change(function () { if ($(this).val()
This is what I thought would be a simple select clause, however the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.