组合数组 - PHP

时间:2014-07-15 10:05:55

标签: php arrays

我有一个像:

这样的数组
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++;
    }
}

2 个答案:

答案 0 :(得分:3)

我去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.

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

答案 1 :(得分:0)

编辑(使用@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