I have a script to add items to a db.
currently I have an array being created like this:
foreach(blah as $album){
$add[] = array('album' => $album['name'],
'test' => $album['test']
);
} // end foreach
the album name is not always unique, and $album['test']
can be a random text (which is used later) or it can be 'FALSE' (when i say false, I mean, the word false, spelled in text letters)
i need a function to somehow search the array, and compare the names and test values... there could be a few entries for each album name, with a few different random text values, and one saying FALSE. if there is an array entry with the test value set as FALSE, then it will remove all other contained arrays that have the same album name...
so this:
array(array('album' => 'album_name1', 'test' => 'xqus8n4n'),
array('album' => 'album_name1', 'test' => 'FALSE'),
array('album' => 'album_name1', 'test' => 'jdh4dhdk'),
array('album' => 'album_name2', 'test' => 'hdis8ebk'),
array('album' => 'album_name2', 'test' => 'hisb8bs7'),
array('album' => 'album_name2', 'test' => 'FALSE'),
array('album' => 'album_name3', 'test' => 'yek98abk'),
array('album' => 'album_name3', 'test' => '8iksh2q7')
);
becomes this:
array(array('album' => 'album_name1', 'test' => 'FALSE'),
array('album' => 'album_name2', 'test' => 'FALSE'),
array('album' => 'album_name3', 'test' => 'yek98abk'),
array('album' => 'album_name3', 'test' => '8iksh2q7')
);
I hope this makes sense, as it can seem a little confusing unless you know my whole script structure, this is why I gave so much info.
$multiarray = array(
array('album' => 'album_name1', 'test' => 'xqus8n4n'),
array('album' => 'album_name1', 'test' => 'FALSE'),
array('album' => 'album_name1', 'test' => 'jdh4dhdk'),
array('album' => 'album_name2', 'test' => 'hdis8ebk'),
array('album' => 'album_name2', 'test' => 'hisb8bs7'),
array('album' => 'album_name2', 'test' => 'FALSE'),
array('album' => 'album_name3', 'test' => 'yek98abk'),
array('album' => 'album_name3', 'test' => '8iksh2q7')
);
$keysToRemove = array( );
// Go and find all the FALSE values and which albums they belong to
foreach ( $multiarray as $album ) {
if ( in_array( 'FALSE', $album ) ) {
$keysToRemove[] = $album['album'];
}
}
// Remove all instances of the albums that had FALSE values
// except the ones that had the word FALSE (we keep those :p )
for( $i = 0; $i < count($multiarray); $i++ ) {
if ( ( in_array( $multiarray[$i]['album'], $keysToRemove ) ) && ( $multiarray[$i]['test'] != 'FALSE' ) ){
unset( $multiarray[$i] );
}
}
// reset keys so that they are numerical again.
$multiarray = array_values(array_filter($multiarray));
print_r( $multiarray );
Gives the following result:
Array (
[0] => Array ( [album] => album_name1 [test] => FALSE )
[1] => Array ( [album] => album_name2 [test] => FALSE )
[2] => Array ( [album] => album_name3 [test] => yek98abk )
[3] => Array ( [album] => album_name3 [test] => 8iksh2q7 )
)