首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用两个数组组合前6个元素

如何使用两个数组组合前6个元素
EN

Stack Overflow用户
提问于 2013-10-04 06:19:09
回答 2查看 76关注 0票数 0

我想将两个数组中的前6个元素组合起来。

我使用下面的代码组合数组。

代码语言:javascript
运行
复制
$combined = array_combine($models,$prices);

这是$models数组

代码语言:javascript
运行
复制
if (!is_null($elements)) {
    foreach ($elements as $element) {
        $nodes = $element->childNodes;
        foreach ($nodes as $node) {

            $modle = $node->nodeValue;
            $modle1 = basename($modle); 
            $models[] = $modle1;

        }
    }
}

这是$prices数组

代码语言:javascript
运行
复制
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电视等。

有人能帮我吗。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-04 06:50:59

在组合数组之前先对数组进行array_slice

票数 0
EN

Stack Overflow用户

发布于 2013-10-04 06:46:04

尝试下面的代码,让我知道如果有任何问题的..waiting为您重播.

代码语言:javascript
运行
复制
<?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";
 }

?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19174586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档