1

I have an array:

$path_r = array("/oldsites/web-sites","/oldsites/web_elements","/oldsites/web_old_stuff");

I do a bunch of stuff to it and everything is fine until this line of code:

$path_r[$i][$j] = $name;

Just prior to that line, the array ($path_r) looks like this (same as above):

Array
(
    [0] => /oldsites/web-sites
    [1] => /oldsites/web_elements
    [2] => /oldsites/web_old_stuff
)

But, just after, it looks like this:

Array
(
    [0] => /olds0tes/web-sites
    [1] => /oldsites/web_elements
    [2] => /oldsites/web_old_stuff
)

That is to say, the letter "i" in the first value is replaced by the value (zero) of the variable $i. But only once. Can't figure out why. Am I doing something wrong or is it just run-of-the-mill demonic activity?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
CWill
  • 390
  • 3
  • 11
  • What are the values of the other variables? – shawnt00 Oct 01 '16 at 16:29
  • 2
    what is there in `$name` and `$j`? explanation is when you write `$path_r[$i][$j] ` it's become `"/oldsites/web-sites"[$j]` (if `$i = 0`), and this exactly means `substr($string, int, $j)` so basically `$path_r[$i][$j] = $name;` is mean that assign `$name` at the place of `$path_r[$i][$j]` which is actually `substr($string, int, $j)` – Alive to die - Anant Oct 01 '16 at 16:29
  • shawnt00 $i and $j are just incremented $i++ , $j++ – CWill Oct 01 '16 at 16:36
  • Anant Sorry, I'm not sure I understand.$name is just the name of the next dirname in the path, for example, "/logos". So $path_r[$i][$j] would be Array ( [0] => /oldsites/web-sites [1] => Array ( [0] => logos)) edit: ok, I see what you are saying. You are right. Thank you. – CWill Oct 01 '16 at 16:46
  • @CWill i mean to say something like this:- https://eval.in/653580 happens in your code. it's quite possible because you said `I do a bunch of stuff to it and everything is fine until this line of code:`. Not exactly like my code link, but something similar like that one. – Alive to die - Anant Oct 01 '16 at 16:56

1 Answers1

2

As you said "I do a bunch of stuff to it and everything is fine until this line of code:", so we don't know what's going on there in your code.

One logical explanation why this happens is given below:-

When you write:-

$path_r[$i][$j]

it's become something like :- "/oldsites/web-sites"[$j] (if $i = 0; // for an example),

and this expression exactly means substr($string, int, $j)

(based on this link explanation:- https://stackoverflow.com/a/17193651/4248328)

So basically $path_r[$i][$j] = $name;

is mean that assign $name at the place of $path_r[$i][$j] which is actually substr($string, int, $j)

To understand it easily, check this link:- https://eval.in/653580

Conclusion:-

Something like above happens in your code. Not exactly as above, but something similar like that one.

Community
  • 1
  • 1
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98