2

Is it possible to insert this kind of data from database into an array?

Mouse
Keyboard  //Saved from a Textarea box 
Monitor

The whole table looks like this;

id  ||   Owner  ||  Items
--------------------------
1   ||   John   || Mouse
    ||          || Keyboard //This is in a single row
    ||          || Monitor
--------------------------

How to assign each line of the data from field "Items" into an array (let's say $items)?

So that it will be like:

  $items[0] = "Mouse";
  $items[1] = "Keyboard";
  $items[2] = "Monitor";
2
  • So you want it to appear as though John is the owner of the mouse, keyboard, and monitor, or that they keyboard and mouse have no owner? or just have a list of items? Commented May 23, 2012 at 4:48
  • All those three items are Johns but nevermind. I got my answers already. Thanks to Vishal Commented May 23, 2012 at 5:00

4 Answers 4

4

Try:

$items = explode("\n", $string_from_db);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use mysql_fetch_assoc()

Or PDOStatement::fetchAll()

Or mysqli_result::fetch_assoc()

2 Comments

Sorry, I think you misunderstand my question.
@HafizAbdullah - I think we are all misunderstanding your question. What do you want the final PHP array to look like?
0

something like this?

$arr = explode("\n",$data);   // split into lines
$categories = explode("||",$arr[0]);
unset($arr[0]); array_values($arr[0]);  // dont want categories anymore
array_walk($categories,create_function('&$v','$v = trim($v);'));   // trim space from categories
$data = array();   // holds info
foreach((array)$arr as $key=>$val) {
   if (substr($val,0,5)=="-----") { continue; }   // ignore "-----..." lines
   $row = explode("||",$val);
   foreach((array)$row as $key2=>$val2) {
      $data[ $categories[$key2] ][] = trim($val2);
   }
}

echo "<pre>";
print_r($data);
echo "</pre>";

your example with the above code gives:

Array
(
    [id] => Array
        (
            [0] => 1   
            [1] =>     
            [2] =>     
        )

    [Owner] => Array
        (
            [0] =>    John   
            [1] =>           
            [2] =>           
        )

    [Items] => Array
        (
            [0] =>  Mouse
            [1] =>  Keyboard
            [2] =>  Monitor
        )

)

1 Comment

Thanks for your answer but why are you include the lines in your code? The table that I show you is the table in the mysql database. It has nothing to do with my problem.Sorry if you misunderstand.
0

If I'm reading the last line correctly, you want:

  $results = $db -> query("SELECT Items FROM table");

  while($row = $results -> fetch_assoc()) {
        $items[] = $row['Items'];
  }

1 Comment

If you are wanting the entire table as an array, you'll need to normalize your data to make it clear the ownership and id of each item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.