darkfreaks Posted August 21, 2012 Share Posted August 21, 2012 are you making sure the array is not empty or exists Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371241 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. //making sure array is set and is not empty if(isset($output) && !empty($output)){ print_r($output); echo $output; } else{ print "Array is not set or is empty";} Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371253 Share on other sites More sharing options...
mrooks1984 Posted August 21, 2012 Author Share Posted August 21, 2012 this is the full code //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) //setting output inside variable $output= $options['option_title']." : ".$options['option_value']." "; //output option title and value. //making sure array is set and is not empty if(isset($output) && !empty($output)){ print_r($output); echo $output; }else{ print "Array is not set or is empty"; } i am getting this still: Notice: Undefined index: option_value in C:\xampp\htdocs\jubileeleather\site\_class\store.php on line 239 Notice: Undefined index: option_value in C:\xampp\htdocs\jubileeleather\site\_class\store.php on line 239 Notice: Undefined index: option_title in C:\xampp\htdocs\jubileeleather\site\_class\store.php on line 239 : Black : Black this is 239: $output= $options['option_title']." : ".$options['option_value']." "; doesent seem to like this: $options['option_title'] or $options['option_value'] array print out is same as other post. thanks for your help so far Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371257 Share on other sites More sharing options...
Jessica Posted August 21, 2012 Share Posted August 21, 2012 OP: Can you state in English what you are trying to do? Not what your existing code does, but what your end goal is? What data do you have, and what do you want to do with it? Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371258 Share on other sites More sharing options...
Barand Posted August 21, 2012 Share Posted August 21, 2012 ... and why you persist with an array structure that is giving you so many problems Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371286 Share on other sites More sharing options...
mrooks1984 Posted August 22, 2012 Author Share Posted August 22, 2012 hello jesirose this was my code before, all i want it to do is stop looping when there are no values left, it currently get values from a form and puts the results into 2 arrays one is a list of all the titles ($product_option_title) and one is all the values ($product_option_value), the values are selected from a drop down menu, so sometimes there is less values then titles, but sometimes the values and the titles are the same. so again if say the values is black, the titles are color and size, if only color is selected in the drop down menu the value would only have the value black. So what it needs to do is go right there is only black in the value, get the title then stop the loop, instead of going color black, size black. hope i have explained it clear enough. as i said before in a post i don't think it needs merging into another array(might be wrong). //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; Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371340 Share on other sites More sharing options...
carugala Posted August 22, 2012 Share Posted August 22, 2012 aye, aye, aye... just foreach the titles, and then use the index for your value array. You are going thru each title, and then each value. you want the vals for the title. foreach titles as id foreach value[id] as newid wait, you need this.... the key foreach (array_expression as $key => $value), so foreach (titles as $key => $title_value) nest foreach (values[$key]=> $val_value) name your variables better. shorter, and don't use ambiguous names, like value, or each, or if, or not, or continue, or break..... Try: $prod_name $prod_desc Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371343 Share on other sites More sharing options...
mrooks1984 Posted August 22, 2012 Author Share Posted August 22, 2012 thanks for the post, could you do a example so i can see what you mean please? Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371344 Share on other sites More sharing options...
jazzman1 Posted August 22, 2012 Share Posted August 22, 2012 Everything in your script is a little mess. You want to be something like this: $arr_title = array("Colours","Values","Test","Something else"); $arr_opt = array("Black","76",'',"something else"); $arr_res = array_combine($arr_title, $arr_opt); foreach ($arr_res as $key => $value) { if($value == ''){ // skip empty values continue; } echo "Title: ".$key."\n"."Value: ".$value.'<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371389 Share on other sites More sharing options...
mrooks1984 Posted August 22, 2012 Author Share Posted August 22, 2012 thanks for your responce, the issue is with this code it sometimes only has one value in the post see the code below. //Get option values foreach($_POST['option_value'] as $key => $value){ if($value !=''){ $product_option_values[] = array("option_value" => $value); } } so if i replicate this from your code: $arr_title = array("Colours","Values","Test","Something else"); $arr_opt = array("Black","76"); $arr_res = array_combine($arr_title, $arr_opt); foreach ($arr_res as $key => $value) { if($value == ''){ // skip empty values continue; } echo "Title: ".$key."\n"."Value: ".$value.'<br />'; } i get this error: Warning: array_combine(): Both parameters should have an equal number of elements in C:\xampp\htdocs\jubileeleather\site\_class\store.php on line 237 this is the main issue, is sometimes the values has less results then the title, so needs to unstead of looping the one value around all the titles, is just to stop when there isnt any more values. Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371402 Share on other sites More sharing options...
Christian F. Posted August 22, 2012 Share Posted August 22, 2012 mrooks1984: The best tip I can give you, at this point, is to step back and thinks things through properly before starting to write code. If you're confused about what you want to do or the steps you need to take (even just a single one), then the code you write will be bugged. Writing code is just translating a solved problem from one language (your native) to another (PHP in this case). So if you haven't solved the problem in your native, how can you expect to solve it in a language you're less fluent in? Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371422 Share on other sites More sharing options...
Jessica Posted August 22, 2012 Share Posted August 22, 2012 Your explanation was kind of the opposite of what I asked for. Christian is right, you need to start over and figure out what you're trying to do. Write an outline for it. What are the steps you're trying to accomplish. No arrays, no dropdowns, just the ideas. Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371424 Share on other sites More sharing options...
ialsoagree Posted August 22, 2012 Share Posted August 22, 2012 In the same vane as what ChristianF wrote, it seems like you're making things harder for yourself than you have to. Why are you allowing them to have multiple selections, only make a few, and then you have to figure out which ones they made? Write your program so it's obvious which selections they did and did not make so that you don't have to write complicated logic to figure it out. In other words, if you they can submit color and size, but only submit color, then make the selection of size something you know is a non-selection, like the string "no selection". Then you'll always have 1 input for each option, and you can always know which options were and weren't chosen. Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371426 Share on other sites More sharing options...
mrooks1984 Posted August 22, 2012 Author Share Posted August 22, 2012 thanks for your responces, looks like it not possible the way i wanted too, i go away and have a rethink. thanks again all. Quote Link to comment https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/page/2/#findComment-1371456 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.