For one, the second iteration in the loop is overriding the values of your array "$values" which is why you'd only be seeing the values from the second product node (if you are inspecting the "$values" array which is what I'm assuming you are doing).
Try this:
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$xpath = new DOMXPath($xmlDoc);
$x = $xmlDoc->getElementsByTagName("product");
$array = array('id','pn','stock','item_number');
$values = array();
foreach ($x as $product) {
$data = array();
// node name here
$data['node'] = $product->nodeName;
foreach ($array as $v){
$obj = $xpath->query($v, $product)->item(0);
if (gettype($obj) == 'object'){
$data[$v] = $obj->textContent;
}
}
$values[] = $data;
}
echo '<pre>' . print_r($values, true). '</pre>';
That should produce this:
Array
(
[0] => Array
(
[node] => product
[id] => 1336
[pn] => NX.RZNAA.003
[stock] => 1.00
)
[1] => Array
(
[node] => product
[id] => 1337
[pn] => NX.RZNAA.004
[stock] => 4.00
[item_number] => 5
)