Viewed   159 times

I have an array that I want on multiple pages, so I made it a SESSION array. I want to add a series of names and then on another page, I want to be able to use a foreach loop to echo out all the names in that array.

This is the session:

$_SESSION['names']

I want to add a series of names to that array using array_push like this:

array_push($_SESSION['names'],$name);

I am getting this error:

array_push() [function.array-push]: First argument should be an array

Can I use array_push to put multiple values into that array? Or perhaps there is a better, more efficient way of doing what I am trying to achieve?

 Answers

2

Yes, you can. But First argument should be an array.

So, you must do it this way

$_SESSION['names'] = array();
array_push($_SESSION['names'],$name);

Personally I never use array_push as I see no sense in this function. And I just use

$_SESSION['names'][] = $name;
Thursday, October 13, 2022
2

You are using an associative array so you just set the key/value pair like this.

$array["Password"] = pass;

I think you may need to review the difference between an array and an associative array. For example if I ran the same command again with a different value it would overwrite the old one:

$array["Password"] = "overwritten";

Giving you this

Array ( [Username] => user 
        [Email] => email
        [Password] => "overwritten"
      )

Which judging by your question is not what your expecting

Monday, October 24, 2022
4

You check if it's there, using in_array, before pushing.

foreach($something as $value){
    if(!in_array($value, $liste, true)){
        array_push($liste, $value);
    }
}

The ,true enables "strict checking". This compares elements using === instead of ==.

Saturday, December 10, 2022
3

You can use Array#collect to execute a block for each element of the outer array. To get the first element, pass a block that indexes the array.

arr.collect {|ind| ind[0]}

In use:

arr = [["value1", "value1_other"], ["value2", "value2_other"], ["value3", "value3_other"]]
=> [["value1", "value1_other"], ["value2", "value2_other"], ["value3", "value3_other"]]
arr.collect {|ind| ind[0]}
=> ["value1", "value2", "value3"]

Instead of {|ind| ind[0]}, you can use Array#first to get the first element of each inner array:

arr.collect(&:first)

For the &:first syntax, read "Ruby/Ruby on Rails ampersand colon shortcut".

Saturday, November 12, 2022
 
s_boot
 
1

ActiveRecord queries support the :select parameter, which lets you define the fields you want returned to you as a string.

Usually people use something like:

:select => 'field1, field2'

If you know the raw query language for your database server, then you can use that in the select string. One of the options when selecting fields using SQL is to use the as modifier:

select field1 as the_first_field, field2 as the_second_field

and the database will return the fields using the new field names instead of the old field names. It's an easy way to manage legacy fields that are named in ways that conflict with Rails if your database supports that.

See "Learn to Love ActiveRecord's :select Parameter" in "Five ActiveRecord Tips" and "Selecting Specific Fields" in the Ruby on Rails Guides.

Here's an example from one of my Rails apps using the rails console to access my Postgres DB:

ruby-1.9.2-p180 :007 > dn = DomainName.first
 => #<DomainName id: 1, domain_name: "ip72-208-155-230.ph.ph.cox.net", created_at: "2010-04-20 05:53:22", updated_at: "2010-04-20 05:53:22"> 
ruby-1.9.2-p180 :008 > dn = DomainName.first(:select => 'id, domain_name as dn')
 => #<DomainName id: 1> 
ruby-1.9.2-p180 :009 > dn['id']
 => 1 
ruby-1.9.2-p180 :010 > dn['dn']
 => "ip72-208-155-230.ph.ph.cox.net"
Wednesday, November 30, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :