检索For Each Loop Reference& $ value中的先前值

时间:2018-01-20 03:02:27

标签: php arrays loops foreach reference

这是$array的样子

Array
(
    [0] => Array
        (
            [id] => 1
            [SKU] => SKU_1
            [ProductIDRef] => 45645-12
        )

    [1] => Array
        (
            [id] => 2
            [SKU] => SKU_2
            [ProductIDRef] => 43445-45
        )

    [2] => Array
        (
            [id] => 3
            [SKU] => SKU_2
            [ProductIDRef] => 
        )
)

正如您在数组[1]中所看到的,它包含与数组SKU中相同的[2]值。

但问题是,即使数组[2]与数组SKU具有相同的[1],数组[2]也没有ProductIDRef的值。

我要做的是在我的foreach循环中引用以前的数组 - 在上面的示例中,数组[1]需要传递给数组[2]

如果找到前一次迭代中相同的SKU值(SKU_2),请插入缺少的(或NULLProductIDRef IN数组[2]在:

[2] => Array
    (
        [id] => 3
        [SKU] => SKU_2
        [ProductIDRef] => 43445-45
    )

使用foreach使用&$将其循环以修改原始$array

foreach($array as $key => &$value)
{
// if previous SKU value is found . . . 
// $value['ProductIDref'] = $previousProductIDRefValue;
}

我已经尝试了常见的foreach策略,在循环之前设置$f = 0;并在循环结束前使用$f+;递增它。

$f = 0;
foreach($array as $key => &$value)
{
$value['customkey'] = $f;
$f++;
}

这样可行,并返回$array,如下所示:

Array
(
    [0] => Array
        (
            [id] => 1
            [ProductID] => 182040460699
            [SKU] => SKU_1
            [ProductIDRef] => 132438205662
            [customkey] => 1
        )

    [1] => Array
        (
            [id] => 2
            [ProductID] => 182040460699
            [SKU] => SKU_2
            [ProductIDRef] => 132438205662
            [customkey] => 2
        )

    [2] => Array
        (
            [id] => 3
            [ProductID] => 182040460699
            [SKU] => SKU_3
            [ProductIDRef] => 
            [customkey] => 3
        )
)

我可以有条件地为我的数组设置新值,或修改现有值

    $value['customkey'] = $f;

    if ($f == 2) {
    ///// set new `key => value` pair in Array [2] 
    // $value['status'] = 'unpublished';
    ///// modify the value of ProductIDRef in Array [2]
    // $value['ProductIDRef'] = 'inserted_value';
    }

    $f++;

但是做$value['ProductIDRef'] = $value[1]['ProductIDRef'];这样的事情不起作用。

这正是我试图测试php &$引用运算符的功能..另外,即使它失败了,这个测试假设我知道索引数组中的哪个键我想要引用并获取它的值 - 而不是我实际上想要做的是每个SKU和{{的搜索或引用1}}数组中的键,用于检测一对并插入其值。

有没有办法使用ProductIDRef &$循环执行此操作,或者我必须执行不同的操作,例如拆分数组并单独引用它们(有关如何最好地执行此操作的建议将也被接受了。)

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

$arr = array(
    array
    (
        "id" => 1,
        "SKU" => 'SKU_1',
        "ProductIDRef" => '45645-12'
    ),
    array
    (
        "id" => 2,
        "SKU" => 'SKU_2',
        "ProductIDRef" => '43445-45'
    ),
    array
    (
        "id" => 3,
        "SKU" => 'SKU_2',
        "ProductIDRef" => null
    )
);

foreach( $arr as $key => $value ) {
    //Check if current item's ProductIDRef is empty or null
    //Check if prev entry exist AND SKU is the same
    if ( ( $value[ "ProductIDRef" ] == "" || $value[ "ProductIDRef" ] == null ) && ( isset( $arr[ $key - 1 ] ) && $arr[ $key - 1 ][ "SKU" ] == $value[ "SKU" ] ) ) {
        $arr[ $key ][ "ProductIDRef" ] = $arr[ $key - 1 ][ "ProductIDRef" ];
    }
}

echo "<pre>";
print_r( $arr );
echo "</pre>";

这将导致:

Array
(
    [0] => Array
        (
            [id] => 1
            [SKU] => SKU_1
            [ProductIDRef] => 45645-12
        )

    [1] => Array
        (
            [id] => 2
            [SKU] => SKU_2
            [ProductIDRef] => 43445-45
        )

    [2] => Array
        (
            [id] => 3
            [SKU] => SKU_2
            [ProductIDRef] => 43445-45
        )

)