Jump to content

php array duplicating a value help


mrooks1984
 Share

Recommended Posts

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";}

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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 />';
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.