BonifatiusK
BonifatiusK

Reputation: 2331

PHP: pushing new data (including new key) in associative array

So I am trying to push new data into an array. This data inlcudes new key values.

This is what I am using now, however this will always overwrite the last with the newer in stead of adding a row to the array:

foreach ($arr AS $lineNum => $line) {

    list($key, $value) = explode(':', $line);
    $newArray[$key] = $value;

}

return $newArray;

So what can I do to chop these up?

Upvotes: 0

Views: 260

Answers (1)

Sundar
Sundar

Reputation: 4650

<?php 
//try this it won't overwrite

$i = 0;

foreach ($arr AS $lineNum => $line) {

    $i = count($newArray);

    list($key, $value) = explode(':', $line);
    $newArray[$i] = $value;
    $i++;

}

return $newArray;

Upvotes: 1

Related Questions