C
C
CapitanFreeloader2016-10-11 19:01:35
PHP
CapitanFreeloader, 2016-10-11 19:01:35

How to update data in an associative array of arrays?

In general, the essence is to come json array inside which there are arrays and they are also arrays.
It is necessary to replace each link of the nested array and return json back
. I do this:

foreach ($pays as $ke => &$va) {
  $avatar = (array)$va;
  foreach ($avatar as $key => &$value) {
    if($key == "avatar" or $key == "flag"){
      $path = $_SERVER['DOCUMENT_ROOT'].dirname(parse_url($value)['path'])."/";
      $file = basename(parse_url($value)['path']);
      if(!is_dir($path)){
        mkdir($path, 0777, true);
      } 
      if(is_dir($path)){
        file_put_contents($path.$file, file_get_contents($value));
        $newpath = dirname(parse_url($value)['path'])."/".$file;
        $value = $newpath;
      }
      $newpath = dirname(parse_url($value)['path'])."/".$file;
      $value = $newpath;
    }
  }
}

At the level of the second foreach, everything works well => the replacement occurs.
But when I want to print the result after all this (print_r($pays)) I see that the links have not been replaced, what should I do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
CapitanFreeloader, 2016-10-11
@CapitanFreeloader

I answer myself:

foreach ($pays as $ke => &$va) {
  $avatar = (array)$va;
  foreach ($avatar as $key => &$value) {
    if($key == "avatar" or $key == "flag"){
      $path = $_SERVER['DOCUMENT_ROOT'].dirname(parse_url($value)['path'])."/";
      $file = basename(parse_url($value)['path']);
      /*if(!is_dir($path)){
        mkdir($path, 0777, true);
      } 
      if(is_dir($path)){
        file_put_contents($path.$file, file_get_contents($value));
        $newpath = dirname(parse_url($value)['path'])."/".$file;
        $value = $newpath;
      }*/
      $newpath = dirname(parse_url($value)['path'])."/".$file;
      $value = $newpath;
      echo "<br>$value";
    }
  }
  $va = $avatar;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question