如何通过foreach循环覆盖数组元素?

时间:2019-05-07 08:27:43

标签: php arrays loops oop

代码应按以下方式工作:我可以创建任意数量的数组(例如$ category,$ link等)。该数组应存储在$ values中。现在,循环遍历$ values。它应该选择特定数组的“开始”和“结束”以获取“值”(可以为空,如代码示例中所示)。如果该值为null,则该值应被新值(getStringBetween方法中的文本)覆盖。之后,应该可以像这样输出新值了: echo $ category ['value'];

$category = [
        'start' => '@package',
        'end'   => '@',
        'value' => null,
];

$link = [
        'start' => '@link',
        'end'   => '@',
        'value' => null,
];

// Here I want to store the arrays in another array
$values = array($category, $link);

// getStringBetween is the function that gets a string between the defined start and end. $comment is a PHP comment which I'm getting in another method.
$values = array($category, $link);

        foreach ($values as $value)
        {
            $value['value'] = $this->getStringBetween($comment, $value['start'], $value['end']);
        }

之后,我想将字符串(getStringBewteen的值)存储到特定数组中。例如: $ category 的“值”为。循环后,它使用方法(getStringBetween)获得值“ Component”,它是 $ category 的“值”。但是,如何在$ category覆盖数组元素“ null”?对于$ link来说,它的工作原理也一样,即“ null”被getStringBetween方法的值覆盖。

0 个答案:

没有答案