【问题标题】:How to replace some fields of an associative array in PHP?如何在 PHP 中替换关联数组的某些字段?
【发布时间】:2016-05-31 16:13:05
【问题描述】:

如何替换数组中名为“replaceMe”和“replaceMeToo”的所有字段?我知道在 stackoverflow 上有类似的问题,但没有人真正帮助过我。

$array 从 $db->loadObjectList() 中返回;在 Joomla 中。

$array 的 var_dump 示例:

array(2) {
  [0]=>
  object(stdClass)#118 (14) {
    ["id"]=>
    string(1) "1"
    ["replaceMe"]=>
    string(2) "48"
    ["replaceMeToo"]=>
    string(2) "53"
  }
  [1]=>
  object(stdClass)#119 (14) {
    ["id"]=>
    string(1) "1"
    ["replaceMe"]=>
    string(2) "5555"
    ["replaceMeToo"]=>
    string(2) "5555"
  }
}

我想做这样的事情:

// @param int, @return string
$array['replaceMe'] = doSomething($array['replaceMe']);
$array['replaceMeToo'] = doSomething($array['replaceMeToo']);

结果应该是这样的:

array(2) {
  [0]=>
  object(stdClass)#118 (14) {
    ["id"]=>
    string(1) "1"
    ["replaceMe"]=>
    string(2) "I was replaced by a value from db #1"
    ["replaceMeToo"]=>
    string(2) "I was replaced by a value from db #2"
  }
  [1]=>
  object(stdClass)#119 (14) {
    ["id"]=>
    string(1) "1"
    ["replaceMe"]=>
    string(2) "I was replaced by a value from db #3"
    ["replaceMeToo"]=>
    string(2) "I was replaced by a value from db #4"
  }
}

谢谢。

【问题讨论】:

  • 我不清楚你想得到什么结果。你能用替换后想要得到的结果数组来完成你的问题吗?
  • 好的,我添加了一个结果示例。

标签: php arrays associative-array


【解决方案1】:

试试

foreach($array as $arr){

  $arr->replaceMeToo = $arr->replaceMe;
  unset($arr->replaceMe);

}

【讨论】:

  • 请考虑添加一些解释
【解决方案2】:

尝试使用str_replace,示例代码:

带循环:

foreach ($array as &$new) {
  $new = str_replace('replaceMe', 'replacedYou', str_replace('replaceMeToo', 'replacedYouTo', $new));
}

没有循环:

$new = str_replace('replaceMe', 'replacedYou', str_replace('replaceMeToo', 'replacedYouTo', $array));

【讨论】:

    【解决方案3】:

    您可以使用array_replace

    例子

    $yourArray = array("id","replaceMe","replaceMeToo");
    $replaceMentArray = array(1 => "db1", 2 => "db2");
    $newArray = array_replace($yourArray,$replaceMentArray);
    

    输出

    Array
    (
        [0] => id
        [1] => db1
        [2] => db2        
    )
    

    【讨论】:

      【解决方案4】:

      感谢所有回答。你帮了我!每个人都投赞成票。

      我这样解决了:

              $result = $db->loadObjectList();
      
              $newArray = array();
              foreach ($result as $row) {
                  $tempArray = array(
                      'id' => $row->id,
                      'published' => $row->published,
                      'date' => $row->date,
                      'hours' => $row->hours,
                      'class' => $row->class,
                      'type' => $row->type,
                      'subject' => $row->subject,
                      'subject_sub' => $row->subject_sub,
                      'teacher' => JFactory::getUser($row->teacher)->name, //replace userId with name
                      'teacher_sub' => JFactory::getUser($row->teacher_sub)->name, // replace userId with name
                      'room' => $row->room,
                      'comment' => $row->comment,
                      'created' => $row->created,
                      'modified' => $row->modified
                  );
                  array_push($newArray, $tempArray);
              }
      
              echo json_encode($newArray, JSON_FORCE_OBJECT);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-04
        • 1970-01-01
        • 2014-03-27
        • 2017-12-24
        • 2021-01-19
        • 2019-09-17
        • 1970-01-01
        相关资源
        最近更新 更多