3

I have two arrays

$list = Array
([0] => stdClass Object
    (
        [id] => 10
        [data] => "test data"
    )
[1] => stdClass Object
...
...(max 3000 ~ 4000 items)

and

$attributes = Array
([0] => stdClass Object
    (
        [ids] => 11
        [list] => '<ul>...</ul>'
    )
[1] => stdClass Object
...
...(max 3000 ~ 4000 items)

I am trying to left join them but the only performing way I am being able to write for this to be usable is

$nrrowslist = count($list);
for ($i = 0; $i < $nrrowslist; $i++){
  $nrat = count($attributes);
  for ($j = 0; $j < $nrat; $j++)
  {
    if($list[$i]->id == $attributes[$j]->ids){
      $list[$i]->attributes = $attributes[$j]->list;
      array_splice($attributes, $j, 1); // remove the item
      break; // since there is other item with that id 
    }
  }
}

// completes in ~0.470 seconds

But if I write it

foreach($list as $art){
  foreach($attributes as $attr){
    if($art->id == $attr->ids){
      $art->attributes = $attr->list;           
    }
  }
}

it completes in ~ 5.500 seconds.. and is too much

what can I do to perform even more the first method situation?

0

1 Answer 1

1

You may have some success with two iterations and using the common parameter in your assignment

$newlist = array();

// I'm sure there's a better way to do this initial assignment
foreach($list as $row)
{
  $newlist[$row->id] = $row;
}

foreach($attributes as $attr)
{
  if(isset($newlist[$attr->ids]) === true)
  {
    $newlist[$attr->ids]->attributes = $attr->list;
  }
}

var_dump($newlist);
Sign up to request clarification or add additional context in comments.

1 Comment

It runs faster. Thank you! (~ 0.280 second)

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.