Jump to content

Recommended Posts

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)?

Link to comment
https://forums.phpfreaks.com/topic/120982-associative-array/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-623668
Share on other sites

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
);

Link to comment
https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-624086
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-624131
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-624841
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/120982-associative-array/#findComment-624877
Share on other sites

  • 1 month later...
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.