Compare an item within a $i loop

I’m having a little trouble iterating through a loop.

I basically want to compare if a value within the loop is the same, if it is keep the old one and skip the next one.

I want to compare the user-agent to create a robots.txt file so I need to keep the last one in but not the next if it’s the same:

Code
[PHP]
if (isset($_POST[‘submit’])){

$count = $_POST[‘items’];

for( $i = 1; $i <= $count; $i++ ){
$string = "User-Agent: " . $_POST[‘useragent’.$i] . PHP_EOL . $_POST[‘action’.$i] . $_POST[‘url’.$i] . PHP_EOL;
echo $string;
}
}
[/PHP]

Expected output:

User-Agent: Googlebot Allow: /admin Allow: /account User-Agent: * Disallow: /includes

Actual output:

User-Agent: Googlebot Allow: /admin User-Agent: Googlebot Allow: /account User-Agent: * Disallow: /includes

Everything I try pretty much just stops the loop. Any ideas?

You could do something like this:
[php]
$values = array();

foreach($_POST[‘items’] as $post){
if(!in_array($post,$values)){
array_push($values,$post);

     /* DO THE REST HERE */
}

}
[/php]

array_unique is your friend here :wink:

Example:
[php]<?php
// i’ve created an array, you already have one - $_POST
$useragent = array(‘Googlebot’, ‘*’, ‘BingBot’, ‘Googlebot’);

// normal output:
print ‘

’;
print_r($useragent);
print ‘
’;
/*
Array
(
[0] => Googlebot
[1] => *
[2] => BingBot
[3] => Googlebot
)
*/

// run it through array_unique()
$useragent = array_unique($useragent);

// now let’s see what we have…
print ‘

’;
print_r($useragent);
print ‘
’;
/*
Array
(
[0] => Googlebot
[1] => *
[2] => BingBot
)
*/
*?>[/php]

Hope that helps,
Red :wink:

Hahaha are you trying to one up me lol

Thanks for your help.

Still struggling with both methods. Here is what I’m posting:

[PHP]
Array
(
[url1] => /admin
[useragent1] => Googlebot
[action1] => Allow:
[url2] => /account
[useragent2] => Googlebot
[action2] => Allow:
[url3] => /includes
[useragent3] => *
[action3] => Allow:
[submit] => Submit values
[items] => 3
)
[/PHP]

With your code Redscouse, here is my output:

[PHP]
Array
(
[0] => Array
(
[url1] => /admin
[useragent1] => Googlebot
[action1] => Allow:
[url2] => /account
[useragent2] => Googlebot
[action2] => Allow:
[url3] => /includes
[useragent3] => *
[action3] => Allow:
[submit] => Submit values
[items] => 3
)

)
Array
(
[0] => Array
(
[url1] => /admin
[useragent1] => Googlebot
[action1] => Allow:
[url2] => /account
[useragent2] => Googlebot
[action2] => Allow:
[url3] => /includes
[useragent3] => *
[action3] => Allow:
[submit] => Submit values
[items] => 3
)

)
[/PHP]

With Lewisstevens1’s code I did this:

[PHP]

