I am looping through an array which itself contains array to find indexes of values 5 & 6.

Upon finding these indexes, I push the matched array, using array_push, into a another array. My application depends on maintaining the array indexes but array_push resets the keys to 0, 1, 2 etc rather than the matched 5, 6, 7 etc.

It feels like a simple problem but I could use some help ....

share|improve this question
3  
Can you please show a code snippet? – Dor Shemer Jan 18 '12 at 22:23
    
Instead of using array_push($array, $new), can you do $array[] = $new or even $array[$key] = $new (depending on the $key you want)? – cmbuckley Jan 18 '12 at 22:25
up vote 0 down vote accepted

Rather than calling array_push you can add element this way:

$arr[5] = array("foo", "bar");
$arr[6] = array("red", "blue");
$arr[7] = array("123", "567");
share|improve this answer
    
simple but effective, thank you! – sisko Jan 19 '12 at 20:20

Would that do or did I miss something?

$newArray = array();

foreach( $myArrays as $myArray ) 
  if( ($result = array_search(5, $myArray)) || ($result = array_search(6, $myArray))
    $newArray[$result] = $myArray[$result];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.