Solved

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

Posted on 2013-01-19
3
276 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

757 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

22 Experts available now in Live!

Get 1:1 Help Now