isimpledesign Posted February 2, 2011 Share Posted February 2, 2011 Hi Everyone I am looking to just grab the first element of an array and do something to it but i only want this extra bit off code on the first value off the array. Here is my array echo "<pre>"; print_r($_SESSION); echo "</pre>"; Array ( [attach] => Array ( ) [backups-2] => true [dnb] => true [house] => true [tech-house] => true [uk-garage] => true [uk-grime] => true [uk-hip-hop] => true [uncategorized] => true [warehouse] => true [value] => Array ( [0] => Array ( [tune_name] => Music/dnb/Abort_Delta Heavy_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Abort_Delta Heavy_192.mp3 [submit] => Listen ) [1] => Array ( [tune_name] => Music/dnb/Acid Bath (Northern Lights remix)_Twisted Individual_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Acid Bath (Northern Lights remix)_Twisted Individual_192.mp3 [submit] => Listen ) [2] => Array ( [tune_name] => Music/dnb/Awkward_Inside Info_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Awkward_Inside Info_192.mp3 [submit] => Listen ) [3] => Array ( [tune_name] => Music/dnb/Awkward_Inside Info_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Awkward_Inside Info_192.mp3 [submit] => Listen ) ) ) so i just what to effect the first value what to and autostart to it??? Here is my code. session_start(); header("Content-type: text/xml"); $xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; $xml_output .= '<playlist version="1" xmlns="http://xspf.org/ns/0/">\n'; $xml_output .= "<trackList>\n"; $xml_output .= "\t<track>\n"; $xml_output .= "\t\t<location>" . $_SESSION['value'][0]['tune_name'] . "</location>\n"; $xml_output .= "\t\t<creator>sdf</creator>\n"; $xml_output .= "\t\t<album>sdf</album>\n"; $xml_output .= "\t\t<title>" . $_SESSION['value'][0]['tune_name'] . "</title>\n"; $xml_output .= "\t\t<annotation>I love this song</annotation>\n"; $xml_output .= "\t\t<duration>32000</duration>\n"; $xml_output .= "\t\t<image>covers/smetana.jpg</image>\n"; $xml_output .= "\t\t<info></info>\n"; $xml_output .= "\t\t<link>" . $furl . "</link>\n"; $xml_output .= "\t</track>\n"; foreach ($_SESSION['value'] as $value) { $furl = "http://isdmusic.s3.amazonaws.com/".urlencode($value['tune_name']); if(preg_match("/\.mp3$/i", $furl)) { $xml_output .= "\t<track>\n"; $xml_output .= "\t\t<location>" . $furl . "</location>\n"; $xml_output .= "\t\t<creator>" . $value['tune_name'] . "</creator>\n"; $xml_output .= "\t\t<album>" . $value['tune_name'] . "</album>\n"; $xml_output .= "\t\t<title>" . $value['tune_name'] . "</title>\n"; $xml_output .= "\t\t<annotation>I love this song</annotation>\n"; $xml_output .= "\t\t<duration>32000</duration>\n"; $xml_output .= "\t\t<image>covers/smetana.jpg</image>\n"; $xml_output .= "\t\t<info></info>\n"; $xml_output .= "\t\t<link>" . $furl . "</link>\n"; $xml_output .= "\t</track>\n"; } } $xml_output .= "</trackList>"; echo $xml_output; Thanks Link to comment https://forums.phpfreaks.com/topic/226457-grab-first-element-off-array/ Share on other sites More sharing options...
BlueSkyIS Posted February 2, 2011 Share Posted February 2, 2011 if you want to REMOVE the first element and use it, array_shift() http://php.net/manual/en/function.array-shift.php $some_array = array('a', 'b', 'c'); $some_val = array_shift($some_array); echo "some_val: $some_val"; // output: a print_r($some_array); // output: b, c Link to comment https://forums.phpfreaks.com/topic/226457-grab-first-element-off-array/#findComment-1168859 Share on other sites More sharing options...
isimpledesign Posted February 2, 2011 Author Share Posted February 2, 2011 Hi Thanks for the reply sussed that. Got another issue now tho i am getting duplicate values out off my session array so i am ending up with a playlist as follows. warehouse_Dirt-Diver.mp3 warehouse_urban+beat.mp3 warehouse_Dirt-Diver.mp3 warehouse_urban+beat.mp3 warehouse_urban+beat.mp3 i only want the playlist to show. warehouse_Dirt-Diver.mp3 warehouse_urban+beat.mp3 Just show one i have tried unique_array but went i try that it only gives me one value???? heres my code. session_start(); header("Content-type: text/xml"); $xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; $xml_output .= '<playlist version="1" xmlns="http://xspf.org/ns/0/">\n'; $xml_output .= "<trackList>\n"; $turn = $_SESSION['value']; foreach (array_reverse($turn) as $value) { $furl = "http://isdmusic.s3.amazonaws.com/".urlencode($value['tune_name']); if(preg_match("/\.mp3$/i", $furl)) { $xml_output .= "\t<track>\n"; $xml_output .= "\t\t<location>" . $furl . "</location>\n"; $xml_output .= "\t\t<creator>" . $value['tune_name'] . "</creator>\n"; $xml_output .= "\t\t<album>" . $value['tune_name'] . "</album>\n"; $xml_output .= "\t\t<title>" . $value['tune_name'] . "</title>\n"; $xml_output .= "\t\t<annotation>I love this song</annotation>\n"; $xml_output .= "\t\t<duration>32000</duration>\n"; $xml_output .= "\t\t<image>covers/smetana.jpg</image>\n"; $xml_output .= "\t\t<info></info>\n"; $xml_output .= "\t\t<link>" . $furl . "</link>\n"; $xml_output .= "\t</track>\n"; } } $xml_output .= "</trackList>"; echo $xml_output; Link to comment https://forums.phpfreaks.com/topic/226457-grab-first-element-off-array/#findComment-1168905 Share on other sites More sharing options...
BlueSkyIS Posted February 2, 2011 Share Posted February 2, 2011 i think you mean array_unique(). i don't see it used in your code. Link to comment https://forums.phpfreaks.com/topic/226457-grab-first-element-off-array/#findComment-1168911 Share on other sites More sharing options...
isimpledesign Posted February 2, 2011 Author Share Posted February 2, 2011 hi yeah tried array_unique but i only want it to scan through the tune is the session array. here is the print_r() Array ( [attach] => Array ( ) [backups-2] => true [dnb] => true [house] => true [tech-house] => true [uk-garage] => true [uk-grime] => true [uk-hip-hop] => true [uncategorized] => true [warehouse] => true [value] => Array ( [0] => Array ( [tune_name] => Music/dnb/Abort_Delta Heavy_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Abort_Delta Heavy_192.mp3 [submit] => Listen ) [1] => Array ( [tune_name] => Music/dnb/Acid Bath (Northern Lights remix)_Twisted Individual_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Acid Bath (Northern Lights remix)_Twisted Individual_192.mp3 [submit] => Listen ) [2] => Array ( [tune_name] => Music/dnb/Awkward_Inside Info_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Awkward_Inside Info_192.mp3 [submit] => Listen ) [3] => Array ( [tune_name] => Music/dnb/Awkward_Inside Info_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Awkward_Inside Info_192.mp3 [submit] => Listen ) [4] => Array ( [tune_name] => Music/dnb/Awkward_Inside Info_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Awkward_Inside Info_192.mp3 [submit] => Listen ) [5] => Array ( [tune_name] => Music/dnb/Awkward_Inside Info_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Awkward_Inside Info_192.mp3 [submit] => Listen ) [6] => Array ( [tune_name] => Music/dnb/Awkward_Inside Info_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Awkward_Inside Info_192.mp3 [submit] => Listen ) [7] => Array ( [tune_name] => Music/dnb/Awkward_Inside Info_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Awkward_Inside Info_192.mp3 [submit] => Listen ) [8] => Array ( [tune_name] => Music/dnb/Awkward_Inside Info_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Awkward_Inside Info_192.mp3 [submit] => Listen ) [9] => Array ( [tune_name] => Music/dnb/Im_Not_A_Monster_Morebeat_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Im_Not_A_Monster_Morebeat_192.mp3 [submit] => Listen ) [10] => Array ( [tune_name] => Music/dnb/Im_Not_A_Monster_Morebeat_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Im_Not_A_Monster_Morebeat_192.mp3 [submit] => Listen ) [11] => Array ( [tune_name] => Music/dnb/Itzalot (Feat. Cabbie & Stapleton)_Crystal Clear_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Itzalot (Feat. Cabbie & Stapleton)_Crystal Clear_192.mp3 [submit] => Listen ) [12] => Array ( [tune_name] => Music/dnb/Itzalot (Feat. Cabbie & Stapleton)_Crystal Clear_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Itzalot (Feat. Cabbie & Stapleton)_Crystal Clear_192.mp3 [submit] => Listen ) [13] => Array ( [tune_name] => Music/dnb/Itzalot (Feat. Cabbie & Stapleton)_Crystal Clear_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Itzalot (Feat. Cabbie & Stapleton)_Crystal Clear_192.mp3 [submit] => Listen ) [14] => Array ( [tune_name] => Music/dnb/Im_Not_A_Monster_Morebeat_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Im_Not_A_Monster_Morebeat_192.mp3 [submit] => Listen ) [15] => Array ( [tune_name] => Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [submit] => Listen ) [16] => Array ( [tune_name] => Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [submit] => Listen ) [17] => Array ( [tune_name] => Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [submit] => Listen ) [18] => Array ( [tune_name] => Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [submit] => Listen ) [19] => Array ( [tune_name] => Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [submit] => Listen ) [20] => Array ( [tune_name] => Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [submit] => Listen ) [21] => Array ( [tune_name] => Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [submit] => Listen ) [22] => Array ( [tune_name] => Music/dnb/May Contain Nutz (VIP Mix)_Dub Zero_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/May Contain Nutz (VIP Mix)_Dub Zero_192.mp3 [submit] => Listen ) [23] => Array ( ) [24] => Array ( [tune_name] => Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [tune] => http://isdmusic.s3.amazonaws.com/Music/dnb/Let_The_Story_Begin_Sub_Focus_192.mp3 [submit] => Listen ) [25] => Array ( [tune_name] => Music/warehouse/01 Dirt Diver.m4a [tune] => http://isdmusic.s3.amazonaws.com/Music/warehouse/01 Dirt Diver.m4a [submit] => Listen ) [26] => Array ( [tune_name] => Music/warehouse/01 Get Off (Jack Beats Remix).m4a [tune] => http://isdmusic.s3.amazonaws.com/Music/warehouse/01 Get Off (Jack Beats Remix).m4a [submit] => Listen ) [27] => Array ( [tune_name] => Music/warehouse/01 Peep Thong.m4a [tune] => http://isdmusic.s3.amazonaws.com/Music/warehouse/01 Peep Thong.m4a [submit] => Listen ) [28] => Array ( [tune_name] => Music/warehouse/01 Dirt Diver.m4a [tune] => http://isdmusic.s3.amazonaws.com/Music/warehouse/01 Dirt Diver.m4a [submit] => Listen ) [29] => Array ( ) [30] => Array ( [tune_name] => Music/warehouse/01 Dirt Diver.m4a [tune] => http://isdmusic.s3.amazonaws.com/Music/warehouse/01 Dirt Diver.m4a [submit] => Listen ) [31] => Array ( [tune_name] => Music/warehouse/01 Dirt Diver.m4a [tune] => http://isdmusic.s3.amazonaws.com/Music/warehouse/01 Dirt Diver.m4a [submit] => Listen ) [32] => Array ( [tune_name] => Music/warehouse/01 Dirt Diver.m4a [tune] => http://isdmusic.s3.amazonaws.com/Music/warehouse/01 Dirt Diver.m4a [submit] => Listen ) ) [active] => ) Here is where i have added it in the code but then it doesnt work??? session_start(); header("Content-type: text/xml"); $xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; $xml_output .= '<playlist version="1" xmlns="http://xspf.org/ns/0/">\n'; $xml_output .= "<trackList>\n"; $turn = $_SESSION['value']; foreach (array_reverse($turn) as $value) { $furl = "http://isdmusic.s3.amazonaws.com/".urlencode($value['tune_name']); if(preg_match("/\.mp3$/i", array_unique ($furl))) { $xml_output .= "\t<track>\n"; $xml_output .= "\t\t<location>" . $furl . "</location>\n"; $xml_output .= "\t\t<creator>" . $value['tune_name'] . "</creator>\n"; $xml_output .= "\t\t<album>" . $value['tune_name'] . "</album>\n"; $xml_output .= "\t\t<title>" . $value['tune_name'] . "</title>\n"; $xml_output .= "\t\t<annotation>I love this song</annotation>\n"; $xml_output .= "\t\t<duration>32000</duration>\n"; $xml_output .= "\t\t<image>covers/smetana.jpg</image>\n"; $xml_output .= "\t\t<info></info>\n"; $xml_output .= "\t\t<link>" . $furl . "</link>\n"; $xml_output .= "\t</track>\n"; } } $xml_output .= "</trackList>"; echo $xml_output; Link to comment https://forums.phpfreaks.com/topic/226457-grab-first-element-off-array/#findComment-1168916 Share on other sites More sharing options...
BlueSkyIS Posted February 2, 2011 Share Posted February 2, 2011 $furl isn't an array, so using an array function on it will not be productive. $furl = "http://isdmusic.s3.amazonaws.com/".urlencode($value['tune_name']); if(preg_match("/\.mp3$/i", array_unique ($furl))) { // $furl is a string, not an array. seeing the duplicates makes me think the duplicates should not be there to start. are you using a query that produces duplicate records? or are you given an array with duplicates and there is no way around that? Link to comment https://forums.phpfreaks.com/topic/226457-grab-first-element-off-array/#findComment-1168918 Share on other sites More sharing options...
isimpledesign Posted February 2, 2011 Author Share Posted February 2, 2011 Hi what i am doing is looping through my amazon and using a popup form to add the info to a session. <form action="http://example.co.uk/music-player" target="popupWin" onsubmit="return openWindow('http://example-player', 'popupWin', 250, 350);" class="play" method="get"> <input type="hidden" id="tune_name" name="tune_name" value="<?php echo $fname; ?>"/> <input type="hidden" id="tune" name="tune" value="<?php echo $furl; ?>"/> <input type="submit" name="submit" value="Listen" /> </form> everytime someone click a track it adds it to a session array. session_start(); $test = $_GET; $_SESSION['value'][]=$test; echo "<pre>"; print_r($_SESSION); echo "</pre>"; so i can prevent them clicking the tracks duplicate times just need to get rid off them in the playlist session array. Link to comment https://forums.phpfreaks.com/topic/226457-grab-first-element-off-array/#findComment-1168927 Share on other sites More sharing options...
BlueSkyIS Posted February 2, 2011 Share Posted February 2, 2011 okay, this might work: $turn = $_SESSION['value']; $turn = array_unique(array_reverse($turn)); foreach ($turn as $value) { // etc. Link to comment https://forums.phpfreaks.com/topic/226457-grab-first-element-off-array/#findComment-1168933 Share on other sites More sharing options...
isimpledesign Posted February 2, 2011 Author Share Posted February 2, 2011 Hi no ive tried that i think its not working because it scan the whole array for duplicate when i only need it to scan through ['tune'] Link to comment https://forums.phpfreaks.com/topic/226457-grab-first-element-off-array/#findComment-1168942 Share on other sites More sharing options...
Omangryn Posted August 28, 2012 Share Posted August 28, 2012 Hi no ive tried that i think its not working because it scan the whole array for duplicate when i only need it to scan through ['tune'] I am looking for some code for switching LED Lights. Can you provide that. Link to comment https://forums.phpfreaks.com/topic/226457-grab-first-element-off-array/#findComment-1373270 Share on other sites More sharing options...
PFMaBiSmAd Posted August 28, 2012 Share Posted August 28, 2012 A) Stop bumping old thread having nothing to do with what you are asking. B) The point of a programming help forum is not to give or help you find code to do what you want. C) If you want someone to write code for you, post your question in the freelancing help forum. D) If you have a programming question, programming problem, or programming error that you need help with, start your own thread. E) If you continue doing #A, your account will be banned. Link to comment https://forums.phpfreaks.com/topic/226457-grab-first-element-off-array/#findComment-1373274 Share on other sites More sharing options...
Recommended Posts