2

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?

1
  • I think you should make your question with a better look, like this. Commented Jul 26, 2016 at 15:27

7 Answers 7

1

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.

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

Comments

1

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

1 Comment

Why should the OP try this? A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
0

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)

Comments

0

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

Comments

0

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

either do

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

or

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

Comments

0

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.

Comments

0

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

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.