Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 如果键存在,则替换多维数组中的数组_Php_Arrays_Multidimensional Array - Fatal编程技术网

Php 如果键存在,则替换多维数组中的数组

Php 如果键存在,则替换多维数组中的数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我试图构建一个表单,其中用户从字符串输入数据,表单将根据输入通过json src获得数据子集,并创建多维数组并将其保存在数据库中。如果输入以前添加到数据库中,我不希望它再次附加到数组中 下面是我的代码的外观: //SET UP the Array $thisquery_themes[] = array( strtolower($themename) => array( "author"=>$data['Author'], "desc"=>

我试图构建一个表单,其中用户从字符串输入数据,表单将根据输入通过json src获得数据子集,并创建多维数组并将其保存在数据库中。如果输入以前添加到数据库中,我不希望它再次附加到数组中

下面是我的代码的外观:

//SET UP the Array
$thisquery_themes[] = array(
    strtolower($themename) => array(
        "author"=>$data['Author'],
        "desc"=>$data['Description'],
        "screenshot"=> $screenshot,
        "link"=> $data['URI'],
        "count"=> 1
    )
);

//Get Previously saved data
$existing_themes = get_option('top_themes');

if(!empty($existing_themes)){
    foreach ($existing_themes as $group){
        foreach(array_keys($group) as $key) {
            if($group[strtolower($themename)] == strtolower($themename)){
                unset($group[$key][strtolower($themename)]);
            }
        }
    }

    $total_themes= array_merge($existing_themes , $thisquery_themes);
    update_option('top_themes', $total_themes);
} else {
    update_option('top_themes', $thisquery_themes);
}
不是。如果该键存在于数组中,则仍将数据添加到数组中:

Array ( 
[0] => Array ( 
        [towfiq-i._v5] => Array ( 
           [author] => Towfiq I. 
           [desc] => Towfiq I. official website. 
           [count] => 1 
        ) 
) 
[1] => Array ( 
        [towfiq-i._v5] => Array ( 
           [author] => Towfiq I. 
           [desc] => Towfiq I. official website. 
           [count] => 1 
        ) 
) 
[2] => Array ( 
        [wp-bangla] => Array ( 
           [author] => Ifty Rahman 
           [desc] => A website template for wpbangla
           [count] => 1 
        ) 
) 
[3] => Array ( 
        [towfiq-i._v5] => Array ( 
           [author] => Towfiq I. 
           [desc] => Towfiq I. official website. 
           [count] => 1 
        ) 
)
[4] => Array ( 
        [wp-bangla] => Array ( 
           [author] => Ifty Rahman 
           [desc] => A website template for wpbangla 
           [count] => 1 
        ) 
)
但我希望它是这样的(注意“count”字段值是如何加起来的。如果可能的话,请告诉我):


任何帮助都将不胜感激。谢谢

如果数组只包含一个值,为什么要在数组中使用数组?除非这是向你扔的东西,否则你的结构很容易看起来像:

Array ( 
        [towfiq-i._v5] => Array ( 
           [author] => Towfiq I. 
           [desc] => Towfiq I. official website. 
           [count] => 3 
        ) 
        [wp-bangla] => Array ( 
           [author] => Ifty Rahman 
           [desc] => A website template for wpbangla 
           [count] => 2 
        ) 
) 
然后,您的世界将变得更加容易,因为您可以通过使用
isset($existing_themes[strtolower($themename)])
检查数组元素是否存在(例如)

如果无法更改接收
$existing_themes
的方式,则可以按如下方式重新格式化数据:

$existing_themes_mod = array();
foreach ($existing_themes as $t) {
    $existing_themes_mod[key($t)] = reset($t);
}
这将为您提供足够的策略,使您能够使用现有条目而不是附件来运行“更新”。如果你需要帮助写它,请先告诉我你的尝试

编辑-感谢粘贴您的代码。顺便说一下,你应该注意所有的空白。。。这里有一个更好的方法来实现你的目标

if (!empty($existing_themes)) {
    if (isset($existing_themes[strtolower($themename)])) {
        if (isset($existing_themes[strtolower($themename)]['count'])) {
            $existing_themes[strtolower($themename)]['count'] = $existing_themes[strtolower($themename)]['count'] + 1;
        } else {
            $existing_themes[strtolower($themename)]['count'] = 1;
        }                                              
        $thisquery_themes = array();
    }                               
    $total_themes= array_merge($existing_themes , $thisquery_themes);
    update_option('top_themes', $total_themes);                       
} else {
    update_option('top_themes', $thisquery_themes);
}

您的尝试不起作用的原因是,当您尝试通过将
$value
增加1来更改它时,它不会修改原始数组。当您像那样使用
foreach
时,需要通过引用进行迭代,否则无法修改值。你为自己做的工作太多了

好极了。不知道为什么我要把数组结构弄得这么复杂。。谢谢你指出这一点。我接受了你的建议,解决了重复输入的问题。我遇到的唯一问题是,更新count字段。下面是我的代码的外观:。谢谢
if (!empty($existing_themes)) {
    if (isset($existing_themes[strtolower($themename)])) {
        if (isset($existing_themes[strtolower($themename)]['count'])) {
            $existing_themes[strtolower($themename)]['count'] = $existing_themes[strtolower($themename)]['count'] + 1;
        } else {
            $existing_themes[strtolower($themename)]['count'] = 1;
        }                                              
        $thisquery_themes = array();
    }                               
    $total_themes= array_merge($existing_themes , $thisquery_themes);
    update_option('top_themes', $total_themes);                       
} else {
    update_option('top_themes', $thisquery_themes);
}