0

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?

3
  • array_search ? Commented Feb 4, 2013 at 5:16
  • $myarray=array(); $myarray[]=$someelementdefinedbefore; $myarray[][2]=$second element of same array; why you making it a two dimesional array?? Commented Feb 4, 2013 at 5:17
  • @rohitarora sorry i hadnt expained the question clearly enough, kindly read the edited question Commented Feb 4, 2013 at 5:22

2 Answers 2

1
$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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

sorry i hadnt expained the question clearly enough, kindly read the edited question

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.