drdyszuy488152 2014-03-30 06:17
浏览 148
已采纳

如何在此PHP对象中循环并更改值?

Here is a fairly big object dumped using print_r.

https://docs.google.com/document/d/175RLhWlMQcyhGR6ffGSsoJGS3RyloEqo4EEHCL2H2vg/edit?usp=sharing

I am trying to change the values of the uploaded_files.

Towards the end of that object you'll see something like

[uploaded_files] => Array ( [attachment] => /home2/magician/public_html/development/testing/wp-content/uploads/wpcf7_uploads/Central-Coast-Montessori-logo.jpg [attachment2] => /home2/magician/public_html/development/testing/wp-content/uploads/wpcf7_uploads/Andrew.jpg )

My code // move the attachments to wpcf7ev temp folder

foreach ($cf7ev_object['uploaded_files'] as $key => $uploaded_file_path) {
    $new_filepath = WPCF7EV_UPLOADS_DIR . '/' . basename($uploaded_file_path);
    wpcf7ev_debug("New file path is {$new_filepath}");
    rename($uploaded_file_path, $new_filepath);
    wpcf7ev_debug("'{$key}'is the KEY for {$uploaded_file_path}");
    wpcf7ev_debug($cf7ev_object['uploaded_files']);        
    $cf7ev_object['uploaded_files'][$key] = $new_filepath; // this is not updating
}

To loop through it I have been using

    foreach ($cf7ev_object->uploaded_files as $key => $uploaded_file_path) {

and this has worked.

But shouldn't it be

    foreach ($cf7ev_object['uploaded_files'] as $key => $uploaded_file_path) {

? As '->' is for accessing methods?

And specifically I want to update the values of those uploaded_files, so to do that I need to do

        $cf7ev_object['uploaded_files'][$key] = $new_filepath; // this is not updating

? But this doesn't seem to be working.

I think I need to be clear on how to access values in an object.

Thanks.

  • 写回答

1条回答 默认 最新

  • douque8861 2014-03-30 06:28
    关注

    First of all, regarding the single arrow "->" that is how you reference an objects values. But I won't get into that. Since you say it works, $cf7ev_object is obviously an object.

    You say you want to "access the values in the object".

    var_dump($cf7ev_object);
    

    This will spit out what is in that object. I gather you are a bit of a newbie, so I will try to help you out best I can with the limited data you provided (you may want to expand your question.

    Looping is not a one-shot deal. You can have nested loops and nested loops inside of those. However, it is a resource hog if you're not careful. Here is an exercise that might help you.

    $new_array = array();
    foreach($cf7ev_object->uploaded_files as $key => $value) {
      $new_value = $value;//do something to the $value here
      $new_array[$key] = $new_value;
    }
    //take a look at your work now:
    print_r($new_array);
    

    I hope this helps. Note: your google doc is restricted, public can't see it.. And your question is too vague. Let me know if I can help more.

    If you want to change the object array values instantly you just set it equal to the above loop result:

    $cf7ev_object->uploaded_files = $new_array;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!