Append a value to an array ELEMENT

Discussion in 'PHP' started by Omzy, Feb 13, 2009.

  1. #1
    
    $cats=array($cat1, $cat2, $cat3, $cat4, $cat5);
    
    foreach($category as $index => $value)
    {
    	if(in_array($index, $cats))
    	{
    		$output[]= '<a href="/directory/'.$index.'/">'.$value[2].'</a>';
    		
    		foreach($subcats as $index1 => $value1)
    		{
    			if($value1[0] == $index)
    			{
    				$output[]= $value1[2];
    			}
    		}
    	}
    }
    
    $output = implode(' | ',$output);
    echo $output;
    
    Code (markup):
    I want the INNER foreach loop to append it's value to the current element of the $output[] array, rather than adding a new element.
     
    Omzy, Feb 13, 2009 IP
  2. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Basically what I'm trying to acheive is to not have the 'pipe' symbol appear when the inner foreach loop is run - if I can get the value appended to the current element of the array then it isn't going to display it.
     
    Omzy, Feb 13, 2009 IP
  3. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    $cats=array($cat1, $cat2, $cat3, $cat4, $cat5);
    $i = 0;
    foreach($category as $index => $value)
    {
    	if(in_array($index, $cats))
    	{
    		$output[$i]= '<a href="/directory/'.$index.'/">'.$value[2].'</a>';
    		
    		foreach($subcats as $index1 => $value1)
    		{
    			if($value1[0] == $index)
    			{
    				$output[$i] .= $value1[2];
    			}
    		}
            $i++;
    	}
    }
    
    $output = implode(' | ',$output);
    echo $output;
    
    PHP:
     
    Shoro, Feb 13, 2009 IP
  4. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks shoro, that works perfect :)
     
    Omzy, Feb 14, 2009 IP