Jump to content

Loop Multiple Arrays


Go to solution Solved by cyberRobot,

Recommended Posts

Hi All,

 

Arrays seem to be my downfall so hope someone can help!

 

I have the following:

			$pa_no_of_doors_value = array_shift( wc_get_product_terms( 1008, 'pa_no-of-doors', array( 'fields' => 'names' ) ) );
			echo '<br/>No Of Doors: '.$pa_no_of_doors_value.'<br/>';
						
			$pa_no_of_doors['type'] = array(
				'name' => htmlspecialchars(stripslashes('No Of Doors')),
				'value' => $pa_no_of_doors_value,
				'position' => 1,
				'is_visible' => 1,
				'is_variation' => 0,
				'is_taxonomy' => 0
			);
			print_r($pa_no_of_doors);
			
			echo '<p> </p>';
			
			$pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) );
			echo '<br/>Car Make: '.$pa_car_make_value.'<br/>';
						
			$pa_car_make['type'] = array(
				'name' => htmlspecialchars(stripslashes('Car Make')),
				'value' => $pa_car_make_value,
				'position' => 1,
				'is_visible' => 1,
				'is_variation' => 0,
				'is_taxonomy' => 0
			);	
			print_r($pa_car_make);

I then use:

update_post_meta($post_id, '_product_attributes', $pa_car_make);

The above then updates the DB (based on Wordpress).

 

As you can see, this is only updating with one of the options. What I'm trying to do is a foreach loop (as I've got many more add, then pass all of them into the _product_attributes field

 

Having added them via Wordpress and looked at the DB, I can see how it needs to then appear:


a:2:{
	s:8:"car-make";a:6:{s:4:"name";s:8:"Car Make";s:5:"value";s:12:"Aston Martin";s:8:"position";s:1:"0";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:0;}
	s:14:"pa_no-of-doors";a:6:{s:4:"name";s:14:"pa_no-of-doors";s:5:"value";s:0:"";s:8:"position";s:1:"1";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}
}	

So thinking something like:

 


update_post_meta($post_id, '_product_attributes', serialize( $attributes ));

But my brain just can't work out how to do a foreach of the various types! Sorry for asking a probably simple question!!

Link to comment
https://forums.phpfreaks.com/topic/300718-loop-multiple-arrays/
Share on other sites

First I would imagine that the "1008" found in the line below:

$pa_no_of_doors_value = array_shift( wc_get_product_terms( 1008, 'pa_no-of-doors', array( 'fields' => 'names' ) ) );

...is going to be the same as $post_id here:

$pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) );

Assuming that's correct, you'll want to use the variable instead of hard coding the number.

 

 

As for the loop, what are you trying to loop through? A series of post IDs?

 

Or does wc_get_product_terms() in the following code pass back multiple values that you need to process?

$pa_no_of_doors_value = array_shift( wc_get_product_terms( 1008, 'pa_no-of-doors', array( 'fields' => 'names' ) ) );
$pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) );

Hi cyberRobot

 

Thanks for the reply.

 

Basically, I'm trying to grab the values from a submitted form and pass them into an array.

 

I'm making progress and now have:

$attributes = array(
	'pa_car-make' => array_shift( wc_get_product_terms( $post_id , 'pa_car-make', array( 'fields' => 'names' ) ) ), 
	'pa_no-of-doors' => array_shift( wc_get_product_terms( $post_id, 'pa_no-of-doors', array( 'fields' => 'names' ) ) )
);
?><pre><?php print_r($attributes); ?></pre>


<?php
echo '<hr/>';

$i = 0;
// Loop through the attributes array
foreach ($attributes as $name => $value) {
	$product_attributes[$i] = array (
		'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name
		'value' => $value, // set attribute value
		'position' => 1,
		'is_visible' => 1,
		'is_variation' => 0,
		'is_taxonomy' => 0,
		'row' => $i
	);

	$i++;
	
?><pre><?php print_r($product_attributes); ?></pre>


<?php
}

