1

I have a functon that is passed an array of url's. I am extracting data from each webpage and then assigning each piece of data to an array. Here's my function:

 function getitems ($urls) {
  $iteminfo = array();
  foreach($urls as $link) {
   $circdl = my_curl($link);
   $circqp = htmlqp($circdl,'body');
   $itemtitle = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('title');
   $itemlink = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('src');
   $itemdesc = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('alt');
   $iteminfo[][] = $itemtitle;
   //$iteminfo[$itemtitle][] = $itemlink;
   //$iteminfo[$itemtitle][] = $itemdesc;
  }
    return $iteminfo;  
 }

I want the array to look like this:

 Array ( [0] => Array ( [0] => title [1] => link [2] => desc ) [1] => Array ( [0] => title [1] => link [2] => desc ) [2] => Array ( [0] => title [1] => link [2] => desc ) ) 

But I can't wrap my head around how to additional fields to the sub-arrays.

4 Answers 4

2

try something like this

function getitems ($urls) {
    $iteminfo = array();
    $i = 0;
    foreach($urls as $link) {
        $circdl = my_curl($link);
        $circqp = htmlqp($circdl,'body');
        $itemtitle = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('title');
        $itemlink = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('src');
        $itemdesc = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('alt');
        $iteminfo[$i][] = $itemtitle;
        $iteminfo[$i][] = $itemlink;
        $iteminfo[$i][] = $itemdesc;
        $i++;
    }
    return $iteminfo;  
}

Everything is ok, you just have to assign index to each of your rows.

Sign up to request clarification or add additional context in comments.

1 Comment

There are better ways to do this task than maintaining a counter variable. See other answers.
0

If i understand you correctly...

$iteminfo[] = array($itemtitle, $itemlink, $itemdesc);

Comments

0
function getitems ($urls) {
   $iteminfo = array();
   foreach($urls as $link) {
       $subInfo = array();
       $circdl = my_curl($link);
       $circqp = htmlqp($circdl,'body');
       $subInfo[] = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('title');
       $subInfo[] = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('src');
       $subInfo[] = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('alt');
      $iteminfo[] = $subInfo;
   }
   return $iteminfo;  
}

Comments

0

You could easily replace

$iteminfo[][] = $itemtitle;
//$iteminfo[$itemtitle][] = $itemlink;
//$iteminfo[$itemtitle][] = $itemdesc;

with

$iteminfo = array($itemtitle, $itemlink, $itemdesc);

You can do this because the syntax

$array = $element; // where     $array = array();

is just another way to add element to an array in PHP and $element can be an array() as well.

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.