将两个数组与索引号组合

时间:2016-10-09 04:49:26

标签: arrays

我有2个阵列

Array
(
    [0] => bedroom
    [1] => traditional
    [2] => farmhouse
    [3] => modern
    [4] => contemporary
)

Array
(
    [0] => aaaa
    [1] => bbbb
    [2] => cccc
    [3] => dddd
    [4] => eeee
    )

如何组合上面的两个数组,我希望结果像 String 一样......

bedroom_aaaa, traditional_bbbb, farmhouse_cccc, modern_dddd, contemporary_eee

到目前为止,我尝试过这样,但它不起作用......

$res = array_combine ($a,$b);

                foreach($res as $r){
                  echo $res.'_'.$r.', ';
                }

2 个答案:

答案 0 :(得分:1)

从你的逻辑中我找到了方法......谢谢

        if(count($a)==count($b)){
            for($i=0;$i<count($a);$i++){
                $res = $a[$i]." _ ".$b[$i];
                echo $res.'<br />';
            }
        }

答案 1 :(得分:0)

这个php函数将两个数组作为输入,并检查它们是否具有相同的大小。如果两个阵列的大小不同,则会出现错误&#34;未定义的索引&#34;。

\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\meteor-promise\promise_server.js:165
  throw error;
  ^

Error: unexpected eof
at decorate (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\fstream\lib\abstract.js:67:36)
at Extract.Abstract.error (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\fstream\lib\abstract.js:61:12)
at Extract._streamEnd (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\tar\lib\extract.js:91:35)
at BlockStream.<anonymous> (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\tar\lib\parse.js:51:8)
at emitNone (events.js:67:13)
at BlockStream.emit (events.js:166:7)
at BlockStream._emitChunk (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\block-stream\block-stream.js:203:10)
at BlockStream.resume (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\block-stream\block-stream.js:58:15)
at Extract.Reader.resume (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\fstream\lib\reader.js:242:34)
at Entry.<anonymous> (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\tar\lib\parse.js:261:8)
at emitNone (events.js:67:13)
at Entry.emit (events.js:166:7)
at Entry.resume (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\tar\lib\entry.js:93:8)
at resume (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\fstream\lib\collect.js:62:16)
at unblockEntry (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\fstream\lib\collect.js:43:28)
at Entry.<anonymous> (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\fstream\lib\collect.js:47:9)
at FileWriter.<anonymous> (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\fstream\lib\dir-writer.js:146:11)
at emitNone (events.js:67:13)
at FileWriter.emit (events.js:166:7)
at WriteStream.<anonymous> (C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\fstream\lib\file-writer.js:45:10)
at emitOne (events.js:77:13)
at WriteStream.emit (events.js:169:7)
at C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\graceful-fs\graceful-fs.js:214:14
at C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\graceful-fs\graceful-fs.js:241:16
at C:\Users\aboo\AppData\Local\.meteor\packages\meteor-tool\1.4.1_2\mt-os.windows.x86_32\dev_bundle\lib\node_modules\cordova-lib\node_modules\npm\node_modules\graceful-fs\graceful-fs.js:232:16
at FSReqWrap.oncomplete (fs.js:82:15)