<?php if (isset($_POST['submit'])){ $count = $_POST['items']; $useragent = $_POST; $values = array(); for( $i = 1; $i <= $count; $i++ ){ // $string = "User-Agent: " . $_POST['useragent'.$i] . PHP_EOL . $_POST['action'.$i] . $_POST['url'.$i] . PHP_EOL; // echo $string; //echo $useragent['useragent'.$i] . "
"; if(!in_array($useragent['useragent'.$i],$values)){ array_push($values,$useragent['useragent'.$i]); print '
';
	  	  print_r($values);
	  	  print '
'; /* DO THE REST HERE */ } } } ?>

[/PHP]

Which produced this:

[PHP]
Array
(
[0] => Googlebot
)
Array
(
[0] => Googlebot
[1] => *
)
[/PHP]

In the form, I use JS to add a new row to the form and add a count to the post which is called ‘items’, I wouldn’t normally do this as I’ve never had the need to add elements to a form dynamically before, I think that’s why I’m a little stuck.

Where can I go from there?

Cheers guys!

[php]if (isset($_POST[‘submit’])){
$count = $_POST[‘items’];
$useragent = $_POST;
$values = array();

for($i = 1; $i <= $count; $i++){
	if(!in_array($useragent['useragent'.$i],$values)){
        array_push($values,$useragent['useragent'.$i]);
		$string = "User-Agent: " . $_POST['useragent'.$i] . PHP_EOL . $_POST['action'.$i] . $_POST['url'.$i] . PHP_EOL;
		echo $string;
    }
}

}[/php]

Morning chaps :slight_smile:

Sorry Scott, didn’t realise you had a multidimensional array, try this
[php]$useragent = array (
array(‘url1’ => ‘/admin’, ‘useragent1’ => ‘Googlebot’, ‘action’ => ‘Allow:’),
array(‘url2’ => ‘/account’, ‘useragent2’ => ‘Googlebot’, ‘action’ => ‘Allow:’),
array(‘url3’ => ‘/includes’, ‘useragent3’ => ‘*’, ‘action’ => ‘Allow:’)
);

print ‘

’;
print_r($useragent);
print ‘
’;
/*
Array
(
[0] => Array
(
[url1] => /admin
[useragent1] => Googlebot
[action] => Allow:
)
[1] => Array
    (
        [url2] => /account
        [useragent2] => Googlebot
        [action] => Allow:
    )

[2] => Array
    (
        [url3] => /includes
        [useragent3] => *
        [action] => Allow:
    )

)
*/

foreach($useragent as $agent) {
$useragent = array_unique($agent);
}
print ‘

’;
print_r($useragent);
print ‘
’;
/*
Array
(
[url3] => /includes
[useragent3] => *
[action] => Allow:
)
[/php]

Red :wink:

PS: I just notice it dropped both ‘googles’…
BRB…

Scott, I grabbed a coffee and now I’m back with a solution. It’s a bit ‘dirty’ but it works:
[php]$post = array (
array(‘useragent’ => ‘Googlebot’, ‘action’ => ‘Allow:’, ‘url’ => ‘/admin’),
array(‘useragent’ => ‘Googlebot’, ‘action’ => ‘Allow:’, ‘url’ => ‘/account’),
array(‘useragent’ => ‘*’, ‘action’ => ‘Disallow:’, ‘url’ => ‘/includes’)
);

$count = sizeof($post);

$string = $useragent = $url = ‘’;
for($i=0; $i<$count; $i++) {
// let’s check if the value was the same on last pass…
if($post[$i][‘useragent’] != $useragent)
$string .= "User-Agent: " . $post[$i][‘useragent’] . ‘
’;

if($post[$i]['url'] != $url) 
	$string .= $post[$i]['action'] . $post[$i]['url'] . '<br>';


// let's remember what each pass was
$useragent = $post[$i]['useragent'];
$url = $post[$i]['url'];

}
echo $string;

/*
User-Agent: Googlebot
Allow:/admin
Allow:/account
User-Agent: *
Disallow:/includes
*/
[/php]

Sorry for the confusion earlier - tired eyes :-[

Hope that helps,
Red :wink:

Almost!

I don’t know if I’m having a bad week or what, but I can’t output the array via $_POST.

I think I’ve been looking at this code too much my heads a little bamboozled lol.

[PHP]
if (isset($_POST[‘submit’])){
function br2nl( $input ) {
return preg_replace(’/<br(\s+)?/?>/i’, “\n”, $input);
}

$total = $_POST[‘items’];
$items = array($_POST);

$post = array (
array(‘useragent’ => $items[‘useragent’][$i], ‘action’ => $items[‘action’][$i], ‘url’ => $items[‘url’][$i])
);

$count = sizeof($post);

$string = $useragent = $url = ‘’;
for($i=0; $i<$count; $i++) {
// let’s check if the value was the same on last pass…
if($post[$i][‘useragent’] != $useragent)
$string .= "User-Agent: " . $post[$i][‘useragent’] . PHP_EOL;

if($post[$i]['url'] != $url) 
	$string .= $post[$i]['action'] . $post[$i]['url'] . PHP_EOL;


// let's remember what each pass was
$useragent = $post[$i]['useragent'];
$url = $post[$i]['url'];

}
echo $string;

}
[/PHP]

Are you trying to create the $_POST element into a multidimensional array - as the $_POST element is already an array…?
array($_POST);

If so you would need to call
$items[‘useragent’][$i]

as

$items[0][‘useragent’][$i]

It’s already in an array posted from a form on a different page, here is the actual $_POST output:

[PHP]
Array ( [url1] => /admin [useragent1] => Googlebot [action1] => Allow: [url2] => /sitemap.xml [useragent2] => Googlebot [action2] => Allow: [url3] => /sitemap.xml [useragent3] => * [action3] => Disallow: [submit] => Submit values [items] => 3 ) User-Agent: Googlebot
[/PHP]

It’s the useragent1, useragent2 etc that I’m struggling with. The form I wrote is dynamic where I can add new rows using JS, I’ve never done it that way before.

in your form i suspect you have something like this:

<input name="useragent1"..
<input name="useragent2"..

and you’re adding like so:

$('div').append('<input name="useragent+i"..)

Would this be correct?
If so, a little rejig of your code would make life easier.

<input name="useragent[]"..
<input name="useragent[]"..

and

$('div').append('<input name="useragent[]"..)

Would that not be better than numbering all the inputs yourself?
Red :wink:

Ha, I swear I tried that yesterday and it broke the script.

Anyway, I’ve changed that over and now have a pretty looking array.

Here is my code:

[PHP]
if (isset($_POST[‘submit’])){

$post = array(
		array(
		    'useragent' 	=> $_POST['useragent'],
		    'action' 		=> $_POST['action'],
		    'url' 		=> $_POST['url'],
		)
);

$count = sizeof($post);
  
$string = $useragent = $url = '';
for($i=0; $i<$count; $i++) {
	// let's check if the value was the same on last pass..
	if($post[$i]['useragent'] != $useragent) 
		$string .= "User-Agent: " . $post[$i]['useragent'] . '<br>';

	if($post[$i]['url'] != $url) 
		$string .= $post[$i]['action'] . $post[$i]['url'] . '<br>';
	 	
	 	// let's remember what each pass was
	 	$useragent = $post[$i]['useragent'];
	 	$url = $post[$i]['url'];
}

echo $string;

}
[/PHP]

I’m now getting [QUOTE]Notice: Array to string conversion in[/QUOTE] on lines 19 & 22.

Thanks for your help, don’t know what’s up with me this week. I’m off next week that’s probably my problem! 8)

Does your post array look like this?
[php]// post array
$_POST[0][‘useragent’] = ‘Googlebot’;
$_POST[0][‘action’] = ‘Allow:’;
$_POST[0][‘url’] = ‘/admin’;

$_POST[1][‘useragent’] = ‘Googlebot’;
$_POST[1][‘action’] = ‘Allow:’;
$_POST[1][‘url’] = ‘/account’;

$_POST[2][‘useragent’] = ‘*’;
$_POST[2][‘action’] = ‘Allow:’;
$_POST[2][‘url’] = ‘/includes’;

$count = sizeof($_POST);

$string = $useragent = $url = ‘’;
for($i=0; $i<$count; $i++) {
// let’s check if the value was the same on last pass…
if($_POST[$i][‘useragent’] != $useragent)
$string .= "User-Agent: " . $_POST[$i][‘useragent’] . ‘
’;

if($_POST[$i]['url'] != $url) 
	$string .= $_POST[$i]['action'] . $_POST[$i]['url'] . '<br>';
	 	
// let's remember what each pass was
$useragent = $_POST[$i]['useragent'];
$url = $_POST[$i]['url'];

}

echo $string;

/*
User-Agent: Googlebot
Allow:/admin
Allow:/account
User-Agent: *
Allow:/includes
*/
[/php]

It’s driving me nuts lol.

Here’s my post directly from the form:

[PHP]
Array
(
[url] => Array
(
[0] => /account
[1] => /includes
[2] => /admin
)

[useragent] => Array
    (
        [0] => Googlebot
        [1] => Googlebot
        [2] => *
    )

[action] => Array
    (
        [0] => Allow: 
        [1] => Allow: 
        [2] => Disallow: 
    )

[submit] => Submit values
[items] => 3

)
[/PHP]

Trying to get it into your array:

[PHP]

<?php if (isset($_POST['submit'])){ echo '
';
	print_r($_POST);
	echo '
'; $counter = 0; foreach($_POST as $key => $value) { $counter++; $post = array(); $post[$counter]['useragent'] = $_POST['useragent']; $post[$counter]['action'] = $_POST['action']; $post[$counter]['url'] = $_POST['url']; } $count = sizeof($post); $string = $useragent = $url = ''; for($i=0; $i<$count; $i++) { // let's check if the value was the same on last pass.. if($post[$i]['useragent'] != $useragent) $string .= "User-Agent: " . $post[$i]['useragent'] . '
'; if($post[$i]['url'] != $url) $string .= $post[$i]['action'] . $post[$i]['url'] . '
'; // let's remember what each pass was $useragent = $post[$i]['useragent']; $url = $post[$i]['url']; } echo $string; } ?>

[/PHP]

The more I try the worse it gets lol!

Got it at long last:

[PHP]
if (isset($_POST[‘submit’])){

$post = $_POST;

$count = $_POST['items'];

$string = $useragent = $url = '';
for($i=0; $i<$count; $i++) {

	// let's check if the value was the same on last pass..
	if($post['useragent'][$i] != $useragent) 
		$string .= "User-Agent: " . $post['useragent'][$i] . '<br>';

	if($post['url'][$i] != $url) 
		$string .= $post['action'][$i] . $post['url'][$i] . '<br>';
	 	
	 	// let's remember what each pass was
	 	$useragent = $post['useragent'][$i];
	 	$url = $post['url'][$i];
}

echo $string;

}
[/PHP]

Thanks for your help Red.

No worries mate, happy to help. Glad you got it sorted :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service