Jump to content

Recommended Posts

Hello all,

Basically I have two arrays. The first array, $one, has 9 numbers ( 1 , 2 , 3 , 4 , 3 , 6 , 1 , 3 , 2 ) , { please not that there are numbers that are double } . Now, I'd like to have $two with just ( 1 , 2 ,3 ,4 , 6 ) , that is, $one but without the doubles. I have tried several solutions, and the closest i went is the following code, however, it is crashing my server. I can understand where its crashing it, but I somehow cant figure out the solution.


[code]
<?php

$one = array( 1 , 2 );
$two = array( "1" );



for ( $i=0 ; $i<count($one) ; $i++ )
  {
  $set = 0;
  for ( $c=0 ; $c<count($two) ; $c++ )
    {
    if ( trim($one[$i] ) !== trim($two[$c] ) )
      {
       
        $set = 1;
       
      }
      if ( $set == 1 )
        {
        $two[] = $one[$i];

     
    }
  }
 

?>

[/code]

thanks beforehand,
dragunu
Link to comment
https://forums.phpfreaks.com/topic/33575-adding-to-an-array-from-a-nested-loop/
Share on other sites

You've way over complicated things here.  Try this:

[code]<?php
// Assign values to array
$one = array(1,2,3,4,3,6,1,3,2);

// Create an array of unique values from $one
$two = array_unique($one);

// Output as proof
echo "<pre>\n";
print_r($two);
echo "</pre>\n";
?>[/code]

Regards
Huggie
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.