Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 18th, 2003, 03:35 PM
Registered User
 
Join Date: Aug 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Question about Arrays

Hi everyone, I am a newbie to PHP. I have a question I hope somebody here will answer. I am a bit confused about array declarations, I would like to know if the following two code lines are equivalent:

$my_array=array('a','b');

$my_array[0]='a';
$my_array[1]='b';

If they are not the same thing, could anybody write an example of how to set an array and initialize it with string values, without using the array() function ? Thank you all in advance and sorry if this question is a bit trivial, but as I said before I am just a newbie with PHP.

 
Old August 18th, 2003, 03:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The two methods of initializing arrays are identical. Using the array() function lets you initialize all your values in one statement. The end result is the same.

You can also initialize string indexes using either method:

$my_array = array('index_1' => 'a', 'another index' => 'b');

$my_array['some index'] = "yet another value";




Take care,

Nik
http://www.bigaction.org/
 
Old August 18th, 2003, 04:53 PM
Registered User
 
Join Date: Aug 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your reply Nikolai. I would like to ask you one more question on the same topic.

Given:
$my_array=array();

How can I do the same thing not using array() function ?


 
Old August 18th, 2003, 06:08 PM
Registered User
 
Join Date: Aug 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

$my_array[0] = ' ';
unset($my_array[0]);

I guess this will do it.
 
Old August 18th, 2003, 06:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It will, but it's a waste of effort... why the aversion to using array()?


Take care,

Nik
http://www.bigaction.org/
 
Old August 18th, 2003, 06:47 PM
Registered User
 
Join Date: Aug 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nothing in particular, I was just wondering if there was another way to declare an empty array, but I will keep using array()






Similar Threads
Thread Thread Starter Forum Replies Last Post
Help With Arrays Crippy Ruby 2 March 6th, 2013 05:59 PM
Question on Collections/Arrays/ArrayList csharplearner C# 2 October 13th, 2005 06:05 PM
Multidemmesional Arrays OR arrays gmoney060 Classic ASP Basics 3 November 1st, 2004 03:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.