I just need to display one line from fruits.
$fruits = 'Apple, Banana, Lemon, Strawberry';
$rows = explode(', ' ,$fruits);
foreach($rows as $row => $data){
$row_data = explode('^', $data);
$info[$row]['id']= $row_data[0];
Apple
Banana
Lemon
Strawberry
Your $row_data = explode('^', $data);
doesn't appear to be doing what you think it will. Try something like this:
$info = [];
$fruits = explode(', ' ,'Apple, Banana, Lemon, Strawberry');
foreach($fruits as $index => $fruit){
$info[$index]['id'] = $fruit;
}
or even:
$info = [];
$fruits = ['Apple', 'Banana', 'Lemon', 'Strawberry'];
foreach($fruits as $index => $fruit){
$info[$index]['id'] = $fruit;
}
From your code, foreach($rows as $row => $data)
, $row
stores the index of each array element and $data
contains its data. If you want to pick using index, you can try looking the data in as $rows[0]
or $rows[1]
and so on. In your code above, you don't really need a foreach, you can grab the results as shown below
$fruits = 'Apple, Banana, Lemon, Strawberry';
$rows = explode(', ' ,$fruits);
echo($rows[0]); // outputs Apple
echo($rows[1); // outputs Banana
echo($rows[2]); // outputs Lemon
echo($rows[3]); // outputs Strawberry