I'm trying to convert this text file:
mammals|A living thing that gives birth to their young.
mammals|A living thing that produce milk for their young.
mammals|A living thing that can grow hair or fur.
warm-blooded|The ability to produce own body heat
lungs|Mammals use this to breathe.
lungs|Mammals breathe with __________.
lungs|__________ helps mammals to breathe.
birth|Mammals give __________ to their young.
milk|Mammals produce this to feed their young.
hair|Some mammals, especially human, grow __________ on their skin.
fur|Some mammals, such as bears, grow __________ on their skin.
into PHP array as:
array(
mammals=>A living thing that gives birth to their young.
mammals=>A living thing that produce milk for their young.
mammals=>A living thing that can grow hair or fur.
warm-blooded=>The ability to produce own body heat
lungs=>Mammals use this to breathe.
lungs=>Mammals breathe with __________.
lungs=>__________ helps mammals to breathe.
birth=>Mammals give __________ to their young.
milk=>Mammals produce this to feed their young.
hair=>Some mammals, especially human, grow __________ on their skin.
fur=>Some mammals, such as bears, grow __________ on their skin.
)
I tried:
$array_mammals = explode("\n", file_get_contents('cw_mammals.txt'));
foreach ($array_mammals as $key => $value) {
$list_mammals[] = explode('|', $value);
}
print_r($list_mammals);
i got:
Array (
[0] => Array ( [0] => mammals [1] => A living thing that gives nirth to their young. )
[1] => Array ( [0] => mammals [1] => A living thing that produce milk for their young. )
[2] => Array ( [0] => mammals [1] => A living thing that can grow hair or fur. )
[3] => Array ( [0] => warm-blooded [1] => The ability to produce own body heat )
[4] => Array ( [0] => lungs [1] => Mammals use this to breathe. )
[5] => Array ( [0] => lungs [1] => Mammals beathe with __________. )
[6] => Array ( [0] => lungs [1] => __________ helps mammals to breathe. )
[7] => Array ( [0] => birth [1] => Mammals give __________ to their young. )
[8] => Array ( [0] => milk [1] => Mammals produce this to feed their young. )
[9] => Array ( [0] => hair [1] => Some mammals, especially human, grow __________ on their skin. )
[10] => Array ( [0] => fur [1] => Some mammals, such as bears, grow __________ on their skin. ) )
I need the keys to the first word of every line but I can't seem to get the approach right. Any pointers is appreciated.
You cannot have an array like you desire as @esqew mentioned. An array should always have unique keys. But you can try this in your existing problem, if this helps.
<?php
$array_mammals = explode("\n", file_get_contents('cw_mammals.txt'));
foreach ($array_mammals as $key => $value) {
$list_mammals[] = explode('|', $value);
}
foreach ($list_mammals as $list) {
echo $list[0]. "-". $list[1];
}
?>
As you can not have same key in an array but you can make an array of that particular key to keep the same type of values for your problem.
$array_mammals = explode("\n", file_get_contents('cw_mammals.txt'));
$mammals = [];
foreach ($array_mammals as $value) {
$key_vallue = explode('|', $value);
$mammals[ $key_vallue[0] ] [] = $key_vallue[1];
}
var_dump($mammals);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.