Jump to content

PHP Parse Array


codeinphp

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.