如何根据值删除数组中的数组

How to delete an array inside an array based on value

我在会话中有一个数组

array:7 [▼
  0 => array:2 [▼
    "store" => "store1"
    "product" => "1"
  ]
  1 => array:2 [▼
    "store" => "store2"
    "product" => "2"
  ]
  2 => array:2 [▼
    "store" => "store3"
    "product" => "4"
  ]
]

我创建了一个函数,用于在给定时删除与存储值匹配的数组。例如我给 store1 它应该删除 store1 数组并像这样输出

array:7 [▼
  0 => array:2 [▼
    "store" => "store2"
    "product" => "2"
  ]
  1 => array:2 [▼
    "store" => "store3"
    "product" => "4"
  ]
]

相反,我得到了输出

array:2 [▼
  1 => "store2"
  2 => "store3"
]

我的函数

function removeFromSessionArray($name, $value)
{
    return session()->put($name, array_diff(session()->get('stores'), [$value]));
}

谁能告诉我怎样才能达到可能的输出?

PS。学习数组。

你可以为这个数组使用简单的索引([store value].[product value]),像这样:

array:7 [▼
  'store1.1' => array:2 [▼
    "store" => "store1"
    "product" => "1"
  ]
  'store2.2' => array:2 [▼
    "store" => "store2"
    "product" => "2"
  ]
  'store3.4' => array:2 [▼
    "store" => "store3"
    "product" => "4"
  ]
]

在这些更改之后,您可以简单地从会话数组中删除任何值

试试这个

$m = session('products');
    for($i=0;$i<count($m);$i++)
    {
        if($m[$i]['store']==$username)
        {
            unset($m[$i]['store']);
            unset($m[$i]['product']);
        }
    }

    dd(array_values(array_filter($m)));