我想将两个数组中的前6个元素组合起来。
我使用下面的代码组合数组。
$combined = array_combine($models,$prices);
这是$models数组
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
$modle = $node->nodeValue;
$modle1 = basename($modle);
$models[] = $modle1;
}
}
}
这是$prices数组
if (!is_null($price)) {
foreach ($price as $p) {
$nodes1 = $p->childNodes;
foreach ($nodes1 as $node1) {
$modlePrice = trim($node1->nodeValue,'Regular Price:');
$modlePrice1 = basename($modlePrice);
$prices[] = $modlePrice1;
}
}
}
这些阵列的输出为:-价格为295,000.00 LKR 395,000.00 LKR 290,000.00 LKR 150,000.00 LKR 68,000.00 LKR 45,000.00 LKR 31,000.00 LKR 340,000.00 LKR
=>三星40“UA40 ES6220系列6智能3D LED电视带4副眼镜三星55”F8000系列8智能3D全高清F8000电视三星46“F8000系列8智能3D全高清LED电视三星40”F6400系列6智能3D全高清LED电视等。
有人能帮我吗。
发布于 2013-10-04 06:50:59
在组合数组之前先对数组进行array_slice
。
发布于 2013-10-04 06:46:04
尝试下面的代码,让我知道如果有任何问题的..waiting为您重播.
<?php
// Your Price Result
$prices = array('LKR 295,000.00','LKR 395,000.00',' LKR 290,000.00',' LKR 150,000.00',' LKR 68,000.00',' LKR 45,000.00',' LKR 31,000.00 ','LKR 340,000.00');
// Your Modles Results
$models = array('Samsung 40" UA40 ES6220 Series 6 SMART 3D LED TV with 4 glasses',' Samsung 55" F8000 Series 8 Smart 3D Full HD LED TV ','Samsung 46" F8000 Series 8 Smart 3D Full HD LED TV',' Samsung 40" F6400 Series 6 Smart 3D Full HD LED TV',' Samsung 40" F6400 Series 6 Smart 3D Full HD LED TV' ,' Samsung 48" F6400 Series 4 Smart 3D LED TV' ,' Samsung 48" F6400 Series 6 Smart 2D HD LED TV' ,' Samsung 42" F6400 Series 6 Smart 4D Full HD LED TV' );
if(sizeof($prices) == sizeof($models)){
$combined = array_combine($models,$prices);
echo "<pre>";
print_r($combined);
echo "</pre>";
exit;
}else{
echo "Length of array are not same";
}
?>
https://stackoverflow.com/questions/19174586
复制相似问题