Remove like items from associative array

This is sample POST data I need to save in a database:

    [suggested_quantity|sku1] => 
    [suggested_comment|sku1] => test
    [suggested_quantity|sku2] => 
    [suggested_comment|sku2] => test

Instead of looping through all POST data that could be 100 or more, and doing 100 or more db updates… is it possible to pull out all like data?

For example, instead of doing 2 db updates for sku1, is there a way to pull out those name/value pairs to add to 1 update statment?

Can you show the result of

echo var_export($yourarray, 1);

instead of the var_dump() ?

You’ll want to parse it out and group them into a 2 dimensional array like this. This makes it easy to build an insert query.

	foreach ($_POST as $k=>$v)
		$parts = explode('|', $k);
		if ($parts[1]) {
			$post[$parts[1]][sanitize($parts[0])] = sanitize($v);	
	echo '<pre>';
	print_r($post); die;

thanks for your help.

boss changed the specs so now it makes sense to just loop over the $_POST data. Your suggestions would have worked for me though :slight_smile:

What’s the difference between var_dump() and var_export()? I printed them on screen and it appears to hold the same information.

var_export outputs a proper PHP array that we can copy/paste – var_dump shows you what is in the array, but we have to type it out to replicate what you had.

What you posted as var_dump did not make sense to me – I wanted to see how it was written in PHP.


You mean, print_r(), right? (:[/ot]

Ah, was it so?

Just as equally useless if you want anyone to take your array problem and fix it for you.


Actually I don’t think I have used print_r since I was like, 7. var_dump and xDebug is a nice combo that shows you so much more data.