1

The Key() function in php:

The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, key() returns NULL.

Is their any thing simmilar to that in AS3.0?

Also on a side note: does .= in php mean the same as +=?

Here is an example of what I am trying to achieve. Based on the above info for the PHP Key() function converted to actionscript 3.

Essentially in PHP this is what Key() is used for:

<?php
$array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple');

// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
    if ($fruit_name == 'apple') {
        echo key($array).'<br />';
    }
    next($array);
}
?>

The above example will output:

fruit1
fruit4
fruit5

You can see more at: https://www.php.net/manual/en/function.key.php

3
  • .= I believe is for string concatenation and is there to differentiate from mathematical operations (+=). Commented Mar 14, 2012 at 22:24
  • Also on a side not: does .= in php mean the same as +=? No. It does not. Commented Mar 14, 2012 at 22:24
  • Edit note: Removed PHP tag because you're not really needing a PHP dev here, you're explaining a PHP function and asking an actionscript developer for something similar. Commented Mar 14, 2012 at 23:25

2 Answers 2

1

Your example uses a php array as an hash map or a dictionary. The documentation says:

An array in PHP [...] can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.

There is a dedicated class for that in as3 and it is called Dictionary. You can also use an Array, or any other dynamic object to achieve the same functionality. But let's use Dictionary here. The documentation says:

ActionScript 3.0 introduces an advanced type of associative array called a Dictionary ...

Also, the functions current() and next() are somehow implementations of the iterator pattern, which has no built in equivalent in as3. But the closest thing to the code you provided is, I think, the use of a simple for..in loop:

for (var key:String in dictionary) {
    if (dictionary[key] == "apple")
        trace(key);
}
Sign up to request clarification or add additional context in comments.

Comments

1

From what I understand you're trying to do this:

A function (don't think what you want to do inbuilt, though it may be):

function getKey(array:Array, find:*):String
{
    for(var i:String in array)
    {
        if(array[i] == find) return i;
    }

    return "";
}

And some test code:

var test:Array = [];

test["example"] = "hello";
test["something"] = "there";


trace(getKey(array, "hello")); // "example"

2 Comments

please see above OP edits to question. What your doing gets me the spot in the array where that value is stored.
No.. That's what indexOf() does.. My code returns example if you look for hello (like how apple gives you fruit1 in your example) and something for there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.