Damian Dominella
Damian Dominella

Reputation: 620

php - Explode array into a key->value array

I have the following string:

Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone_break_

I got this from a cURL of an HTML page using DOM.

My final result must be a JSON file like:

Shop 1: { name: "example", location: "example", telephone: "0123"}
Shop 2: { name: "example", location: "example", telephone: "0123"}

But I know that first I have to split the string, I tried this

$shops = explode("break",$result);
$values = array();

foreach ($shops as $shop) {
    $values = explode("_", $shop);
    foreach($values as $value) {
        $name = $value[0];
        $location = $value[1];
        $tel = $value[2];
    }
}

But it doesn't work. Can anybody help me?

Upvotes: 2

Views: 3614

Answers (7)

LF-DevJourney
LF-DevJourney

Reputation: 28529

After first explode you need to trim the '_' at the last of string in the $shops array.

<?php
    $result = "Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone";
    $shops = explode("break",$result);
    echo json_encode($shops)."\n";
    $values = array();
    foreach ($shops as $shop){
        $values = explode("_", trim($shop, '_'));
        echo json_encode($values)."\n";
        $name = $values[0];
        $location = $values[1];
        $tel = $values[2];
        echo $name."\n";
    }
?>

and the result is,

["Name_Location_Telephone_","_Name_Location_Telephone_","_Name_Location_Telephone_","_Name_Location_Telephone"]
["Name","Location","Telephone"]
Name
["Name","Location","Telephone"]
Name
["Name","Location","Telephone"]
Name
["Name","Location","Telephone"]
Name

Upvotes: 0

Goku
Goku

Reputation: 2139

1) Your first explode keeps some trimming underscore.

2) You have to use json_encode method to transform your array to json data

A example that you can do

$result = "Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone_break_";
$shops = explode("_break_",$result);

foreach($shops as $key => $shop) {
    $res["Shop ".$key] = explode('_', $shop);
}

$jsonData = json_encode($res);

Upvotes: 1

Gino Pane
Gino Pane

Reputation: 5011

Try this:

$result = "Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone_break_Name_Location_Telephone";

$shops = explode("_break_",$result);
$results = array();
$index = 1;

foreach ($shops as $shop) {
    $values = explode("_", $shop);

    $results["Shop $index"] = [
        'name' => $values[0],
        'location' => $values[1],
        'telephone' => $values[2],
    ];

    $index++;
}

print_r($results);

The result is:

Array
(
    [Shop 1] => Array
        (
            [name] => Name
            [location] => Location
            [telephone] => Telephone
        )

    [Shop 2] => Array
        (
            [name] => Name
            [location] => Location
            [telephone] => Telephone
        )

    [Shop 3] => Array
        (
            [name] => Name
            [location] => Location
            [telephone] => Telephone
        )

    [Shop 4] => Array
        (
            [name] => Name
            [location] => Location
            [telephone] => Telephone
        )

)

You had spare "_" after first explode by only break, that's why the result may became wrong.

Upvotes: 0

maalls
maalls

Reputation: 759

It doesn't work because your first explode keeps some trimming underscore.

either do

$shops = explode("_break_",$result);

or

$shops = trim(explode("break",$result), "_");

Upvotes: 0

Basit Munir
Basit Munir

Reputation: 438

you are almost close to your required result. try following

 $shops = explode("_break_",$result);
 $jsonArray = array();
 foreach ($shops as $shop){
     // three elements are same in response no need to worry just explode in array of three elements.
  $value = explode("_", $shop); 
   // assign prepared array or print it according to your business logic.
   $jsonArray[] = array ('name'=>$value[0], 'location'=>$value[1], 'telephone'=>$value[2]); 
 }
 //here you can encode your final multi dimensional array to json string.
echo json_encode($jsonArray);

Upvotes: 0

Marc B
Marc B

Reputation: 360702

You're exploding on the wrong value:

Name_Location_Telephone_break_Name_Location_Telephone etc...

will become an array:

0 => 'Name_Location_Telephone_'
1 => '_Name_Location_Telephone_'
2 => '_Name_Location_Telephone_'
etc...

when you explode these, the first one will have Name at index 0, but then at index 1 for all subsequent explodes.

You should be exploding on _break_ instead.

If you'd done any basic debugging, like var_dump($shops) and var_dump($values), you'd have seen how things shift around.

Upvotes: 1

pegas
pegas

Reputation: 111

That's not exactly how you write the foreach. It differs from Java. Checkout : http://php.net/manual/en/control-structures.foreach.php for examples, and don't hesitate to do so from now on (thei PHP API is very good)

Upvotes: 0

Related Questions