Jump to content

php array duplicating a value help


mrooks1984

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.

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

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

}

 

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_options_titles,$product_option_values); 
//loop combined arrays
foreach($new_cart_options as $key => $value) {
//print key value
print $key.$value; 

}

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

Archived

This topic is now archived and is closed to further replies.

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