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);
KeesssAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KeesssAuthor 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
0
MlandaTCommented:
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

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

KeesssAuthor 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

0
KeesssAuthor 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
0
Ray PaseurCommented:
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
0
MlandaTCommented:
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 :(
0
KeesssAuthor 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

0
Ray PaseurCommented:
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

0
Slick812Commented:
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
0
Slick812Commented:
sorry the dom text nodes you need are not in the <p> dom nodes, so my code above will not work
0
Ray PaseurCommented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.