Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

weird array syntax

Tags:

arrays

syntax

php

I'm trying to figure how a function I've been given works -- or rather doesn't work. The problematic areas include array notation like this:

$small[$end[$i]{0}]

I think the idea is to append "0" to the value of $end[$i] and use that for the index of $small. But it doesn't work in PHP5.3. Is this a deprecated syntax and is it trying to do what I think it is?

like image 767
dnagirl Avatar asked Apr 23 '10 13:04

dnagirl


People also ask

What is * array [] in C?

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To create an array, define the data type (like int ) and specify the name of the array followed by square brackets [].

What is an array syntax and example?

Syntax to initialize an array:-int numbers[] = {2, 3, 4, 5, 6}; int numbers[] = {2, 3, 4, 5, 6}; int numbers[] = {2, 3, 4, 5, 6}; In case, if you don't specify the size of the array during initialization then the compiler will know it automatically. numbers[0] = 2.

What Is syntax array?

Array declaration syntax is very simple. The syntax is the same as for a normal variable declaration except the variable name should be followed by subscripts to specify the size of each dimension of the array. The general form for an array declaration would be: VariableType varName[dim1, dim2, ...

How do you declare an array in C++?

A typical declaration for an array in C++ is: type name [elements]; where type is a valid type (such as int, float ...), name is a valid identifier and the elements field (which is always enclosed in square brackets [] ), specifies the size of the array.


1 Answers

It's getting the first character from the $end[$i] string and then accessing the $small array using that character as the array key.

Edit:

Since $foo=200; $foo[0] != 2;//=='', what do you recommend for getting the leftmost digit when the magnitude of the number is unknown?

The easiest way is substr($foo, 0, 1) in PHP.

If you're using a strongly typed language, I have some metrics you may be interested in reading from another answer of mine: How can you get the first digit in an int (C#)?

like image 88
John Rasch Avatar answered Sep 20 '22 02:09

John Rasch