繁体   English   中英

合并数组-PHP

[英]Combining arrays - PHP

我有一个像这样的数组:

print_r($arr);

array(2) {
  ["'type'"]=>
  array(3) {
    [0]=>
    string(17) "tell" // <----
    [1]=>
    string(6) "mobile" // <----
    [2]=>
    string(6) "address" // <----
  }
  ["'value'"]=>
  array(3) {
    [0]=>
    string(11) "+00.0000000" // tell
    [1]=>
    string(11) "12345678" // mobile
    [2]=>
    string(11) "Blah SQ." // address
  }
}

我想要一个像这样的最终字符串:

tell = +00.0000000<br />mobile = 12345678<br />address = Blah SQ.

现在我已经花了一个多小时来解决这个问题,但是还没有结果,有人可以帮助我吗? 我将不胜感激。

谢谢

======================================

我试过的

$arr是一个数组,所以我这样做了:

foreach($arr as $values){
// here also $values is an array, so I needed another foreach to access to items:
    $i = 0;
    foreach($values as $items){
        // now making the final output
        @$output.= $items['type'][$i] . '=' . $items['value'][$i] . '<br />';
        $i++;
    }
}

我会去找array_combine() 基本上,它可以满足您的要求:

$yourArray = [
             "type"  => ["tell", "mobile", "address"], 
             "value" => ["+00.0000000", "12345678", "Blah SQ."]
             ];


$combined = array_combine($yourArray["type"], $yourArray["value"]);

将会

$combined = [
             "tell"    =>"+00.0000000",
             "mobile"  =>"12345678",
             "address" =>"Blah SQ."
            ];

最后,您可以遍历该数组,然后加入值:

$finalArray=array();

foreach($combined as $type=>$value)
      $finalArray[]="$type=$value";

$string = join("<br/>", $finalArray); // Will output tell=+00.000000<br/>mobile=12345678<br/>address=Blah SQ.

这不是最快的方法,但是您会学到很多有关数组的知识。

编辑(使用@Dencker的array_combine)

foreach($arr as $values) {
// here also $values is an array, so I needed another foreach to access to items:
    $v = array_combine($values["'type'"], $values["'value'"]);

    foreach($v as $key => $val) {
        // now making the final output
        $output.= $key . '=' . $val . '<br />';
    }
}

尝试这个

    $arr = array(
    array(
        "'type'" => array('tell', 'mobile', 'address'),
        "'value'" => array('+00000', '123123', 'foo')
    ),
    array(
        "'type'" => array('tell', 'mobile', 'address'),
        "'value'" => array('+10000', '123123', 'bar')
    ),
    array(
        "'type'" => array('tell', 'mobile', 'address'),
        "'value'" => array('+20000', '123123', 'foobar')
    ),
);

var_dump($arr);

$output = '';

foreach($arr as $values) {
// here also $values is an array, so I needed another foreach to access to items:
    $i = 0;

    foreach($values as $items) {
        // now making the final output
        $output.= $values["'type'"][$i] . '=' . $values["'value'"][$i] . '<br />';
        $i++;
    }
}

echo $output;

您在第二个循环中引用了另一个数组。

================================================== ===========编辑:

var_dump($arr);

array(3) { 
  [0]=> array(2) { 
       ["type"]=> array(3) { 
            [0]=> string(4) "tell" 
            [1]=> string(6) "mobile" 
            [2]=> string(7) "address" 
       } 
       ["value"]=> array(3) { 
            [0]=> string(6) "+00000" 
            [1]=> string(6) "123123" 
            [2]=> string(3) "foo" 
       } 
  } 
  [1]=> array(2) { 
       ["type"]=> array(3) { 
            [0]=> string(4) "tell" 
            [1]=> string(6) "mobile" 
            [2]=> string(7) "address" 
       } 
       ["value"]=> array(3) { 
            [0]=> string(6) "+10000" 
            [1]=> string(6) "123123" 
            [2]=> string(3) "bar" 
       } 
  } 
  [2]=> array(2) { 
      ["type"]=> array(3) { 
            [0]=> string(4) "tell" 
            [1]=> string(6) "mobile" 
            [2]=> string(7) "address" 
      } 
      ["value"]=> array(3) { 
            [0]=> string(6) "+20000" 
            [1]=> string(6) "123123" 
            [2]=> string(6) "foobar" 
      } 
  } 
} 

OUTPUT:
tell=+00000
mobile=123123
tell=+10000
mobile=123123
tell=+20000
mobile=123123

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM