0

I have one foreach like this

$ret=array();
foreach($temp as $k=>$v)
{
    $thv=mysql_fetch_array(mysql_query(" SOMEQUERY "));
    $ret[]=$thv;
}

Here im pushing every output of $thv to $ret like $ret[]=$thv; and output of $ret is,

[1] => Array
    (
        [0] => 701
        [id] => 701
        [1] => 1180
        [media_image_id] => 1180
        [2] => George Cumming - Session 1
        [name] => George Cumming - Session 1
        [3] => 
        [preparation] => 
        [4] => 
        [description] => 
        [5] => 
        [coaching_points] => 
        [6] => 
        [progressions] => 
    )

[2] => Array
    (
        [0] => 701
        [id] => 701
        [1] => 1180
        [media_image_id] => 1180
        [2] => George Cumming - Session 1
        [name] => George Cumming - Session 1
        [3] => 
        [preparation] => 
        [4] => 
        [description] => 
        [5] => 
        [coaching_points] => 
        [6] => 
        [progressions] =>
    )

Here id=>701 repeating, so what i want to do is, remove duplicate values from array but within that foreach loop. Like,

if(id=>701 NOT EXIST IN $ret)
{
    $ret[]=$thv;
}

SO that way no need to create another foreach. Anyone have idea how to do this in php?

2
  • The mysql extension is deprecated. New code should use PDO or mysqli, both of which have important advantages, such as support for prepared statements. Also, results can be iterated over directly (though mysqli requires PHP 5.4 for this) and the results can be fetched into an array all at once (in other words, each extension already has a function that does what your loop does). Duplicates should be prevented by writing the SQL statement properly, not by filtering them in PHP. Commented Jul 14, 2015 at 7:53
  • Duplicate of "Remove duplicates from PHP array" and probably many others. Commented Jul 14, 2015 at 8:03

3 Answers 3

3

I've an idea - use the id as the key of $ret. Example:

$ret=array();
foreach($temp as $k=>$v)
{
    $thv=mysql_fetch_array(mysql_query(" SOMEQUERY "));
    if (!isset($ret[$thv['id']])){
         $ret[$thv['id']]=$thv;
    }
}

If you still want 0..n to be the keys of $ret, you can do like this:

$ret = array_values($ret);
Sign up to request clarification or add additional context in comments.

Comments

0
$ret = array();
$ids = array();
foreach($temp as $k => $v) {
    // Run query
    $thv = mysql_fetch_array(mysql_query(" SOMEQUERY "));

    // Check if id present
    if (!in_array($thv['id'], $ids)) {
        // Not present, add to arrays
        $ids[] = $thv['id'];
        $ret[] = $thv;
    }
}

3 Comments

in_array() is a poor choice, as it has to search the array. Overall, it makes this O(n^2). If you need a set of scalars, use the scalars as keys, not values.
@outis : Sure, I know that, but for a piece of code like this that should not be any problem. I am pretty sure this query is not used to send people into space. I know about the time complexity, but it is simple and readable.
the problem is it's a poor example. People will be learning from this code. You can write code that's just as simple and reasonable but is also performant by changing two lines.
0

Try this code.

$ret=array();
foreach($temp as $k=>$v)
{
    $temp =array_column($ret,'id');
    $thv=mysql_fetch_array(mysql_query(" SOMEQUERY "));
    if(!in_array($thv['id'],$temp)){
       $ret[]=$thv;
    }

}

1 Comment

Even worse performance than OptimusCrime's answer. array_column() is also O(n), plus you're recreating the $temp array each iteration, even though it only differs by having an extra item ($thv['id']) each time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.