0

i'm pulling data from the database and get an array of the format:

array(2) {
  [0]=>
  array(3) {
    ["topicname"]=>
    string(19) "Signs giving orders"
    ["title"]=>
    array(4) {
      [0]=>
      string(4) "Stop"
      [1]=>
      string(8) "No entry"
      [2]=>
      string(10) "No cycling"
      [3]=>
      string(17) "No motor vehicles"
    }
    ["filename"]=>
    array(4) {
      [0]=>
      string(44) "DQP0GVcUA2dG8ZfqeVYLO68YodgYnJMOjJw2o2iC.png"
      [1]=>
      string(44) "vWUcHGX3VVKPT08JXh9mAqZ40pT0vfDJ78Yoqovz.png"
      [2]=>
      string(44) "bplX8bbwHzHKX9n6SvvQiYNhkWwKxi2bhsrQ94U2.png"
      [3]=>
      string(44) "8KAEovxQn3EgzHoZg1euSgNYTFupnLdKusJ4SIEP.png"
    }
  }
  [1]=>
  array(3) {
    ["topicname"]=>
    string(13) "Warning Signs"
    ["title"]=>
    array(4) {
      [0]=>
      string(29) "Road narrows ahead both sides"
      [1]=>
      string(47) "Road narrows on right (left if symbol reversed)"
      [2]=>
      string(10) "Crossroads"
      [3]=>
      string(22) "Junction on bend ahead"
    }
    ["filename"]=>
    array(4) {
      [0]=>
      string(44) "uZ7fnDjlYfKU3pdSQ7wAq4siFryA6jSEitmRyPhp.png"
      [1]=>
      string(44) "4pslo6cC0I2E606DscWxiDD3sCBW7ZGaESQES4r0.png"
      [2]=>
      string(44) "xdVBMpV6PTLtX48M67kfWOqy59rsYMDZMuWbqDwl.png"
      [3]=>
      string(44) "hLtIGikARgARqmSqit4mqNXRFJthipH6O5vqxlbN.png"
    }
  }
}

The above array is generated by the bit below:

$data = array();

foreach ($content as $value)
{
      $title = explode(',', $value->title);
      $filename = explode(',', $value->filename);
      $d = array();

       $d['topicname'] = $value->topicname;
       $d['title'] = $title;
       $d['filename'] = $filename;

        $data[] = $d;

}
var_dump($data);

From here, my goal is to get the ["filename"] and manipulate it then return it to the array set so that at the end, i get an array of the structure:

array(2) {
  [0]=>
  array(3) {
    ["topicname"]=>
    string(19) "Signs giving orders"
    ["title"]=>
    array(4) {
      [0]=>
      string(4) "Stop"
      [1]=>
      string(8) "No entry"
      [2]=>
      string(10) "No cycling"
      [3]=>
      string(17) "No motor vehicles"
    }
    ["filename"]=>
    array(4) {
      [0]=>
      string(44) "/storage/images/DQP0GVcUA2dG8ZfqeVYLO68YodgYnJMOjJw2o2iC.png"
      [1]=>
      string(44) "/storage/images/vWUcHGX3VVKPT08JXh9mAqZ40pT0vfDJ78Yoqovz.png"
      [2]=>
      string(44) "/storage/images/bplX8bbwHzHKX9n6SvvQiYNhkWwKxi2bhsrQ94U2.png"
      [3]=>
      string(44) "/storage/images/8KAEovxQn3EgzHoZg1euSgNYTFupnLdKusJ4SIEP.png"
    }
  }
  [1]=>
  array(3) {
    ["topicname"]=>
    string(13) "Warning Signs"
    ["title"]=>
    array(4) {
      [0]=>
      string(29) "Road narrows ahead both sides"
      [1]=>
      string(47) "Road narrows on right (left if symbol reversed)"
      [2]=>
      string(10) "Crossroads"
      [3]=>
      string(22) "Junction on bend ahead"
    }
    ["filename"]=>
    array(4) {
      [0]=>
      string(44) "/storage/images/uZ7fnDjlYfKU3pdSQ7wAq4siFryA6jSEitmRyPhp.png"
      [1]=>
      string(44) "/storage/images/4pslo6cC0I2E606DscWxiDD3sCBW7ZGaESQES4r0.png"
      [2]=>
      string(44) "/storage/images/xdVBMpV6PTLtX48M67kfWOqy59rsYMDZMuWbqDwl.png"
      [3]=>
      string(44) "/storage/images/hLtIGikARgARqmSqit4mqNXRFJthipH6O5vqxlbN.png"
    }
  }
}

notice how now the filenames have been modified. to achieve the above, i get the array returned from var_dump($data);, then do my manipulation as follows:

foreach ($data as $val)
{
     foreach ($val['filename'] as $filecheck )
     {

          //get extension since images and videos are in separate folders
          $file_ext = File::extension($filecheck);
          $images = array('jpg','gif','png');
          $videos = array('mp4', 'webm', 'ogg');

          if(in_array($file_ext, $images))
          {
               $filename = 'images/'.$filecheck;
               $file_url = Storage::url($filename);
               $filecheck = $file_url;
          }
          elseif(in_array($extension, $videos))
          {
               $filename = 'videos/'.$filecheck;
               $file_url = Storage::url($filename);
               $filecheck = $file_url;
          }
          $file_array = array();
          $file_array['filename'] = $filecheck;
      }
 }

The purpose of the above is: to grab the filename that is returned from the database and map it to its appropriate filepath on the server. the contents of var_dump($file_array) are:

string(60) "/storage/images/DQP0GVcUA2dG8ZfqeVYLO68YodgYnJMOjJw2o2iC.png"
string(60) "/storage/images/vWUcHGX3VVKPT08JXh9mAqZ40pT0vfDJ78Yoqovz.png"
string(60) "/storage/images/bplX8bbwHzHKX9n6SvvQiYNhkWwKxi2bhsrQ94U2.png"
string(60) "/storage/images/8KAEovxQn3EgzHoZg1euSgNYTFupnLdKusJ4SIEP.png"
string(60) "/storage/images/uZ7fnDjlYfKU3pdSQ7wAq4siFryA6jSEitmRyPhp.png"
string(60) "/storage/images/4pslo6cC0I2E606DscWxiDD3sCBW7ZGaESQES4r0.png"
string(60) "/storage/images/xdVBMpV6PTLtX48M67kfWOqy59rsYMDZMuWbqDwl.png"
string(60) "/storage/images/hLtIGikARgARqmSqit4mqNXRFJthipH6O5vqxlbN.png"

This is where i get stuck. how do i now map this back to my array?

2 Answers 2

2

in PHP the foreach works on a copy of that array so you need to set the data on the original array i.e.:

foreach ($data as $index => $val) {
    foreach ($val['filename'] as $fileIndex => $filecheck ) {
        // convert file name

        $data[$index]['filename'][$fileIndex] = $filename;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can use this code

    foreach ($originalArray as $subArray) {
    $i = 0;
    foreach ($subArray['filename'] as $filePath) {
        $filePath = //do your path thing here
        $subArray['filename'][$i] = $filePath;
        $i++;
    }
}

Comments

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.