mysql - PHP Best way to get to compare to assoc arrays and create a new one with matchings -
image have 2 assocs arrays:
1
array 0 => object(stdclass) public 'id' => string '1' public 'desc' => string 'lorem ipsum' 1 => object(stdclass) public 'id' => string '5' public 'desc' => string 'lorem ipsum' 2 => object(stdclass) public 'id' => string '7' public 'desc' => string 'lorem ipsum' 3 => object(stdclass) public 'id' => string '8' public 'desc' => string 'lorem ipsum' 4 => object(stdclass) public 'id' => string '12' public 'desc' => string 'lorem ipsum' 5 => object(stdclass) public 'id' => string '13' public 'desc' => string 'lorem ipsum'
2
array 0 => object(stdclass) public 'id' => string '2' public 'desc' => string 'lorem ipsum' 1 => object(stdclass) public 'id' => string '5' public 'desc' => string 'lorem ipsum' 2 => object(stdclass) public 'id' => string '7' public 'desc' => string 'lorem ipsum' 3 => object(stdclass) public 'id' => string '58' public 'desc' => string 'lorem ipsum' 4 => object(stdclass) public 'id' => string '66' public 'desc' => string 'lorem ipsum' 5 => object(stdclass) public 'id' => string '78' public 'desc' => string 'lorem ipsum'
what best way create new array, have entries matches @ 'id'? doing sql query inner join on 2 tables contain data.
the result should this:
array 0 => object(stdclass) public 'id' => string '5' public 'desc' => string 'lorem ipsum' 1 => object(stdclass) public 'id' => string '7' public 'desc' => string 'lorem ipsum'
i trying around bit, doing array map checking each object if there equivalent in other array seems not best solution this.
thanks
this works me:
<?php $arr1 = array( (object) array( 'id' => '4', 'desc' => 'four' ), (object) array( 'id' => '5', 'desc' => 'five' ), (object) array( 'id' => '3', 'desc' => 'three' ) ); $arr2 = array( (object) array( 'id' => '2', 'desc' => 'two' ), (object) array( 'id' => '4', 'desc' => 'four' ), (object) array( 'id' => '6', 'desc' => 'six' ) ); $arr3 = array(); foreach( $arr1 $obj1 ){ foreach( $arr2 $obj2 ){ if( $obj1->id == $obj2->id ){ $arr3[] = $obj1; break; } } } echo '<pre>'; print_r( $arr3 ); echo '</pre>';
Comments
Post a Comment