Reputation: 101
Is there a way in php to find out exactly where my element was inserted into an array i.e. at which postion it was inserted into the array. For like the following code I need to insert an element at any position of the array then insert a sub element at the second diimenson of that array
$myarray=array();
$myarray[]=$someelementdefinedbefore;
$myarray[][2]=$second element related to some element defined before;
i.e. i need the two elements mentioned above to be clubbed together so i can later access them both via a single array search
Any idea how I could make this work. I mean I know I could use array search for this but I wanted to know if there was more efficient solution available?
Upvotes: 0
Views: 207
Reputation: 6003
$myarray=array();
$myarray[]=$someelementdefinedbefore; // This will be the last element as it is appended.
$myArrayLastElement = count($myarray)-1; // get the key of the last element.
// append your second element to this array.
$myarray[$myArrayLastElement][2]=$second element
Upvotes: 1
Reputation: 32740
You don't want to add the second one as two dimensional array.
$myarray=array();
$myarray[]=$someelementdefinedbefore;
$myarray[]=$second element of same array;
here $myarray[0]
will give you first value of the array,
and $myarray[1]
will give you second value of the array
Upvotes: 0