0

Here is my PHP code:

<?PHP


$url = 'http://www.sportsdirect.com/dunlop-mens-canvas-low-top-trainers-246046?colcode=24604622';
libxml_use_internal_errors(true); 
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

    $DataVariants = $xpath->query('//span[@class="ImgButWrap"]/@data-variants')->item(0)->nodeValue;

    $jsonStart = strpos($DataVariants, '[');
    $jsonEnd = strrpos($DataVariants, ']');

    $collections = json_decode(substr($DataVariants, $jsonStart, $jsonEnd - $jsonStart + 1));   


    $result = array();

    foreach ($collections as $item) {
        $ColVarId = $item->ColVarId;


        $SizeNames = [];
        foreach ($item->SizeVariants as $size) {
            $SizeNames[] = $size->SizeName;
        }

        if (in_array("7", $SizeNames)) {
            $result[]['colorids'] = $ColVarId;              
        }

    }

    echo json_encode($result);  


?>

Echo prints this:

[{"colorids":"24604603"},{"colorids":"24604684"},{"colorids":"24604640"},{"colorids":"24604609"},{"colorids":"24604682"},{"colorids":"24604686"},{"colorids":"24604681"},{"colorids":"24604689"},{"colorids":"24604602"},{"colorids":"24604679"},{"colorids":"24604680"},{"colorids":"24604622"},{"colorids":"24604685"},{"colorids":"24604683"},{"colorids":"24604621"},{"colorids":"24604677"},{"colorids":"24604688"}]

The desired output format is

{"colorids": ["id1","id2","id3","id4","id5","id6","id7","id8"] }

In hours i can not understand where is my mistake. Can you help me out resolve this because i think the echo result is not correct one.

Thanks in advance!

5
  • In which format do you want the data? Commented Aug 23, 2015 at 18:43
  • Like {"colorids":"id1","id2","id3","id4","id5","id6","id7","id8"} Commented Aug 23, 2015 at 18:46
  • 1
    {"colorids":"id1","id2","id3","id4","id5","id6","id7","id8"} That's not valid JSON. Do you mean {"colorids": ["id1","id2","id3","id4","id5","id6","id7","id8"] }? Commented Aug 23, 2015 at 18:52
  • That's not valid JSON. Also you could just have edited your original question instead of reposting. Commented Aug 23, 2015 at 18:52
  • Yes @VolkerK that is what i want :) Commented Aug 23, 2015 at 18:55

3 Answers 3

2

your code creates an array of arrays

if (in_array("7", $SizeNames)) {
   $result[]['colorids'] = $ColVarId;              
}

to,

if (in_array("7", $SizeNames)) {
        $result['colorids'][] = $ColVarId;              
}

output

{"colorids":["id1","id2","id3","id4","id5","id6","id7","id8"]}
Sign up to request clarification or add additional context in comments.

Comments

1
$result[]['colorids']

From json_encode's point of view here you are adding a new object that has the property colorids to an array. But you want one object that has a property colorids that is an array of strings (more or less the other way round):

$result = array( 'colorids'=>array() );
...
$result['colorids'][] = $ColVarId;

Comments

1

Use this :

if (in_array("7", $SizeNames)) {
        $result['colorids'][] = $ColVarId;              
}

to get the such output

{"colorids":["id1","id2","id3","id4","id5","id6","id7","id8"]}

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.