wemustdesign Posted March 12, 2009 Share Posted March 12, 2009 I have looked on the Internet but cannot find an array example that suits my needs. The array that I am outputting in a 'add to favorites' similar to a shopping cart. I am currently outputting the array using the code below. This output ok but doesn't work when removing the items from the favorites via a query string. What I need is something that will output the array and the array key within a query string like '&remove_from_favotites&id=$ARRAY_KEY" $i=0; while ($i < $favorites_Size){ echo $_SESSION['favoriteArray'][$i]." <a href=$url&function=remove_favorite&cocktail=$favoritesArray[$i]&ai=$i>[x]</a><br />"; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/149080-solved-outputting-an-array/ Share on other sites More sharing options...
sasa Posted March 12, 2009 Share Posted March 12, 2009 try $_SESSION['favoriteArray']= array_values($_SESSION['favoriteArray']); $i=0; while ($i < $favorites_Size){ echo $_SESSION['favoriteArray'][$i]." <a href=$url&function=remove_favorite&cocktail=$favoritesArray[$i]&ai=$i>[x]</a><br />"; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/149080-solved-outputting-an-array/#findComment-782810 Share on other sites More sharing options...
wemustdesign Posted March 12, 2009 Author Share Posted March 12, 2009 No this does not work. I thik I can see where the problem is. When I output the array it outputs like below with the querystring matching the array key (Array key [0])Cocktail remove=0 (Array key [1])Cocktail1 remove=1 (Array key [2])Cocktail2 remove=2 If I delete Cocktail1 from the favorites it then outputs like this: (Array key [0])Cocktail remove=0 (Array key [2])Cocktail2 remove=1 This is because the querystring is generated by $i. I need the querystring to output the same value as the arraykey if you know what I men Quote Link to comment https://forums.phpfreaks.com/topic/149080-solved-outputting-an-array/#findComment-782814 Share on other sites More sharing options...
Mikedean Posted March 12, 2009 Share Posted March 12, 2009 Sorry if I've misunderstood what you want to do, but this should do the trick. $theArray[0] = "Cocktail"; $theArray[1] = "Cocktail1"; $theArray[2] = "Cocktail2"; foreach( $theArray as $arrayKey => $arrayValue ) echo $arrayValue." <a href=$url&function=remove_favorite&cocktail=" . $arrayValue . "&ai=" . $arrayKey . ">[x]</a><br />"; Quote Link to comment https://forums.phpfreaks.com/topic/149080-solved-outputting-an-array/#findComment-782818 Share on other sites More sharing options...
wemustdesign Posted March 12, 2009 Author Share Posted March 12, 2009 Work perfect, thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/149080-solved-outputting-an-array/#findComment-782821 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.