plexus Posted August 23, 2008 Share Posted August 23, 2008 Hi all, I'm not very conversant with PHP but just some quick questions on PHP associative array. Saw this code somewhere and I've got some questions: <?php $mainArray = array(); // Is there such thing? $mainArray[] = array('date' => '2008-08-23', 'content' => <<<PBLOCK <div class="css-class"> <h2><a href="http://somelink">Title</a></h2> Content text here... </div> PBLOCK ); ?> Is it possible to break the content of the array into multiple lines and display as per above? What's the difference between $mainArray[] (with the bracket) and $mainArray (without bracket)? Quote Link to comment https://forums.phpfreaks.com/topic/120982-associative-array/ Share on other sites More sharing options...
ratcateme Posted August 23, 2008 Share Posted August 23, 2008 try adding print_r($mainArray) to the bottom of your script to see what happens when you have $mainArray[] = '1'; $mainArray[] = '2'; $mainArray[] = '3'; $mainArray[] = '4'; you are going to get a array that looks like 0 => 1 1 => 2 2 => 3 3 => 4 the [] is like a auto incrementing index Scott. Quote Link to comment https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-623668 Share on other sites More sharing options...
plexus Posted August 24, 2008 Author Share Posted August 24, 2008 I get what you mean Scott. What about the <<<PBLOCK item in the code? Is there anyway to have multiple lines for the 'content' item in the array? Quote Link to comment https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-624060 Share on other sites More sharing options...
DarkWater Posted August 24, 2008 Share Posted August 24, 2008 It's called a Heredoc and I personally think it's ugly. You can always do it with simple quotes... $something = array('foo' =>'bar bar bar bar', 'foo2' => 'bar2'); Although it's rather annoying to do it that way. >_< Why? Quote Link to comment https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-624066 Share on other sites More sharing options...
plexus Posted August 24, 2008 Author Share Posted August 24, 2008 Hmm. Why would you say it's ugly to do it this way? Assuming I use Heredoc in the following code, how can I use a loop method to display the contents of the array? <?php $mainArray = array(); $mainArray[] = array('date' => '2008-08-24', 'content' => <<<HEREDOC <div class="main"> <h2>template tag title goes here</h2> 'template tags goes here' </div> HEREDOC ); Quote Link to comment https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-624086 Share on other sites More sharing options...
JasonLewis Posted August 24, 2008 Share Posted August 24, 2008 Well you're making the array then another array. So you could do something like this: foreach($mainArray as $arr){ foreach($arr as $key => $value){ echo "{$key} = {$value}<br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-624091 Share on other sites More sharing options...
plexus Posted August 24, 2008 Author Share Posted August 24, 2008 Alright. I get the picture of how it works. However, there are some portions I would like to amend as follows: <?php $mainArray = array(); $mainArray[] = array('type' => 'main', 'date' => '2008-08-24 HH:MM:SS', 'content' => <<<HEREDOC <div class="main"> <h2>template tag title goes here</h2> 'template tag contents goes here' </div> HEREDOC ); $subArray = array(); $subArray[] = array('type' => 'sub', 'date' => '2008-08-24 HH:MM:SS', 'content' => <<<HEREDOC <div class="sub"> <h2>title for sub array content goes here</h2> 'template tag for sub array content goes here' </div> HEREDOC ); ?> For the above code, I've introduced another array ($subArray) to store contents taken from different tables. I would like to display the information as follows: 1. For each array, 'type' refers to the type of content for that array. 'date' refers to the date the entry was posted. 2. Display only the array codes from 'content' 3. Display the array items from both arrays (e.g. main, sub) in order of 'date'. The content don't have to be all from main followed by sub. It's purely base on date and time. Quote Link to comment https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-624131 Share on other sites More sharing options...
plexus Posted August 25, 2008 Author Share Posted August 25, 2008 Mmm, anyone has any idea of the above? Quote Link to comment https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-624821 Share on other sites More sharing options...
nrg_alpha Posted August 25, 2008 Share Posted August 25, 2008 Well, I'm not familar with databases, but something tells me that you could sort or compare tables in your database and use that as starting factor in sorting by date or whatever you need to sort by... But as for your code sample, there are some issues.. First, you do not have to declare a variable as an array first: ex.. no need for - $mainArray = array(); Secondly, when you declare an array, don't use [].. just declare the array as such: $mainArray = array(...); Php will automatically create the needed keys and put the values listed within the parathesis into them. Third, with regards to your array keys containing the heredoc setup, you do not have any quotes encompasing those values. As far as the heredoc stuff, I'm not sure why you would want that.. here is a sample of your code altered somewhat: $mainArray = array('type' => 'main', 'date' => '2008-08-24 HH:MM:SS', 'content' => '<div class="main"> <h2>template tag title goes here</h2> \'template tag contents goes here\' </div>' ); $subArray = array('type' => 'sub', 'date' => '2008-08-24 HH:MM:SS', 'content' => '<div class="sub"> <h2>title for sub array content goes here</h2> \'template tag for sub array content goes here\' </div>' ); foreach($mainArray as $val){ echo $val . '<br />'; } foreach($subArray as $val){ echo $val . '<br />'; } Not sure if any of this helps.. but I would not bother with <<< Heredoc stuff.. surely, you just store the 'meat and potatoes' information into this array value and plug it into some code that echos out HTML tags but plugs in the values from arrays into appropriate places. Hope any of this helps.. Cheers, NRG Quote Link to comment https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-624841 Share on other sites More sharing options...
plexus Posted August 25, 2008 Author Share Posted August 25, 2008 Actually, what I'm trying to do is to include some template tags from a weblog publishing system into the PHP script. Maybe this explains why the script is written in this manner. What the template tag does is that it'll extract the contents from the database and display as text accordingly. What I'm trying to achieve is as follows. 1. To differentiate the data extracted from the tags and place them into arrays (I've data from 2 different blog_id, therefore $mainArray and $subArray) 2. Using $mainArray = array('type' => 'main' ...) and subsequently $subArray = array('type' => 'sub'...) to differentiate the contents extracted. I'm not so sure if this is the appropriate way to do it but I do not want the array 'type' & 'date' to be displayed. I intend to use it for logic (if...else) execution and only display array 'content' from both $mainArray and $subArray 3. The array 'date' from both $mainArray and $subArray should be used to format the data output such that it'll be displayed in order of date and time sequence. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-624877 Share on other sites More sharing options...
plexus Posted September 28, 2008 Author Share Posted September 28, 2008 hi all, any updates / suggestions to the above? Quote Link to comment https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-652285 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.