0

hi there im pretty new at php ... just working on a homework and we have this task to separate an array that has contents.. but the trick is to separate the contents and put it in a new array with the contents organized.

However, my new array is wrong. One index should contain all names into 1 string another index with all the phone numbers ...etc

mine displays like the one at picture

Any suggestions? the pic of code is also attached

so this is the new array

this is the code

<pre>
<?php
$fileName = "c:/wamp/www/datebook";

$line = file($fileName);

print_r($line);

foreach($line as $value)
{
    $newLine[] = explode(":",$value);

}

print_r($newLine);
?>
</pre>

these is little piece, they are 26 in total.. thats from notepad

Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
javaMan1995
  • 51
  • 1
  • 1
  • 12

3 Answers3

1

You can try this -

// The indexes to be set to new array [Currently I am assuming, You can change accordingly]
$indexes= array(
    'Name' , 'Phone', 'Address', 'Date', 'Value'
);

$new = array();
// Loop through the indexes array
foreach($indexes as $key => $index) {
    // extract column data & implode them with [,]
    $new[$index] = implode(', ', array_column($newline, $key));
}

array_column is supported PHP >= 5.5

Example

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
1
    <?php
    $fileName = "c:/wamp/www/datebook";

    $line = file($fileName);

    $newLine= array();
    foreach($line as $va)
    {   
        $new = explode(":",$va);
        $newLine['name'][] = $new[0];
        $newLine['phone'][] = $new[1];
        $newLine['etc'][] = $new[2];
    }
    echo "<pre>";
    print_r($newLine);
    ?>

This will Output

Array
(
    [name] => Array
        (
            [0] => Jon DeLoach
            [1] => Joo Del
        )

    [phone] => Array
        (
            [0] => 408-253-3122
            [1] => 408-253-3122
        )

    [etc] => Array
        (
            [0] => 7/25/53
            [1] => 7/25/53
        )

)
Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20
0

You need to add them to their own arrays.

$line = explode("\n", $s);

$newLine = array('name' => '','phone' => ''); // add the rest of the columns.....address,etc
foreach($line as $value)
{
    list($name,$phone,$address,$date,$postcode) = explode(":",$value);

    $newLine['name'] .= (empty($newLine['name'])? $name : " ". $name);
    $newLine['phone'] .= (empty($newLine['phone'])? $phone : " ". $phone);
    // etc
}

And that'll add them appropriately.

Example (just press ctrl + enter to run it)

And it returns an associative array that looks like this:

Array
(
    [0] => Array
        (
            [name] => Jon DeLoach
            [phone] => 408-253-3122
            [address] => 123 Park St., San Jose, CA 04086
        )

    [1] => Array
        (
            [name] => Sir Lancelot
            [phone] => 837-835-8257
            [address] => 474 Camelot Boulevard, Bath, WY 28356
        )

    [2] => Array
        (
            [name] => Jesse Neal
            [phone] => 408-233-8971
            [address] => 45 Rose Terrace, San Francisco, CA 92303
        )

)
Darren
  • 13,050
  • 4
  • 41
  • 79
  • But my goal is to have a single array, with each index containing particular data... [names] => ........... contains all names as 1 string with spaces as delim then [phone] => .........all phones separated with spaces – javaMan1995 Mar 03 '16 at 05:35
  • @javaMan1995 ahhhhhhhh right. I'll update this answer shortly – Darren Mar 03 '16 at 05:36