Phil
Phil

Reputation: 11175

adding a value to an associative array with a foreach?

Solution Found and voted on


Here is my code:

//go through each question
foreach($file_data as $value) {
    //separate the string by pipes and place in variables
    list($title, $content, $date_posted) = explode('|', $value);

    //create an associative array for each input
    $file_data_array['title'] = $title;
    $file_data_array['content'] = $content;
    $file_data_array['date_posted'] = $date_posted;

}

What happens is that the assoc values keep getting erased. Is there a way I can have the value append to array? If not, how else could I do this?

Upvotes: 8

Views: 17670

Answers (4)

Naftali
Naftali

Reputation: 146310

try this:

$file_data_array = array(
     'title'=>array(),
     'content'=>array(),
     'date_posted'=>array()
);
//go through each question
foreach($file_data as $value) {
    //separate the string by pipes and place in variables
    list($title, $content, $date_posted) = explode('|', $value);

    //create an associative array for each input
    $file_data_array['title'][] = $title;
    $file_data_array['content'][] = $content;
    $file_data_array['date_posted'][] = $date_posted;

}

you final array would look something like:

$file_data_array = array(
   'title' => array ( 't1', 't2' ),
   'content' => array ( 'c1', 'c2' ),
   'date_posted' => array ( 'dp1', 'dp2' )
)

here is a demo of it:

http://codepad.org/jdFabrzE

Upvotes: 0

bpierre
bpierre

Reputation: 11457

Do you want to append associative arrays to $file_data_array?

If so:

//go through each question
foreach($file_data as $value) {
    //separate the string by pipes and place in variables
    list($title, $content, $date_posted) = explode('|', $value);

    //create an associative array for each input
    $file_data_array[] = array(
        "title" => $title,
        "content" => $content,
        "date_posted" => $date_posted,
    );

}

Upvotes: 2

Pascal MARTIN
Pascal MARTIN

Reputation: 401022

You could append to the $file_data_array array using something like this :

foreach($file_data as $value) {
    list($title, $content, $date_posted) = explode('|', $value);
    $item = array(
        'title' => $title, 
        'content' => $content, 
        'date_posted' => $date_posted
    );
    $file_data_array[] = $item;
}

(The temporary $item variable could be avoided, doing the declaration of the array and the affectation at the end of $file_data_array at the same time)


For more informations, take a look at the following section of the manual : Creating/modifying with square bracket syntax

Upvotes: 9

Ryre
Ryre

Reputation: 6181

You need an extra key.

//go through each question
$x=0;
foreach($file_data as $value) {
    //separate the string by pipes and place in variables
    list($title, $content, $date_posted) = explode('|', $value);

    //create an associative array for each input
    $file_data_array[$x]['title'] = $title;
    $file_data_array[$x]['content'] = $content;
    $file_data_array[$x]['date_posted'] = $date_posted;
    $x++;
}    

Upvotes: 0

Related Questions