mrooks1984 Posted August 21, 2012 Share Posted August 21, 2012 hello all i am hoping someone can help me, i have to arrays some times the product_option_values has less or the same values as the product_option_titles when its less i want it to stop when its run out of values instead of continuing and duplicating the product_option_values until product_option_values has run out of values: see the $cart_option here, Test : Black heres the code, really hope someone can help me i have been trying to figure it out all day. //Create cart option $cart_option = ''; if (isset($product_option_values)) { foreach ($product_option_titles as $product_option_title) { foreach ($product_option_values as $product_option_value) { $cart_option .= $product_option_title['option_title']." : ".$product_option_value['option_value']." "; } } } print_r($product_option_titles); print_r($product_option_values); echo $cart_option; heres the print_r's and the echo product_option_titles = Array ( [0] => Array ( [option_title] => Colours ) [1] => Array ( [option_title] => Test ) ) product_option_values = Array ( [0] => Array ( [option_value] => Black ) ) $cart_option = Colours : Black Test : Black thanks all. Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/ Share on other sites More sharing options...
MMDE Posted August 21, 2012 Share Posted August 21, 2012 http://php.net/manual/en/function.isset.php http://php.net/manual/en/function.count.php Those two functions will probably help you! Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371152 Share on other sites More sharing options...
mrooks1984 Posted August 21, 2012 Author Share Posted August 21, 2012 thanks for your re ponce, i had tried them earlier today but it dident make a difference. Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371156 Share on other sites More sharing options...
darkfreaks Posted August 21, 2012 Share Posted August 21, 2012 change: if (isset($product_option_values)) {//checks if value is set to: if (isset($product_option_values) && !empty($product_option_values)) { //checks if value is set and not empty no zero no float value 0.0 no NULL no FALSE then you could do something with count $count_option_values = count($count_option_values); $count_option_titles = count($count_option_titles); for($i=0; $ < $count_option_values; $i++ { //code that goes after counting loop for values } for($i=0; $ < $count_option_titles; $i++ { //code that goes after counting loop for titles } Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371169 Share on other sites More sharing options...
mrooks1984 Posted August 21, 2012 Author Share Posted August 21, 2012 hi thanks again this is the print_r : Array ( [0] => Array ( [option_title] => Colours ) [1] => Array ( [option_title] => Test ) ) Array ( [0] => Array ( [option_value] => Black ) ) and the echo: Colours : Black Test : Black heres the code now: if (isset($product_option_values) && !empty($product_option_values)) { //checks if value is set and not empty no zero no float value 0.0 no NULL no FALSE foreach ($product_option_titles as $product_option_title) { foreach ($product_option_values as $product_option_value) { $cart_option .= $product_option_title['option_title']." : ".$product_option_value['option_value']." "; } } } print_r($product_option_titles); print_r($product_option_values); echo $cart_option; thanks again Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371171 Share on other sites More sharing options...
darkfreaks Posted August 21, 2012 Share Posted August 21, 2012 ran out of editing time so repost here..... using count in a proper array loop $count_option_values = count($count_option_values); $count_option_titles = count($count_option_titles); for($i=0; $i < $count_option_values; $i++ { //code that goes after counting loop for values } for($i=0; $i < $count_option_titles; $i++ { //code that goes after counting loop for titles } Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371173 Share on other sites More sharing options...
mrooks1984 Posted August 21, 2012 Author Share Posted August 21, 2012 thanks for getting back to me i did have a look at count but couldent figure out how to do it, so how do i add the stuff to get each loop to add them togeather like this: $cart_option .= $product_option_title['option_title']." : ".$product_option_value['option_value']." "; Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371179 Share on other sites More sharing options...
darkfreaks Posted August 21, 2012 Share Posted August 21, 2012 look into array_merge or additionally you can do $array1+$array2; Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371181 Share on other sites More sharing options...
mrooks1984 Posted August 21, 2012 Author Share Posted August 21, 2012 i also tried that but i couldn't get it to add the spacing and : between the title and the value and get it to call the results. there must be a way to tell it to stop looping the option title when there are no values, just cant figure out how Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371186 Share on other sites More sharing options...
darkfreaks Posted August 21, 2012 Share Posted August 21, 2012 //merge together both arrays $cart_options .= $product_options_titles+$product_option_values; //for testing purposes print_r($cart_options); Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371194 Share on other sites More sharing options...
darkfreaks Posted August 21, 2012 Share Posted August 21, 2012 //merge together both arrays $new_cart_options = $product_options_titles+$product_option_values; //merged arrays //loop combined arrays foreach($new_cart_options as $key => $value) { //print key value print $key.$value; } Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371196 Share on other sites More sharing options...
mrooks1984 Posted August 21, 2012 Author Share Posted August 21, 2012 i get this now: Notice: Undefined variable: cart_options on line 241 Notice: Array to string conversion on line 241 Array heres the code //Create cart option $cart_option = ''; //checks if value is set and not empty no zero no float value 0.0 no NULL no FALSE if (isset($product_option_values) && !empty($product_option_values)) { //merge together both arrays $cart_options .= $product_option_titles+$product_option_values; //for testing purposes print_r($cart_options); } echo $cart_option; Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371197 Share on other sites More sharing options...
darkfreaks Posted August 21, 2012 Share Posted August 21, 2012 then you want to use array_merge instead of the PLUS Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371198 Share on other sites More sharing options...
mrooks1984 Posted August 21, 2012 Author Share Posted August 21, 2012 with your last post i get this: Notice: Array to string conversion on line 239 0Array Notice: Array to string conversion on line 239 1Array Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371199 Share on other sites More sharing options...
darkfreaks Posted August 21, 2012 Share Posted August 21, 2012 //merge together both arrays $new_cart_options =array_merge($product_options_titles,$product_option_values); //loop combined arrays foreach($new_cart_options as $key => $value) { //print key value print $key.$value; } Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371200 Share on other sites More sharing options...
mrooks1984 Posted August 21, 2012 Author Share Posted August 21, 2012 i am now getting this: Notice: Array to string conversion on line 239 0Array Notice: Array to string conversion on line 239 1Array Notice: Array to string conversion on line 239 2Array Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371201 Share on other sites More sharing options...
darkfreaks Posted August 21, 2012 Share Posted August 21, 2012 can we see your code "this doesnt work" does not tell me where the problem is. and i have no clue what line 239 is. Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371203 Share on other sites More sharing options...
mrooks1984 Posted August 21, 2012 Author Share Posted August 21, 2012 //merge together both arrays $new_cart_options =array_merge ($product_option_titles,$product_option_values); //merged arrays //loop combined arrays foreach($new_cart_options as $key => $value) { //print key value print $key.$value; } 239 is print $key.$value; Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371204 Share on other sites More sharing options...
darkfreaks Posted August 21, 2012 Share Posted August 21, 2012 print "$key = $value/n"; i got no string error. Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371206 Share on other sites More sharing options...
mrooks1984 Posted August 21, 2012 Author Share Posted August 21, 2012 strange i changed to to the $key = and i get: Notice: Array to string conversion in C:\xampp\htdocs\jubileeleather\site\_class\store.php on line 239 0 = Array/n Notice: Array to string conversion in C:\xampp\htdocs\jubileeleather\site\_class\store.php on line 239 1 = Array/n Notice: Array to string conversion in C:\xampp\htdocs\jubileeleather\site\_class\store.php on line 239 2 = Array/n Notice: Array to string conversion in C:\xampp\htdocs\jubileeleather\site\_class\store.php on line 239 3 = Array/n Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371207 Share on other sites More sharing options...
darkfreaks Posted August 21, 2012 Share Posted August 21, 2012 lets try it without the key. //merge together both arrays $new_cart_options =array_merge($product_option_titles,$product_option_values); //merged arrays //loop combined arrays foreach($new_cart_options as $options) { print $options; } Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371212 Share on other sites More sharing options...
Jessica Posted August 21, 2012 Share Posted August 21, 2012 OMG. $value is an array. It doesn't matter how many times you print it, it will say Array. To see what's in an array you can use print_r(). Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371214 Share on other sites More sharing options...
mrooks1984 Posted August 21, 2012 Author Share Posted August 21, 2012 thanks for that, dident notice that, until you pointed it out, displaying fine now. i need to have a think how to get it displayed like i need it now title - value then if no value dont display the title this is the print array Array ( [option_title] => Colours ) Array ( [option_title] => Test ) Array ( [option_value] => Black ) Array ( [option_value] => Bacon ) for the other arrays i did in the script i just do this to show certain results echo $options['option_title']; when i do this i get this: Notice: Undefined index: option_title in C:\xampp\htdocs\jubileeleather\site\_class\store.php on line 239 so how do i do this please? Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371226 Share on other sites More sharing options...
darkfreaks Posted August 21, 2012 Share Posted August 21, 2012 foreach($new_cart_options as $options) //setting output inside variable $output= $options['option_title']." : ".$options['option_value']." "; //output option title and value. print_r($output); echo $output; Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371236 Share on other sites More sharing options...
mrooks1984 Posted August 21, 2012 Author Share Posted August 21, 2012 i am getting this: Notice: Undefined index: option_value in C:\xampp\htdocs\jubileeleather\site\_class\store.php on line 240 Colours : Colours : Notice: Undefined index: option_value in C:\xampp\htdocs\jubileeleather\site\_class\store.php on line 240 Test : Test : Notice: Undefined index: option_title in C:\xampp\htdocs\jubileeleather\site\_class\store.php on line 240 : Black : Black this is 240: $output= $options['option_title']." : ".$options['option_value']." "; Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/#findComment-1371238 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.