But for some odd reason, it shows the first row twice:

Array
(
    [1] => Array
        (
            [name] => pa_car-make
            [value] => Aston Martin
            [position] => 1
            [is_visible] => 1
            [is_variation] => 0
            [is_taxonomy] => 0
            [row] => 1
        )

)

Array
(
    [1] => Array
        (
            [name] => pa_car-make
            [value] => Aston Martin
            [position] => 1
            [is_visible] => 1
            [is_variation] => 0
            [is_taxonomy] => 0
            [row] => 1
        )

    [2] => Array
        (
            [name] => pa_no-of-doors
            [value] => 3 Doors
            [position] => 1
            [is_visible] => 1
            [is_variation] => 0
            [is_taxonomy] => 0
            [row] => 2
        )

)

Which I can't fathom out!?

 

oh and it was my error on the post_id / 1008 you pointed out, it was from me testing

Ah! Of course - thank you

 

With regards to:

$attributes = array(
	'pa_car-make' => array_shift( wc_get_product_terms( $post_id , 'pa_car-make', array( 'fields' => 'names' ) ) ), 
	'pa_no-of-doors' => array_shift( wc_get_product_terms( $post_id, 'pa_no-of-doors', array( 'fields' => 'names' ) ) )
);

Is it possible to check if the value is empty? As I know the following isn't possible:


$attributes = array(
if(isset($_POST['item_meta'][132])){
	$pa_no_of_doors_value = array_shift( wc_get_product_terms( $post_id, 'pa_no-of-doors', array( 'fields' => 'names' ) ) );
	
if(isset($_POST['item_meta'][242])){		
	$pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) );
}
);

Thanks

Thanks again.

 

Using:

$attributes = array(
	if(empty($attributes['pa_no-of-doors'])) {
		'no-of-doors' => $pa_no_of_doors_value = array_shift( wc_get_product_terms( $post_id, 'pa_no-of-doors', array( 'fields' => 'names' ) ) );
	}
	if(empty($attributes['pa_car-make'])) {		
		'car-make' => $pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) );
	}
);

Gives an error:

Parse error: syntax error, unexpected 'if' (T_IF), expecting ')'​

 

Which points to line:

if(empty($attributes['pa_no-of-doors'])) {​

 

But I've a feeling I've just misunderstood?

 

Thanks again and apologies

Sorry about the confusion, the $attributes array doesn't exist until after the following block of code:

$attributes = array(
    'pa_car-make' => array_shift( wc_get_product_terms( $post_id , 'pa_car-make', array( 'fields' => 'names' ) ) ), 
    'pa_no-of-doors' => array_shift( wc_get_product_terms( $post_id, 'pa_no-of-doors', array( 'fields' => 'names' ) ) )
);
 
So the if empty test needs to take place after the array is created. Assuming that you only want the foreach loop to execute if the "pa_car-make" value isn't empty, you could try something like this:
if(!empty($attributes['pa_car-make'])) {
    $i = 0;
    // Loop through the attributes array
    foreach ($attributes as $name => $value) {
        $product_attributes[$i] = array (
            'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name
            'value' => $value, // set attribute value
            'position' => 1,
            'is_visible' => 1,
            'is_variation' => 0,
            'is_taxonomy' => 0,
            'row' => $i
        );
        $i++;
    }
}

 

  • Solution

Looking at your code a bit closer, you probably want to run the test within the loop. Maybe something like this:

foreach ($attributes as $name => $value) {
    if(!empty($value)) {
        $product_attributes[$i] = array (
            'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name
            'value' => $value, // set attribute value
            'position' => 1,
            'is_visible' => 1,
            'is_variation' => 0,
            'is_taxonomy' => 0,
            'row' => $i
        );
        $i++;
    }
}

That way one of your attributes can be empty and the loop will still execute.

Edited by cyberRobot

Thanks @cyberRobot

 

That seems to have sorted it! Final code:

$pa_no_of_doors_value = array_shift( wc_get_product_terms( $post_id, 'pa_no-of-doors', array( 'fields' => 'names' ) ) );
$pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) );

