Filtering arrays based on the value of a key inside of that array in PHP -
i have ton of parsed xml records related 2 attributes: id
, rid
.
these xml records, when parsed, create following (example) php array:
array( 'master' => array( [0] => array( 'id' => 123, 'info' => 'hello' ), [1] => array( 'id' => 456, 'info' => 'world' ) ), 'tbl-ex' => array( [0] => array( 'rid' => 123, 'test' => 'aye', 'num' => 88 ), [1] => array( 'rid' => 123, 'test' => 'bbb', 'num' => 99 ), [2] => array( 'rid' => 456, 'test' => 'zzz', 'num' => 102, 'o' => 'h') ), 'tbl-ey' => array( [0] => array( 'rid' => 456, 'mama' => 'mia' ), [1] => array( 'rid' => 123, 'oof' => 'foo' ) ), 'tbl-ez' => array( [0] => array( 'rid' => 123, 'stop' => 'wegetit', 'ok' => 1 ), [1] => array( 'rid' => 456, 'finally' => 'done' ) ) );
when parse this, want records under master
returned on condition id == rid
, in format:
array( [0] => array( 'master' => array( 'id' => 123, 'info' => 'hello' ), 'tbl-ex' => array( [0] => array( 'test' => 'aye', 'num' => 88 ), [1] => array( 'test' => 'bbb', 'num' => 99 ) ), 'tbl-ey' => array( [0] => array( 'oof' => 'foo' ) ), 'tbl-ez' => array( [0] => array( 'stop' => 'wegetit', 'ok' => 1 ) ) ), [1] => array( 'master' => array( 'id' => 456, 'info' => 'world' ), 'tbl-ex' => array( [0] => array( 'test' => 'zzz', 'num' => 102, 'o' => 'h' ) ), 'tbl-ey' => array( [0] => array( 'mama' => 'mia' ) ), 'tbl-ez' => array( [0] => array( 'finally' => 'done' ) ) ) );
this seems obvious (and works):
$ret = array(); $init_record = $original_array['master']; $records_to_merge = $original_array; unset($records_to_merge['master']); $i = 0; foreach($init_record $master_index => $master){ $current_id = $master['id']; foreach($records_to_merge $record_type => $record_array){ foreach($record_array $index => $record){ if($current_id == $record['rid']){ $ret[$i]['master'] = $master; $ret[$i][$record_type] = $record; $i++; } } } }
this works, since have ton of records (and lot more data), it's excruciatingly slow.
i tried use array_filter
callback function evaluates $record
, followed index rid
being equal $master_id
. filters out matching record, , not entire array contained in. bunch of matching rid
's in array no other data.
is there fast, easy way i'm overlooking right now?
here's solution has 1 nested foreach
instead of two. i'm not sure if make difference since every element of array iterated through. makes 1 change in structure of new array: outer array associative instead of indexed. order should preserved, though.
php > $original = array( php ( 'master' => array( php ( array( 'id' => 123, 'info' => 'hello' ), php ( array( 'id' => 456, 'info' => 'world' ) php ( ), php ( 'tbl-ex' => array( php ( array( 'rid' => 123, 'test' => 'aye', 'num' => 88 ), php ( array( 'rid' => 123, 'test' => 'bbb', 'num' => 99 ), php ( array( 'rid' => 456, 'test' => 'zzz', 'num' => 102, 'o' => 'h') php ( ), php ( 'tbl-ey' => array( php ( array( 'rid' => 456, 'mama' => 'mia' ), php ( array( 'rid' => 123, 'oof' => 'foo' ) php ( ), php ( 'tbl-ez' => array( php ( array( 'rid' => 123, 'stop' => 'wegetit', 'ok' => 1 ), php ( array( 'rid' => 456, 'finally' => 'done' ) php ( ) php ( ); php > php > //$merge = $original; php > //unset($merge['master']); php > unset($pointer); php > php > php > foreach($original $tbl => $data) { php { php { foreach($data $row) { php { if($tbl=='master') { php { $pointer[$row['id']]['master'] = $row; php { continue; php { } php { $pointer[$row['rid']][$tbl][] = $row; php { php { } php { php { } php > php > print_r($pointer); array ( [123] => array ( [master] => array ( [id] => 123 [info] => hello ) [tbl-ex] => array ( [0] => array ( [rid] => 123 [test] => aye [num] => 88 ) [1] => array ( [rid] => 123 [test] => bbb [num] => 99 ) ) [tbl-ey] => array ( [0] => array ( [rid] => 123 [oof] => foo ) ) [tbl-ez] => array ( [0] => array ( [rid] => 123 [stop] => wegetit [ok] => 1 ) ) ) [456] => array ( [master] => array ( [id] => 456 [info] => world ) [tbl-ex] => array ( [0] => array ( [rid] => 456 [test] => zzz [num] => 102 [o] => h ) ) [tbl-ey] => array ( [0] => array ( [rid] => 456 [mama] => mia ) ) [tbl-ez] => array ( [0] => array ( [rid] => 456 [finally] => done ) ) ) ) php >
Comments
Post a Comment