0

The following is an example of a multi-dimensional array that I'm working with:

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
        ]
      ]
        ]
      ]
    ]
  ]
]

As you can see, each key has two values, 0 and 1.

What I want to do is convert the multi-dimensional array into a string, in the format:

discount changed from 10.0 to 100.0.

updated_at changed from 2015-02-25 11:35:36 to 2015-02-25 11:35:57

...

...

lines created_at changed from xxx to yyy

lines products pivot order_line_id changed from 25 to 27.

The latter strings are based on the key names for each level of the array.

I'm nearly there, but it's not an ideal solution as it doesn't handle the level of nesting I need:

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])
            );
    }

}
1
  • What have you tried so far? We are not unpaid workers ready to solve everyone's problems. Commented Feb 25, 2015 at 11:48

2 Answers 2

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

For each dimension you have to add one.

And then you echo it like this:

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

Obviously you can do the rest.

E// Just saw you edited your post, but it doesn't really change much if you are familiar with loops.

0

This is how I ended up doing it:

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.