0
for ($k = 0; $k < $count; $k++) {
    $master[$k] = $namearray[$k], $streetarray[$k], $localityarray[$k], $regionarray[$k], $postalcodearray[$k], $phonearray[$k];
}

I'd like to declare a new array and set values from other arrays already declared. I thought I could just loop through the keys and set the values but this doesn't work for me.

Full code below. I'm parsing yellow pages search results and trying to output search results into a csv file. In the code below I removed the loop and only added a few values to the array to make sure my bug wasn't something else.

<?php

            // include required functions
            include('simple_html_dom.php');

            $url = "http://www.yellowpages.com/" . $_POST['city'] . '-' . $_POST['state'] . '-' . $_POST['postalcode'] . '/' . $_POST['category'] . '?g=' . $_POST['city'] . '%2C+' . $_POST['state'] . '+' . $_POST['postalcode'] . '&q=' . $_POST['category'];

            // get DOM from URL
            $html = file_get_html($url);


            // find all business name
            foreach($html->find('h3.business-name') as $name)
                //echo $name->innertext . '<br />';
                $namearray[] = $name->innertext;

            // find all business street address
            foreach($html->find('span.street-address') as $street) 
                //echo $street->innertext . '<br />';
                $streetarray[] = $street->innertext;

            // find all business city
            foreach($html->find('span.locality') as $locality)
                //echo $locality->innertext . '<br />';
                $localityarray[] = $locality->innertext;            

            // find all business state
            foreach($html->find('span.region') as $region)
                //echo $region->innertext . '<br />';
                $regionarray[] = $region->innertext;            

            // find all business postal code
            foreach($html->find('span.postal-code') as $postalcode)
                //echo $postalcode->innertext . '<br />';
                $postalcodearray[] = $postalcode->innertext;                

            // find all business phone
            foreach($html->find('span.business-phone') as $phone)
                //echo $phone->innertext . '<br />';
                $phonearray[] = $phone->innertext;

        ?>
        <p>Search results for: <?php echo $_POST['category'] . ' ' . $_POST['city'] . ' ' . $_POST['state'] . ' ' . $_POST['postalcode'];  ?></p>
        <?php
            // Output results
            $count = count($namearray);
            for ($i = 0; $i < $count; $i++) {
                echo $namearray[$i] . '<br />';
                echo $streetarray[$i] . '<br />';
                echo $localityarray[$i] . ',' . $regionarray[$i] . ' ' . $postalcodearray[$i] . '<br />';
                echo $phonearray[$i] . '<br />' . '<br />';
            }


        $list = array (
            array($namearray[0], $streetarray[0], $localityarray[0], $regionarray[0], $postalcodearray[0], $phonearray[0]),
            array($namearray[1], $streetarray[1], $localityarray[1], $regionarray[1], $postalcodearray[1], $phonearray[1]),
            array($namearray[2], $streetarray[2], $localityarray[2], $regionarray[2], $postalcodearray[2], $phonearray[2]),
            array($namearray[3], $streetarray[3], $localityarray[3], $regionarray[3], $postalcodearray[3], $phonearray[3])
        );

        $fp = fopen('hrpsearch.csv', 'w');

        foreach ($list as $fields) {
            fputcsv($fp, $fields);
        }

        fclose($fp);

        ?>
4
  • 2
    Please provide an example of the array you want to create and the arrays you currently have. If we don't know what you want to achieve, it is difficult for us to help you. "set values from other arrays already declared" is not specific enough, it does not convey any information about the final result. Commented Aug 9, 2012 at 14:56
  • $master[$k] = array($namearray[$k], $streetarray[$k], ...); ? (Note your missing array() declaration) Commented Aug 9, 2012 at 14:57
  • Ok, so you provided your full code, but you still did not tell use what result you want to get. Do you want to create $list dynamically? Commented Aug 9, 2012 at 15:05
  • Yes, I want $list to be created dynamically. Commented Aug 9, 2012 at 15:09

5 Answers 5

6

Try:

$master = array();
for ($k = 0; $k < $count; $k++) {
    $master[$k] = array
        ( $namearray[$k]
        , $streetarray[$k]
        , $localityarray[$k]
        , $regionarray[$k]
        , $postalcodearray[$k]
        , $phonearray[$k]
        );
}

This will create a new two-dimensional array for you with associated keys for every child array.

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

1 Comment

Thanks pal. This worked for me. haha just realized I changed my array naming on you guys with $master and $list. Haha... First post on here. I'll do better next time. Thanks for the help everyone.
0

Try

for($k = 0; $k < $count; $k++) {
    $master[$k] = array(
        $namearray[$k], 
        $streetarray[$k], 
        $localityarray[$k], 
        $regionarray[$k], 
        $postalcodearray[$k], 
        $phonearray[$k]
    );
}

Comments

0

Maybe do you just want to achieve this?

$master[$k] = array($namearray[$k], $streetarray[$k], $localityarray[$k], $regionarray[$k], $postalcodearray[$k], $phonearray[$k]);

I would suggest using instead:

$master[$k]['name'] = $namearray[$k];
$master[$k]['street'] = $streetarray[$k];
...

The retrieval of the data will be more readable.

Comments

0

I think @DaveRandom's answer is what (I imply) you are looking for.

Since a PHP array can be of any type (scalar, array, object, etc.), you need to tell it you are assigning an array with the construct array().

The end result would be:

$master[$k] = array($namearray[$k], $streetarray[$k], $localityarray[$k], $regionarray[$k], $postalcodearray[$k], $phonearray[$k]);

Comments

0

Try this

for ($k = 0; $k < $count; $k++) {
    $master[$k] = array($namearray[$k], $streetarray[$k], $localityarray[$k], $regionarray[$k], $postalcodearray[$k], $phonearray[$k]);
}

or is better to create associative array

for ($k = 0; $k < $count; $k++) {
    $master[$k] = array('name'=>$namearray[$k], 
                        'street'=>$streetarray[$k], 
                        'city'=>$localityarray[$k], 
                        'region'=>$regionarray[$k], 
                        'postalCode'=>$postalcodearray[$k], 
                        'phone'=>$phonearray[$k]);
}

You also need to check if your array elements are not empty or just put @ befor array element like 'name'=>@$namearray[$k]. It will remove any warning if element doesn't exist.

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.