PHP将多维多深度数组转换为字符串

时间:2015-02-25 11:45:18

标签: php arrays multidimensional-array

以下是我正在使用的多维数组的示例:

array:4 [
  "discount" => array:2 [
    0 => 10.0
    1 => 100.0
  ]
  "updated_at" => array:2 [
    0 => "2015-02-25 11:35:36"
    1 => "2015-02-25 11:35:57"
  ]
  "creator" => array:1 [
    "url" => array:2 [
      0 => null
      1 => null
    ] 
  ]
  "lines" => array:2 [
    0 => array:4 [
      "created_at" => array:2 [
        0 => "2015-02-25 11:35:36"
        1 => "2015-02-25 11:35:57"
      ]
      "updated_at" => array:2 [
        0 => "2015-02-25 11:35:36"
        1 => "2015-02-25 11:35:57"
      ]
      "products" => array:1 [
        0 => array:1 [
           "pivot" => array:1 [
             "order_line_id" => array:2 [
               0 => 25
               1 => 27
        ]
      ]
    ]
  ]
  "sites" => array:1 [
    0 => array:1 [
      "pivot" => array:1 [
        "order_line_id" => array:2 [
          0 => 25
          1 => 27
        ]
      ]
    ]
  ]
]
1 => array:4 [
  "discount" => array:2 [
    0 => 10.0
    1 => 100.0
  ]
  "created_at" => array:2 [
    0 => "2015-02-25 11:35:36"
    1 => "2015-02-25 11:35:57"
  ]
  "updated_at" => array:2 [
    0 => "2015-02-25 11:35:36"
    1 => "2015-02-25 11:35:57"
  ]
  "products" => array:2 [
    0 => array:1 [
      "pivot" => array:1 [
        "order_line_id" => array:2 [
          0 => 26
          1 => 28
        ]
      ]
    ]
    1 => array:1 [
      "pivot" => array:1 [
        "order_line_id" => array:2 [
          0 => 26
          1 => 28
        ]
      ]
        ]
      ]
    ]
  ]
]

如您所见,每个键都有两个值,0和1.

我想要做的是将多维数组转换为字符串,格式为:

  

折扣从10.0更改为100.0。

     

updated_at已于2015-02-25 11:35:36更改为2015-02-25 11:35:57

     

...

     

...

     

lines_tat的行已从xxx更改为yyy

     

line products pivot order_line_id从25变为27.

后面的字符串基于数组每个级别的键名。

我几乎就在那里,但它并不是一个理想的解决方案,因为它无法处理我需要的嵌套级别:

convertDifferencesArrayToString($difference)


public function convertDifferencesArrayToString($array)
{
    // Nothing was changed, or only the creator timestamps were touched.
    if(!$array || (sizeof($array)==1 && isset($array['creator']))){
        return 'Submitted the same data';
    }

    $string = 'Changed ';

    foreach($array as $key=>$value)
    {
        // Calculate the difference for standard values
        if(!$this->hasNestedRelations($value))
        {
            $string .= $this->convertValuesToString($key, $value);
        }
        else
        {
            foreach($value as $rel_key=>$rel_val)
            {
                $string .= $this->convertValuesToString($rel_key, $rel_val, $key);
            }
        }

    }
    return rtrim($string, ', ');
}

private function hasNestedRelations($value)
{
    if(array_key_exists(0, $value) && array_key_exists(1, $value)) {
        return false;
    }

    return true;
}

private function convertValuesToString($key, $value, $parent_key = null)
{
    // The 'creator' parent key is excluded, just in case the user is editing
    // their own user account, which they made themselves.

    if($parent_key) {
        if($key=='name'){
            return sprintf('%s%s from %s to %s, ',
                $parent_key ? ucwords($parent_key) : '',
                '',
                (is_null($value[0]) ? '[empty]' : $value[0]),
                (is_null($value[0]) ? '[empty]' : $value[1])
            );
        }
    } else {
        if($key!='updated_at' && ($value[0]!==$value[1]))
            return sprintf('%s%s from %s to %s, ',
                $parent_key ? ucwords(str_replace('_', ' ', $parent_key)) . ': ' : '',
                ucwords(str_replace('_', ' ', $key)),
                (is_null($value[0]) ? '[empty]' : $value[0]),
                (is_null($value[0]) ? '[empty]' : $value[1])
            );
    }

}

2 个答案:

答案 0 :(得分:0)

$array[0]['discount']
$array[1]['discount']

对于每个维度,您必须添加一个。

然后你就像这样回应:

echo $array[0]['discount'], " to " +, $array[1]['discount']

显然你可以做其余的事。

E //刚刚看到你编辑了你的帖子,但是如果你熟悉循环,它并没有太大的改变。

答案 1 :(得分:0)

这就是我最终做到的方式:

public function recursivelyConvertArrayToString($data)
{
    $string = '';

    foreach($data as $key=>$val)
    {
        // If the $val doesn't have two values with an index of 0 and 1, recursively recall the function with the data
        if(!array_key_exists(0, $val) && !array_key_exists(1, $val)) {
            $string .= $this->recursivelyConvertArrayToString($val);
        }
        else{
            // $val[0] contains an array of more data
            if(is_array($val[0])){
                $string .= $this->recursivelyConvertArrayToString($val[0]);
            } else {
                // Ignore if both fields changed are null
                if($val[0]!==null && $val[1]!==null){
                    $string .= ucwords(str_replace('_', ' ', $key)) . ' from ' . $val[0] . ' to ' . $val[1] . ', ';
                }
            }
        }
    }

    return $string;
}