1

I am using simplehtmldom to parse a webpage and extract a data and then put it in a mysql database. I successfully extracted the data and put it in array but now the problem i am facing is how to convert this array to variable so that I may use it in my database query to insert the record in the specific fields. I want to convert array to individual variables

here is the code

<?php
     include("simple_html_dom.php");

     $html = file_get_html('abc.html'); 

     $sched = array();
     foreach ( $html->find('tr[class=highlight-darkgrey]') as $event ) {
         $item['title'] = trim($event->find('td', 1)->plaintext);

         $sched[] = $item;
    }

    var_dump($sched);
?>

and the output is

array (size=6)
 0 => 
 array (size=1)
  'title' => string 'Network admin, field engineer, engineering analyst, sales  executive, PA to HR director Required by Telecommunication Company' (length=124)
 1 => 
 array (size=1)
  'title' => string 'Karachi, Hydrabad, Lahore, Rawalpindi, Peshawar, Multan, Faisalabad' (length=67)
 2 => 
 array (size=1)
  'title' => string '5 - 6 Years' (length=11)
 3 => 
 array (size=1)
  'title' => string 'Knowledge of Field' (length=18)
 4 => 
 array (size=1)
  'title' => string '' (length=0)
 5 => 
 array (size=1)
  'title' => string '- Salary and incentives are not full and final. Can be discussed on final interview.

Can please somebody help me in achieving it. Thanks in advance

1
  • What you actually have is an array of arrays. I don't understand what your issue is, as you clearly already know how to use a foreach construct. Perhaps you can show some code that you have tried that is not doing what you want. Commented Jul 17, 2013 at 15:11

5 Answers 5

1

Well, if this needs to go in specific fields then you can do something like this:

INSERT INTO table_name (column1, column2, column3,...)
VALUES ($sched[0]['title'], $sched[1]['title'], $sched[2]['title'],...);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Sumoanand thats what i am looking for.
0

Php has a function for getting individual variables, extract() http://php.net/manual/en/function.extract.php

However I don't know why you would do that instead of just using a loop to go though your array.

2 Comments

"However I don't know why you would do that instead of just using a loop to go though your array." – because it's much simpler and quicker?
@TerryHarvey all depends on the database structure. If each title is going into it's own array why would you want to write out a bunch of different queries as compared to using a loop to do it for you?
0

You mean somethign like

foreach($sched as $item) {
   $title = $item['title'];
   insert_into_db($title);
}

?

Comments

0

Why do you want to move data from one perfectly good location i.e. the array into a scalar variable.

Double your memory usage for no actual reason.

I assume you are going to want to store the title's in a table

So you can :

foreach ( $sched as $one_sched ) {

    // do your database update using $one_sched['title'] as the data identifier.

}

Comments

0

Why not use the array values directly? Also to me it makes no sense to build a subarray where every field is called title.

$sched = array();
foreach ( $html->find('tr[class=highlight-darkgrey]') as $event ) {
    $sched[] = trim($event->find('td', 1)->plaintext);
}

Then access the values like:

$value0 = $sched[0];
$value1 = $sched[1];

// PDO example
$sth = $dbh->prepare(
    'INSERT INTO table VALUES (?, ?, ...)'
);
$sth->execute(array($sched[0], $sched[1], ...));
// or if array count is right
$sth->execute($sched);

Comments

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.