9

I need to create an association between an Array and a Number; as PHP lacks a Map type, I am trying using an array to achieve this:

$rowNumberbyRow = array();
$rowNumberByRow[$rowData] = $rowNumber;

However, when I evaluate the code, I get the following Error:

Warning: Illegal offset type

Just to note, the data stored in the array ($rowData) does not have any 'unique' values that I can use as a key for the $rowNumberByRow Array.

Thanks!

UPDATE: To answer some of my commenters, I am trying to create a lookup table so that my application can find the row number for a given row in O(1) time.

4
  • $rowNumberByRow [ $rowData ['your_key'] ] = $rowNumber; Commented Aug 10, 2011 at 23:07
  • @Jacek_FH I'm trying to create a lookup table so I can retrieve the rowNumber for a given row. Commented Aug 10, 2011 at 23:10
  • 1
    @bensiu - as I said in the question, none of the fields in $rowData are unique enough to use as keys ;) Commented Aug 10, 2011 at 23:10
  • Made something here: stackoverflow.com/questions/4047903/complex-type-as-array-index/… Commented Aug 10, 2011 at 23:51

3 Answers 3

9

PHP does have a map Class: It's called SplObjectStorage. It can be accessed with exactly the same syntax as a general array is (see Example #2 on the reference).

But to use the class you will have to use the ArrayObject class instead of arrays. It is handled exactly the same way arrays are and you can construct instances from arrays (e.g. $arrayObject = new ArrayObject($array)).

If you don't want to use those classes, you can also just create a function that creates unique hash-strings for your indexes. For example:

function myHash($array){
   return implode('|',$array);
}
$rowNumberByRow[myHash($array)] = $rowNumber;

You will of course have to make sure that your hashes are indeed unique, and I would strongly suggest you use the SplObjectStorage and maybe read a little bit more about the SPL classes of php.

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks, I did not know about these classes!
I forgot to mention: SplObjectStorage will index by Instance, not by Object content, so you would have to hold on to your ArrayObjects. If you want a content-based map, you will have to write a hash function, as outlined in the second part of my answer.
There is absolutely no requirement to use ArrayObject at all, a boring old stdClass would do just as well for the map.
If you can hold onto the instances you might as well just store the row number in the original array, and save the object overhead and hash lookup.
@salathe PHP throws the following warning when I try to assign an array as a key -- Warning: SplObjectStorage::offsetSet() expects parameter 1 to be object, array
|
2

Why not just store the row number in the array? e.g:

$rowData['rowNumber'] = $rowNumber;

You could instead serialize the array, e.g:

$rowNumberByRow[serialize($rowData)] = $rowNumber;

However that's pretty inefficient.

1 Comment

Yeah, that crossed my mind, but I came to the same conclusion - it's a pretty poor way of trying to implement a lookup table :(
1

In php you can use only scalar values as array keys.

If your $rowNumber is unique - then you'd try to use the opposite relation direction. If it is not unique - then you don't have any possible solution I know.

Comments

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.