1

I have a multi dimensional array, which is printed out. But it is printed in reverse order how I need. But when I try to use array_reverse() it outputs almost random values:

for($j=0; $j<count($positions);$j++){
    $temp[$j]=strlen($positions[$j]);
}
array_push($GLOBALS['lengths'],$temp);

and when I echo $lengths I get(as it should be, but in reverse order):

1 1
2 1

but when I use array_reverse($GLOBALS['lengths']) I get:

4 1
2 1

where might be the issue?

Thats the code that I use:

for($j=0; $j<count($positions);$j++){

    $temp[$j]=strlen($positions[$j]);
}
array_push($GLOBALS['lengths'],$temp);
$lengths=array_reverse($GLOBALS['lengths']);
5
  • Please show us the array declaration and what your goal is? Something like: this is the situation and this is my goal Commented Nov 22, 2014 at 21:15
  • Where is the code that outputs your array? What's the content of $positions? Is it an array of strings or an array of arrays? I noticed you are using strlen and not count. Commented Nov 22, 2014 at 21:15
  • try array_reverse($GLOBALS['lengths'], true); to preserve keys it will give a good hint. Commented Nov 22, 2014 at 21:16
  • @Rizier123 codepaste.net/tx5c9m Firstly, I want to find the position of a given text in the string, and then to save that positions' length in the array $lengths Commented Nov 22, 2014 at 21:25
  • @Victory ,doesnt work Commented Nov 22, 2014 at 21:26

1 Answer 1

1

Try this:

//this will reverse the result output
for($j=count($positions); $j>0; $j--){
    array_push($GLOBALS['lengths'],strlen($positions[$j-1]));
}
$lengths=$GLOBALS['lengths'];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, your answer, even though not working on my particular case, showed me, how to do it in another place :)

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.