Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数据添加到JSON中,如果存在相同的值,则停止函数_Php - Fatal编程技术网

Php 将数据添加到JSON中,如果存在相同的值,则停止函数

Php 将数据添加到JSON中,如果存在相同的值,则停止函数,php,Php,很难想出这个问题的正确标题。我正在用php做一个小库存系统。这种添加方法几乎可以正常工作,但我一直在努力解决一个小问题。我可以添加具有相同ID的产品 我的代码: <?php // Viestit $same_id = "This ID number is already taken"; // Formin inputit $name = $_POST['add_nimi']; $product_id = $_POST['add_tuoteid']; $url = $

很难想出这个问题的正确标题。我正在用php做一个小库存系统。这种添加方法几乎可以正常工作,但我一直在努力解决一个小问题。我可以添加具有相同ID的产品

我的代码:

<?php

// Viestit
$same_id = "This ID number is already taken";

// Formin inputit
$name = $_POST['add_nimi'];
$product_id = $_POST['add_tuoteid'];
$url = $_POST['add_image_url'];
$category = $_POST['add_kategoria'];
$price = $_POST['add_hinta'];
$weight = $_POST['add_paino'];

// Muuttaa formin antamat stringit luvuiksi
$price = (double)$price; 
$product_id = (int)$product_id;
$weight = (double)$weight;

// Hakee JSON datan
$tavarat_json = file_get_contents('JSON/tuotteet.json'); 

// Decoodaa json filun arrayksi
$tavarat_array = json_decode($tavarat_json, true);

array_push($tavarat_array, array('nimi' => $name, "tuoteid" => $product_id, "tuotekuva_url" => $url, "kategoria" => $category, "hinta" => $price, "paino" => $weight));
    
    // Encoodaa Arrayn takaisin JSON muotoon ja pushaa sen tuotteet.json filuun
    file_put_contents('JSON/tuotteet.json', json_encode($tavarat_array));
    
    // Palauttaa add sivulle takaisin
    Header( "Location: add.php" );

?>
现在的工作代码:

$product_id_list = array_column($tavarat_array  ,'tuoteid');
$name_list = array_column($tavarat_array  ,'nimi');


