Add key to array

Keesss
Keesss used Ask the Experts™
on
array { [0]=>
     City
     Street
     Zip

I have this but what I need is something like:

    array { [0]=>
     'city'=>City
     'Street'=>Street
     'zip'=>Zip

I am using PHPDOM to get the html which I than add to an array

    $dom->loadHTML($html);
    foreach ($dom->getElementsByTagName('div') as $element) {
        if (strpos($element->getAttribute('class'), 'contentSection') !== false) {
            $string = $element->C14N();
        }
    }
   
    $array = explode("</p>", $string);
    print_r($array);
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
The simplest way - just loop through your array and create a new array with key and value the same.
$newarray = array();
foreach($array as $s) {
    $newarray[$s] = $s;
}

Open in new window

Author

Commented:
Hi Julian,

This is all very new to me so maybe you can add your code to mine and share it. Because I just get the same array this way. How do I add the keys.

Thanks
In the same way you want @Julian to add some code, we would also ask you to post a snippet of the HTML that you are processing.
<?php

$string = "cityName</p>streetName</p>zipCode</p>unknownValue</p>anotherUnknownValue";
$keys = array("city","street", "zip");

$array = array_explode_with_keys("</p>", $keys, $string);
print_r($array);

function array_explode_with_keys($delimiter, $keys, $string){
    $return = array();
    $pieces = explode($delimiter,$string);
    foreach($pieces as $i=>$piece){
        if($i<count($keys)) {
        	$return[$keys[$i]] = $piece;
        } else {
        	$return[$i] = $piece;
        }
    }
    return $return;
}

?>

Open in new window

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Author

Commented:
HTML part that I am processing with DOM

 
<div class="contentSection">

    City
    <br>
    Street
    <br>
    ZIP                

    <p class="Separator">
    </p>

    City
    <br>
    Street
    <br>
    ZIP 

Open in new window

Author

Commented:
I have tried to do this:
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('div') as $element) {
    if (strpos($element->getAttribute('class'), 'contentSection') !== false) {
        $string = $element->C14N();

      


    }
}



$keys = array("city","street", "zip");
$array = array_explode_with_keys("<br>", $keys, $string);
print_r($array);

function array_explode_with_keys($delimiter, $keys, $string){
    $return = array();
    $pieces = explode($delimiter,$string);
    foreach($pieces as $i=>$piece){
        if($i<count($keys)) {
        	$return[$keys[$i]] = $piece;
        } else {
        	$return[$i] = $piece;
        }
    }
    return $return;
}

?>

Open in new window


but this returns:

Array ( [city] =>
city[street] =>
street [zip] =>
zip

city [3] =>
street [4] =>
zip

So it goes wrong in the second part
Most Valuable Expert 2011
Top Expert 2016

Commented:
Hi, Keesss, and welcome to E-E.  If you're new to PHP and want to learn how to use the language, this article can help you find dependable learning resources.  Just skip over any of the the basic computer science stuff that you may already know.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

As with most data-dependent questions the quality of the solution you may get is going to be directly related to the quality of your test data.  We often use a thought-model called the SSCCE to express our questions and answers.  In this case the SSCCE would consist of a realistic web page document, warts and all, that we would use for input.  We would also need you to show us the expected output after the web page has been processed.  We never need to see your code - we can provide the code - we just need to see the before and after data sets, showing us what you have and what you need.

PHPDom or HTMLDom may not be the right tool for parsing web pages.  In my experience, these DOM processors are always more complicated and less capable than advertised.  I would probably lean in the direction of string manipulation, using the HTML document as the input string.  But in any case, as soon as you can show us your test data set (or a link to a web page you want to scrape) and the desired output, we can get you a good solution very quickly.

Best regards, ~Ray
Yeah... I didnt have sample of what your HTML data looks like at the time. Unfortunately am in meetings for the next couple of hours :(

Author

Commented:
Hi Ray,

Thanks for your comment I will check that out.
The data that I am scraping looks like this.
The div "contentSection" is what I need.
<div class="contentSection">

    City
    <br>
    Street
    <br>
    ZIP                

    <p class="Separator">
    </p>

    City
    <br>
    Street
    <br>
    ZIP 

 <p class="Separator">
    </p>

    City
    <br>
    Street
    <br>
    ZIP 

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
Sorry, this cannot be a very good example because the input test data is hypothetical.  When you try to parse HTML documents, the devil is in the details, and hypothetical examples usually overlook some of the important details.  Getting good test data is well more than half the work.  If you get us a more realistic data set, we can get you a more realistic code sample.

Please see: http://iconoun.com/demo/temp_keesss.php
<?php // demo/temp_keesss.php

/**
 * http://www.experts-exchange.com/questions/28714585/Add-key-to-array.html
 */
error_reporting(E_ALL);

// TEST DATA FROM THE POST AT E-E SHOWS A MOCKUP OF A FRAGMENT OF A WEB PAGE
$htm = <<<EOD
<div class="contentSection">

    City
    <br>
    Street
    <br>
    ZIP

    <p class="Separator">
    </p>

    City
    <br>
    Street
    <br>
    ZIP

 <p class="Separator">
    </p>

    City
    <br>
    Street
    <br>
    ZIP
EOD;

// ISOLATE THE USEFUL INFORMATION
$arr = explode('<div class="contentSection">', $htm);
$htm = $arr[1];
$htm = str_replace('</p>', NULL, $htm);
$arr = explode('<p class="Separator">', $htm);

// COLLECT THE OUTPUT ARRAY OF USEFUL INFORMATION
$out = [];

// ITERATE OVER THE USEFUL INFORMATION TO REFINE THE SELECTION
foreach ($arr as $str)
{
    $sub = [];
    $str = trim($str);
    $str = explode('<br>', $str);
    $sub['city']   = trim($str[0]);
    $sub['street'] = trim($str[1]);
    $sub['zip']    = trim($str[2]);
    $out[] = $sub;
}

// SHOW THE WORK PRODUCT
echo '<pre>';
print_r($out);

Open in new window

greetings Keesss, , the php DOM will return DOM element objects on many methods like -
    $domOBJ = $dom->getElementsByTagName('div');
This means that in your loop -
       foreach ($dom->getElementsByTagName('div') as $div) {
you can further parse out sub elements
$outArray = array();
    foreach ($dom->getElementsByTagName('div') as $div) {
      foreach ($div->getElementsByTagName('p') as $par){
         $html = $par->ownerDocument->saveHTML( $par );
         $aryBR = explode('<br>', $html);
         $outArray[] = array('c'=>$aryBR[0], 's'=>$aryBR[1], 'z'=>$aryBR[2])
         }
}

Open in new window


I have NOT USED or TESTED the code above, it is mostly just my thoughts on this, no time now for testing, but you or some experrt here may can hammer on this a bit and get a workin
sorry the dom text nodes you need are not in the <p> dom nodes, so my code above will not work
Most Valuable Expert 2011
Top Expert 2016

Commented:
It probably seems like a detail, but it causes hundreds of software failures every day... For anyone coming across this question and answer in the future, please be aware that PHP array keys are case-sensitive, so the accepted answer cannot work with the given test data.  A tested and working code example is shown here.  The output looks like this:
Array
(
    [0] => Array
        (
            [city] => City
            [street] => Street
            [zip] => ZIP
        )

    [1] => Array
        (
            [city] => City
            [street] => Street
            [zip] => ZIP
        )

    [2] => Array
        (
            [city] => City
            [street] => Street
            [zip] => ZIP
        )

)

Open in new window

Obviously the answers would have been better if the test data had been better!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial