Use PHP functions in JavaScript

JavaScript array

!No description available for array. @php.js developers: Please update the function summary text file.

1
2
3
4
56
7
8
9
10
function array () {
    // !No description available for array. @php.js developers: Please update the function summary text file.
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/array    // +   original by: d3x
    // *     example 1: array('Kevin', 'van', 'Zonneveld');
    // *     returns 1: ['Kevin', 'van', 'Zonneveld']
    return Array.prototype.slice.call(arguments);
}
external links: original PHP docs | raw js source

Examples

Running

1
array('Kevin', 'van', 'Zonneveld');

Should return

1
['Kevin', 'van', 'Zonneveld']

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have array goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
10 Dec '08 Permalink

q  @ Ates Goral: Wow thanks a lot Ates! We'll leave it open for improvement then! Though I don't really like the idea of global variables, I believe the include_once functions also already work like that so we may be able to do that here as well. What do you think?

Gravatar
Ates Goral
5 Dec '08 Permalink

q  Perhaps the each() implementation I have below is a hasty one. It won't play nice with reset(), next() and previous(). It can be improved by actually storing the cursor position instead of consuming the key array with the shift(). I'll try to improve it and provide reset/next/prev if I find the time.

Gravatar
Ates Goral
5 Dec '08 Permalink

q  I apologize for the earlier, unformatted post :)

[CODE="Javascript"]
function each(arr) {
// Return the current key and value pair from an array and advance the array cursor

// + original by: Ates Goral (http://magnetiq.com)
// * example 1: each([42,43]);
// * returns 1: {0: 0, 1: 42, key: 0, value: 42}
// * example 2: each({a:"apple",b:"balloon"});
// * returns 2: {0:"a",1:"apple",key:"a",value:"apple"}
if (!(arr instanceof Object) || (arr._keys && !arr._keys.length)) {
return false;
}

if (!arr._keys) {
arr._keys = [];

for (var k in arr) {
if (k != "_keys") {
arr._keys.push(k);
}
}
}

var k = arr._keys.shift();
var v = arr[k];

return {
0: k,
1: v,
key: k,
value: v
};
}
[/CODE]

Gravatar
Ates Goral
5 Dec '08 Permalink

q  function each(arr) {
// Return the current key and value pair from an array and advance the array cursor

// + original by: Ates Goral (http://magnetiq.com)
// * example 1: each([42,43]);
// * returns 1: {0: 0, 1: 42, key: 0, value: 42}
// * example 2: each({a:"apple",b:"balloon"});
// * returns 2: {0:"a",1:"apple",key:"a",value:"apple"}
if (!(arr instanceof Object) || (arr._keys && !arr._keys.length)) {
return false;
}

if (!arr._keys) {
arr._keys = [];

for (var k in arr) {
if (k != "_keys") {
arr._keys.push(k);
}
}
}

var k = arr._keys.shift();
var v = arr[k];

return {
0: k,
1: v,
key: k,
value: v
};
}

Gravatar
Kevin van Zonneveld
1 Dec '08 Permalink

q  @ Sean & Onno Marsman: I totally agree with Onno. If we can't get it right, and people have to learn a different notation anyway, let's just stick with JavaScript.

Gravatar
Onno Marsman
30 Nov '08 Permalink

q  @Sean: We already can create associative arrays with JS:
[CODE="Javascript"]
{
'first': 'kevin',
'last': 'Zonnevelt' //I don't know why this is with a T ;)
}
[/CODE]
Technically this is not an array but an object, but your proposal wouldn't return anything different. So we already have a syntax that is "not quite the same as php" and it does exactly the same.

Also think of using variables:
[CODE="Javascript"]
{ a: b, c: d}
[/CODE]
Your alternative would result in something like this, because proper escaping is needed:
[CODE="Javascript"]
array(" '"+addslashes(a)+"' => '"+addslashes(b)+"' ", " '"+addslashes(c)+"' => '"+addslashes(d)+"' " );
[/CODE]
I hope I didn't make any typo's, but I think my point is clear.

Gravatar
Sean
29 Nov '08 Permalink

q  @Kevin: Could you not do:

[CODE="Javascript"]
array(" 'first' => 'kevin' ", " 'last' => 'Zonnevelt' " );
[/CODE]

It's not quite the same as php, but at least this way you could create hash arrays

Gravatar
Kevin van Zonneveld
6 Oct '08 Permalink

q  @ thinsoldier: Yes that would be very nice, but I don't see how we could implement that unfortunately :( Here we run against the hard wall of language differences.

Gravatar
thinsoldier
5 Oct '08 Permalink

q  what about

array('first'=>'Kevin', 'last'=>'Zonnevelt')

Gravatar
covings
9 Sep '08 Permalink

q  VERY INTERESTING AND REALLY USEFUL INFORMATIONS! THX SO MUTCH

Gravatar
Kevin van Zonneveld
19 Jun '08 Permalink

q  @ thinsoldier: What about them? Judging by your comment I would think you mean that our asort doesn't support them.. But it appears we don't have an asort yet ;)
JavaScript does support them in general. But they're called objects to be strict.
But I guess I don't fully understand your question.

Gravatar
thinsoldier
19 Jun '08 Permalink

q  what about associative arrays?
var Divs = array(); // array of divs that I want to sort
Divs['amelie'] = divHtmlElement3;
Divs['randolph'] = divHtmlElement1;
Divs['judy'] = divHtmlElement9;

asort(Divs);

showDivArray(Divs);


Contribute a New function