ROM
ROM

Reputation: 265

Converting array to individual variables

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

Upvotes: 1

Views: 1724

Answers (5)

bitWorking
bitWorking

Reputation: 12655

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);

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94662

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.

}

Upvotes: 0

Marc B
Marc B

Reputation: 360672

You mean somethign like

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

?

Upvotes: 0

Sumoanand
Sumoanand

Reputation: 8929

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'],...);

Upvotes: 1

Pitchinnate
Pitchinnate

Reputation: 7556

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.

Upvotes: 0

Related Questions