// Etsii annettua tuotenumeroa arraysta $column, jossa tuoteid:t listattuna. !== tarkoittaa not identical
if(array_search($product_id, $product_id_list) !== false) // true ja false toimii molemmat samalla tavallaonko ideaa ???????
{
    
    echo    "<script>
                alert('Varastossa on tuote samalla tuotenumerolla.');
                window.location.href='add.php';
            </script>";

} elseif(array_search($name, $name_list) !== false) // true ja false toimii molemmat samalla tavallaonko ideaa ???????
{
    echo    "<script>
                alert('Varastossa on tuote samalla nimellä.');
                window.location.href='add.php';
            </script>";

} else {

    // Tekee annetuista tiedoista uuden arrayn, ja siitä uuden elementin JSON listaan
    array_push($tavarat_array, array('nimi' => $name, "tuoteid" => $product_id, "tuotekuva_url" => $url, "kategoria" => $category, "hinta" => $price, "paino" => $weight));
  
    // Encoodaa Arrayn takaisin JSON muotoon ja pushaa sen tuotteet.json filuun
    file_put_contents('JSON/tuotteet.json', json_encode($tavarat_array));
  
    // Palauttaa add sivulle takaisin
    Header( "Location: add.php" );
    
}
$product\u id\u list=array\u列($tavarat\u array,'tuoteid');
$name_list=array_列($tavarat_array,'nimi');
//在一个$column中,jossa tuoteid:t listattuna.!=tarkoittaa不完全相同
如果(数组搜索($product\U id,$product\U id\U list)!==false)//true ja false toimii molemmat samalla tavallaonko ideaa???????
{
回声“
警报(“瓦拉斯托萨在图特萨马拉图特努梅罗拉镇上”);
window.location.href='add.php';
";
}elseif(数组搜索($name,$name\u list)!==false)//true-ja-false-toimii-molemmat-samalla-tavallaonko-ideaa???????
{
回声“
警惕(“瓦拉斯托萨在图特萨马拉尼迈勒河上”);
window.location.href='add.php';
";
}否则{
//如果你想知道,在JSON列表中的元素是什么
数组推送($tavarat_数组,数组($nimi'=>$name,“tuoteid”=>$product\u id,“tuotekuva\u url”=>$url,“kategoria”=>$category,“hinta”=>$price,“paino”=>$weight));
//如果您有任何疑问,请联系我们
文件内容('JSON/tuotteet.JSON',JSON编码($tavarat_数组));
//帕劳塔加西维尔塔卡辛酒店
标题(“位置:add.php”);
}
和的组合对我很有用

$column = array_column($tavarat_array  ,'tuoteid');

if(array_search($product_id, $column) !== false) 
{
  echo "<script type='text/javascript'>alert('$same_id');</script>";

} else {

  array_push($tavarat_array, array('nimi' => $name, "tuoteid" => $product_id, "tuotekuva_url" => $url, "kategoria" => $category, "hinta" => $price, "paino" => $weight));
  
  // Encoodaa Arrayn takaisin JSON muotoon ja pushaa sen tuotteet.json filuun
  file_put_contents('JSON/tuotteet.json', json_encode($tavarat_array));
  
  // Palauttaa add sivulle takaisin
  Header( "Location: add.php" );
    
}
$column=array\u column($tavarat\u array,'tuoteid');
if(数组搜索($product\U id,$column)!==false)
{
回显“警报('$same_id');”;
}否则{
数组推送($tavarat_数组,数组($nimi'=>$name,“tuoteid”=>$product\u id,“tuotekuva\u url”=>$url,“kategoria”=>$category,“hinta”=>$price,“paino”=>$weight));
//如果您有任何疑问,请联系我们
文件内容('JSON/tuotteet.JSON',JSON编码($tavarat_数组));
//帕劳塔加西维尔塔卡辛酒店
标题(“位置:add.php”);
}

你的意思是如果产品id存在,就忽略它吗?@Jerson是的。如果product_id存在,那么ignore.added答案belowit现在起作用了,我也用它作为产品名称。在问题中添加了新代码。
[
    {
        "nimi": "Logitech K270",
        "tuoteid": 1,
        "tuotekuva_url": "https://cdn.verk.net/576/images/33/2_145225-700x423.jpeg",
        "kategoria": "Mouse",
        "hinta": 18.9,
        "paino": 0.2
    }
]


$product_id_list = array_column($tavarat_array  ,'tuoteid');
$name_list = array_column($tavarat_array  ,'nimi');


// Etsii annettua tuotenumeroa arraysta $column, jossa tuoteid:t listattuna. !== tarkoittaa not identical
if(array_search($product_id, $product_id_list) !== false) // true ja false toimii molemmat samalla tavallaonko ideaa ???????
{
    
    echo    "<script>
                alert('Varastossa on tuote samalla tuotenumerolla.');
                window.location.href='add.php';
            </script>";

} elseif(array_search($name, $name_list) !== false) // true ja false toimii molemmat samalla tavallaonko ideaa ???????
{
    echo    "<script>
                alert('Varastossa on tuote samalla nimellä.');
                window.location.href='add.php';
            </script>";

} else {

    // Tekee annetuista tiedoista uuden arrayn, ja siitä uuden elementin JSON listaan
    array_push($tavarat_array, array('nimi' => $name, "tuoteid" => $product_id, "tuotekuva_url" => $url, "kategoria" => $category, "hinta" => $price, "paino" => $weight));
  
    // Encoodaa Arrayn takaisin JSON muotoon ja pushaa sen tuotteet.json filuun
    file_put_contents('JSON/tuotteet.json', json_encode($tavarat_array));
  
    // Palauttaa add sivulle takaisin
    Header( "Location: add.php" );
    
}
$column = array_column($tavarat_array  ,'tuoteid');

if(array_search($product_id, $column) !== false) 
{
  echo "<script type='text/javascript'>alert('$same_id');</script>";

} else {

  array_push($tavarat_array, array('nimi' => $name, "tuoteid" => $product_id, "tuotekuva_url" => $url, "kategoria" => $category, "hinta" => $price, "paino" => $weight));
  
  // Encoodaa Arrayn takaisin JSON muotoon ja pushaa sen tuotteet.json filuun
  file_put_contents('JSON/tuotteet.json', json_encode($tavarat_array));
  
  // Palauttaa add sivulle takaisin
  Header( "Location: add.php" );
    
}