Avatar of Keesss
Keesss
 asked on

Add key to array

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

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Keesss

ASKER
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
Mlanda T

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

Keesss

ASKER
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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Keesss

ASKER
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
Ray Paseur

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.
https://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
Mlanda T

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 :(
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Keesss

ASKER
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

Ray Paseur

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

Member_2_248744

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Member_2_248744

sorry the dom text nodes you need are not in the <p> dom nodes, so my code above will not work
Ray Paseur

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!