$attributes = array(
    'pa_car-make' => $pa_no_of_doors_value, 
    'pa_no-of-doors' => $pa_car_make_value,
);


$i = 0;
// Loop through the attributes array
foreach ($attributes as $name => $value) {
	if(!empty($value)) {	
		$product_attributes[$i] = array (
			'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name
			'value' => $value, // set attribute value
			'position' => 1,
			'is_visible' => 1,
			'is_variation' => 0,
			'is_taxonomy' => 0
		);
	
		$i++;
	}
}
print_r($product_attributes);

Thanks for all your help!

No problem; glad to help!  :happy-04:

 

For what it's worth, you could eliminate some duplication by calling wc_get_product_terms() in a loop:

$taxonomyOfInterest = array(
    'pa_car-make',
    'pa_no-of-doors'
);
 
$attributes = array();
foreach($taxonomyOfInterest as $currTaxonomy) {
    $attributes[$currTaxonomy] = array_shift( wc_get_product_terms( $post_id , $currTaxonomy, array( 'fields' => 'names' ) ) );
}

@cyberRobot

Thanks again for your help yesterday! 

 

I know the last post you sent was to help prevent repetition by using this code:

		$taxonomyOfInterest = array(
			'pa_car-make',
			'pa_no-of-doors',
		);
		 
		$attributes = array();
		foreach($taxonomyOfInterest as $currTaxonomy) {
			$attributes[$currTaxonomy] = array_shift( wc_get_product_terms( $post_id , $currTaxonomy, array( 'fields' => 'names' ) ) );
		} 

I've managed to get a loop of all the attributes I need that are being manually typed in the first part:

		$taxonomyOfInterest = array(
			'pa_car-make',
			'pa_no-of-doors',
		); 

This block of code produces the inner part:

$attribute_taxonomies = wc_get_attribute_taxonomies();
if ( $attribute_taxonomies ) :
	foreach ($attribute_taxonomies as $tax) :
		echo esc_html(wc_attribute_taxonomy_name($tax->attribute_name)).'<br/>'; 
	endforeach;
endif; 

Which produces:

 

pa_car-make
pa_no-of-doors

 

And any others in the CMS, so negates having to add them into the code manually. However, I'm struggling to see how I can effectively do this:

$taxonomyOfInterest = array(
$attribute_taxonomies = wc_get_attribute_taxonomies();
if ( $attribute_taxonomies ) :
	foreach ($attribute_taxonomies as $tax) :
		esc_html(wc_attribute_taxonomy_name($tax->attribute_name)) 
	endforeach;
endif;
);

Or is this not possible?

Thanks again

You could try something like this:

$taxonomyOfInterest = array();
$attribute_taxonomies = wc_get_attribute_taxonomies();
if ( $attribute_taxonomies ) :
    foreach ($attribute_taxonomies as $tax) :
        $taxonomyOfInterest[] = esc_html(wc_attribute_taxonomy_name($tax->attribute_name));
    endforeach;
endif;

Thanks cyberRobot!

 

I came up with this:

		$attribute_taxonomies = wc_get_attribute_taxonomies();
		if ( $attribute_taxonomies ) :
			$taxonomyOfInterest = array();
			foreach ($attribute_taxonomies as $tax=>$tax_value) :
		
				#echo "Key=" . $tax . ", Value=" . $tax_value->attribute_name.'<br/>'; #debug
				$taxonomyOfInterest[] = 'pa_'.$tax_value->attribute_name;
				 
			endforeach;
		endif;
		#print_r($taxonomyOfInterest);	

Is this acceptable or have I overcomplicated something?

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.