PHP ARRAY search key and apply action
I have a array which contains values like this
See below for script sample
Suppose this is in a variable $Output_Data
I want to apply certain function on this $Output_Data listed below
- Remove Duplicate VALUE also associating KEY (but must keep one) i know this can be achieved by function array_unique but how to delete the array key
If img exist than put it in a variable adding img tag example
if exist img key in output data (or flv or mp4) $output_img[] = '<a href="$Output_Data[img]">Image "$i"</a>';
Like this for every key i mean FLV, MP4, IMG
Output to user screen Now again we created new Array separating the data
$output_img[] $output_flv[] $output_mp4[]
So if the array contains data than output it to user screen...(here also check if they exist or empty)
I am not a pro in this thing i just want to add a feature to my website and i hate array a lot but it is the only thing that will help me.
You can also mention a Alternative Solution for this... Because it is just my thought not a Necessity.
Sorry for wrong code here is a sample script
function sortoutput($data_to_sort,$key_explode_char) {
$output_content = array();
foreach (explode(PHP_EOL, $data_to_sort) as $cLine) {
list ($cKey, $cValue) = explode($key_explode_char, $cLine, 2);
$output_content[$cKey] = $cValue;
}
return $output_content;
}
ob_start();
$flv = 'http://example.com/example.flv';
$flv2 = 'http://example.com/example2.flv';
$img = 'http://example.com/my.jpg';
$mp4 = 'http://example.com/okay.mp4';
print 'flv=`'.$flv.PHP_EOL;
print 'flv=`'.$flv2.PHP_EOL;
开发者_运维问答 print 'img=`'.$img.PHP_EOL;
print 'mp4=`'.$mp4;
$Output_Download_Link = ob_get_contents();
ob_end_clean();
echo $Output_Download_Link;
$Sorted_Output_Links = sortoutput($Output_Download_Link, '=`');
echo '<pre>';
print_r($Sorted_Output_Links);
echo '</pre>';
You have to switch keys and values. A key is always unique! You can't have two or more entries with the same key name (except using a 2-dimensional array...).
After this you can get the keys of an array with array_keys() and use array_unique()
$array = array('url1' => 'flv', 'url2' => 'flv', 'url3' => 'png');
$keys = array_keys($array); //all urls
$keys = array_unique($keys); //remove url duplicates
$output = array( //all extensions which should be stored in array
'flv' => array(),
'png' => array(),
'mp4' => array(),
);
foreach($keys as $url) {
$extension = $array[$url];
if(isset($output[$extension])) {
$id = count($output[$extension]) + 1;
$output[$extension][] = "<a href=\"".$url."\">Image #".$id."</a>";
}
}
foreach($output as $o) {
//Output all arrays which aren't empty
if(!empty($o)) {
$o = implode('', $o);
echo $o.'<hr />';
}
}
A better way would be if you get the extension from the link directly using substr() and strrpos().
Than you can check whether $output[$extension] exists and create first an empty array() on $output[$extension]. This way you are more flexible with the extensions...
Edit: After you added a correct example...
You have to change this line: $output_content[$cKey] = $cValue;
//OLD: $output_content[$cKey] = $cValue;
//NEW:
if(!isset($output_content[$cKey])) {
$output_content[$cKey] = array();
}
$output_content[$cKey][] = "<a href=\"".$cValue."\">".$cValue."</a>";
And then you can check every entry of $output_content whether it is empty or not and output it. (have a look at the my first example above)
精彩评论