【问题标题】:assoc arrays to two different arrays and join them将数组关联到两个不同的数组并将它们连接起来
【发布时间】:2015-11-04 05:47:27
【问题描述】:

我有这样的关联数组(存储在 $array 中):

Array
(
    [0] => Array
        (
            [one] => some text1
            [two] => some paragraph here1.
        )

    [1] => Array
        (
            [one] => some text2
            [two] => some paragraph here2.        
        )

    [2] => Array
        (
            [one] => some text3
            [two] => some paragraph here3.        
        )

    [3] => Array
        (
            [one] => some text4
            [two] => some paragraph here4.        
        )

    [4] => Array
        (
            [one] => some text5
            [two] => some paragraph here5.        
        )

)

现在,我想像这样存储它们的结果:

$first = '一些文本1,一些文本2,一些文本3,一些文本4,一些文本5';

$second = '这里的某个段落1.这里的一些段落2。这里有一段3。一些段落here4。这里的一些段落5。';

苦苦挣扎,我得到了这样的解决方案:

$first= '';
$second = '';
for($i = 0; $i<count($array); $i++){
    $first .= $array[$i]['one'] . ($i == count($array) - 1 ? '': ',');
    $second .= $array[$i]['two'];
}

echo $first;
echo $second;

但我希望已经有一个内置的 php 函数可以将 assoc 数组拆分为两个不同的数组。

【问题讨论】:

    标签: php


    【解决方案1】:

    对于 PHP 版本 > 5.5。您可以简单地使用 array_columnimplode 函数,如 as

    echo $first = implode(',',array_column($your_array,'one'));
    echo $second = implode(',',array_column($your_array,'two'));
    

    对于较低版本,您可以使用array_map like as

    echo $first = implode(',',array_map(function($v){ return $v['one'];} ,$your_array));
    echo $second = implode(',',array_map(function($v){ return $v['two'];},$your_array));
    

    【讨论】:

    • 试试这个echo $keywords = implode(',',array_map(function($v){ return $v['metakey'];} ,$results));$results之前错过了那个, (comma)
    【解决方案2】:
    foreach ( $array as $data ) {
       $first .= $data['one'].',';
       $two .= $data['two'].',';
    }
    
    $firstdata = rtrim($first,',');
    $seconddata = rtrim($two, ',');
    

    【讨论】:

      【解决方案3】:
       $arrFirst = array_column($array,'one');
       $arrSecond = array_column($array,'two');
      
       $first = implode(',',$arrFirst);
       $second = implode(',',$arrSecond);
      

      如果array_column() 没有退出,请使用此

      if (! function_exists('array_column')) {
          function array_column(array $input, $columnKey, $indexKey = null) {
              $array = array();
              foreach ($input as $value) {
                  if ( ! isset($value[$columnKey])) {
                      trigger_error("Key \"$columnKey\" does not exist in array");
                      return false;
                  }
                  if (is_null($indexKey)) {
                      $array[] = $value[$columnKey];
                  }
                  else {
                      if ( ! isset($value[$indexKey])) {
                          trigger_error("Key \"$indexKey\" does not exist in array");
                          return false;
                      }
                      if ( ! is_scalar($value[$indexKey])) {
                          trigger_error("Key \"$indexKey\" does not contain scalar value");
                          return false;
                      }
                      $array[$value[$indexKey]] = $value[$columnKey];
                  }
              }
              return $array;
          }
      }
      

      【讨论】:

        【解决方案4】:

        使用array_column()implode()

        array_column() 函数适用于 > PHP 5.5.0

        工作示例:

        <?php
        $arr = array(
           array('one' => 'some text1', 'two' => 'some paragraph here1'),
           array('one' => 'some text2', 'two' => 'some paragraph here2'),
           array('one' => 'some text3', 'two' => 'some paragraph here3'),
           array('one' => 'some text4', 'two' => 'some paragraph here4'),
           );
        
        $firstS = implode(', ', array_column($arr, 'one'));
        $secondS = implode('. ', array_column($arr, 'two'));
        print_r($firstS);
        echo "\n\n";
        print_r($secondS);
        ?>
        

        See it working live

        解释:

        1) 你需要数组列。

        2) 在两个单独的数组中获取数组列 onetwo

        3) impode() 这些数组以获得所需的输出。

        如果你的PHP版本低于5.5.0,你需要为array_column()写一个自定义函数。

        这样它就不会返回Fatal Error

        以下是代码:

        <?php
        if (! function_exists('array_column')) {
            function array_column(array $input, $columnKey, $indexKey = null) {
                $array = array();
                foreach ($input as $value) {
                    if ( ! isset($value[$columnKey])) {
                        trigger_error("Key \"$columnKey\" does not exist in array");
                        return false;
                    }
                    if (is_null($indexKey)) {
                        $array[] = $value[$columnKey];
                    }
                    else {
                        if ( ! isset($value[$indexKey])) {
                            trigger_error("Key \"$indexKey\" does not exist in array");
                            return false;
                        }
                        if ( ! is_scalar($value[$indexKey])) {
                            trigger_error("Key \"$indexKey\" does not contain scalar value");
                            return false;
                        }
                        $array[$value[$indexKey]] = $value[$columnKey];
                    }
                }
                return $array;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2015-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-06
          相关资源
          最近更新 更多