php - Create an array within an array using a string from key -
i can't past making think 2d array in php. i'm using csv source input , want take 1 of columns , split array well.
$csv = array_map("str_getcsv", file("data/data.csv", file_skip_empty_lines)); $keys = array_shift($csv); foreach ($csv $i => $row) { $csv[$i] = array_combine($keys, $row); $linkto = $csv[$i]['linkto']; // $linktoarray = explode(" ", $linkto); echo '<pre>'; // print_r($linktoarray); echo '</pre>'; $csv[$i] = array_combine($keys, $row); } $csv['timestamp'] = time(); echo '<pre>'; print_r($csv); echo '</pre>';
will output:
array ( [0] => array ( [color] => 1 [shape] => 0 [label] => string [size] => 1 [linkto] => 1 2 3 )...
using similar commented out, i'd love see like:
array ( [0] => array ( [color] => 1 [shape] => 0 [label] => string [size] => 1 [linkto] => array ( [0]=>1 [1]=>2 [2]=>3 ) )...
however, right i'm getting array before containing array. pardon ignorance, haven't had experience past front-end. suggestions? i'm sure has been explained before, can't find right terminology utilize search.
it's straight forward. need this:
$linkto = $csv[$i]['linkto']; $linktoarray = explode(" ", $linkto); $csv[$i]['linkto'] = $linktoarray;
after having read through code again, seem struggling concept of foreach. when use foreach don't access $csv[$i]
have, use $row
. try like:
//the & symbol means changes made $row inside foreach apply outside foreach. foreach($csv $i => &$row) { $row['linkto'] = explode(" ", $row['linkto']); }
that should need, none of array_combine
stuff.
Comments
Post a Comment