Solved

What does the "0" mean in this PHP excerpt?

Posted on 2013-01-19
3
277 Views
Last Modified: 2013-03-09
I am learning from a book called "Head First PHP and MYSQL"

One of the lessons has this line written in it:

$category_totals = array(array($mismatch_categories[0], 0));

Open in new window


if you have array($variable[0],0)  what does the second zero do?

Thanks
0
Comment
Question by:NewWebDesigner
  • 2
3 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 38796948
$category_totals is assigned to be an array of one element

Inside that array is another array of two elements
    The first element is the zero-th position of the array $mismatch_categories
    The second element is the integer zero

It may be easier to understand if you write it out line-by-line with indenting like this:

$category_totals = array
( array
  ( $mismatch_categories[0]
  , 0
  )
)
;

Open in new window


HTH, ~Ray
0
 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 250 total points
ID: 38797771
also it is much better for me to understand if I do it as it should be done as -
 $insideArray = array($mismatch_categories[0],  0);     // two array elements, , 0 as mismatch and 1 as  0
 echo "Second element is ".$insideArray[1];  // will output "Second element is 0"

//now add to $category_totals
$category_totals = array( $insideArray );

if you want to create an array, you add "Elements" as things you want to get out of array later, arrays are a kind of compartment storage area, each thing between the commas goes into the array as an element |compartment, , data segment| so you can use numbers to get them out later.

you use line of -
$category_totals = array(array($mismatch_categories[0], 0));

is code that works well , but is considered short cut code because it does not create the first array before it creates the second, does all in one line.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38798282
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now