1

I'm using a breadcrumb script for it's obvious use, and I am trying to remove &anyqueryafterhere from the result. I'm using for each to go through the array to remove this, but it returns 'Array'. The code I have is

if(empty($breadcrumbs)===false){
    foreach($breadcrumbs as &$r){
        $r = explode("&", $r);  
    }
}

Currently, it, as mentioned before, just returns Array, so it shows as something like Array » Array. So first question, what's the cause of this, and second, how can I sort it? Thanks in advance.

  • You realize that is only going to return the last $breadcrumbs right? You're overwriting $r with each iteration of the foreach loop. (that is after you remove that & from the foreach of course) – Rick Calder Dec 09 '12 at 14:55
  • How exactly is he modifying anything in the above code? Other than overwriting $r of course. – Rick Calder Dec 09 '12 at 14:58
  • 1
    He is overwriting `$r` by reference. In PHP, his line: `$r = explode("&", $r)` is the same as `$breadcrumbs[$key] = explode("&", $r)`. He is modifying the source array because it is being passed *by reference* – Colin M Dec 09 '12 at 15:02

2 Answers2

2

try

foreach($breadcrumbs as &$r){
                        ^-----is reference operator ..remove this Those are references, they are similar to "pointers" in C or C++. 

foreach($breadcrumbs as $r){

and

if(empty($breadcrumbs)===false){

better approach will be

 if(!empty($breadcrumbs)){...}

Good Read

How does “&” operator work in PHP function?

Community
  • 1
  • 1
NullPoiиteя
  • 53,430
  • 22
  • 120
  • 137
1

Replace

foreach($breadcrumbs as &$r){

With:

foreach($breadcrumbs as $r){

Remove the &.

Praveen Kumar Purushothaman
  • 154,660
  • 22
  • 177
  • 226