codeinphp Posted June 3, 2015 Share Posted June 3, 2015 I know this should be fairly simple but I have having a really hard time understanding. I can parse html using tags and elements but I am not able to parse an array. I am loading an array from a file with the structure shown: <?php error_reporting(0); $items = array( array( "name" => "A&E", "number" => "", "id"=>"I265.28459333.microsoft.com", "country" => "United States", "url" => "68952", "url2" => "playlist.m3u8?xs=", "url3" => "playlist.m3u8?xs=", "url4" => "", "stream" => "http://streams/A&E.m3u8", "image" => "http://image/generic.png", "Guide" => "http://GUIDE/AE.xml", "liveguide" => "http:///streamguideXML/A&E.xml", "cid" => "9" ), array( "name" => "ABC", "number" => "", "id"=>"0", "country" => "United States", "url" => "46476", "url2" => "playlist.m3u8?xs=", "url3" => "playlist.m3u8?xs=", "url4" => "playlist.m3u8?xs=", "stream" => "http://ABC.m3u8", "image" => "http://image/generic.png", "Guide" => "http://GUIDE/ABCHD.xml", "liveguide" => "http://streamguideXML/ABC.xml", "cid" => "14" ), ?> I load the file by using file_get_contents but from there I am kinda of lost. I seem to think it's the way the file is structured that is confusing me but not sure. I have tried: $file=file_get_contents("http://channel_list.php"); foreach($items as $item){ $a=$item->cid; } But this doesn't work at all. I know the $file is populate, I can var_dump and shows everything. I want to pick out different elements but mainly the cid. If I could get some guidance to that I can take it from there. Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/296612-php-parse-array/ Share on other sites More sharing options...
blacknight Posted June 3, 2015 Share Posted June 3, 2015 foreach($items as $i => $item){$a=$item['cid'];} this should work -> is used in objects not arrays ususaly 1 Quote Link to comment https://forums.phpfreaks.com/topic/296612-php-parse-array/#findComment-1513069 Share on other sites More sharing options...
codeinphp Posted June 3, 2015 Author Share Posted June 3, 2015 When this is executed I get error stating "Undefined variable: items " and "nvalid argument supplied for foreach()". Quote Link to comment https://forums.phpfreaks.com/topic/296612-php-parse-array/#findComment-1513086 Share on other sites More sharing options...
codeinphp Posted June 3, 2015 Author Share Posted June 3, 2015 This works foreach($items as $item){$a=$item['cid];} Quote Link to comment https://forums.phpfreaks.com/topic/296612-php-parse-array/#findComment-1513087 Share on other sites More sharing options...
ginerjm Posted June 3, 2015 Share Posted June 3, 2015 Uh.... How is your $items array related at all to the second snippet of code you posted that reads a file into $file? And what do you expect to be read and stored from that snippet? You are simply reading a php file as a 'text' file and storing it as a string(s) in $file. $file will not be an array - it will be a string. Am I missing something? Quote Link to comment https://forums.phpfreaks.com/topic/296612-php-parse-array/#findComment-1513094 Share on other sites More sharing options...
CroNiX Posted June 3, 2015 Share Posted June 3, 2015 I believe you want to include() the file, which loads AND parses the php file, not use file_get_contents() which loads any file as a string as ginerjm pointed out. Quote Link to comment https://forums.phpfreaks.com/topic/296612-php-parse-array/#findComment-1513095 Share on other sites More sharing options...
ginerjm Posted June 3, 2015 Share Posted June 3, 2015 And with Cronix's tip making sense out of these posts, your code needs to reference the cid element like this: foreach ($items as $item) { $a = $item['cid']; ... ... } Your array is not an object nor does it contain objects, so no 'object notation' is necessary. It is simply an array reference. PS: Are you really constrained into using an array to store your data instead of a much more sensible and practical db table? Quote Link to comment https://forums.phpfreaks.com/topic/296612-php-parse-array/#findComment-1513100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.