Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/267381-php-array-duplicating-a-value-help/
Share on other sites

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

}

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

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
}

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']." ";

 

 

 

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

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;

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

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

 

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;

}

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?

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']." ";

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.