Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.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多维数组-基于密钥ID更新值_Php_Arrays - Fatal编程技术网

Php多维数组-基于密钥ID更新值

Php多维数组-基于密钥ID更新值,php,arrays,Php,Arrays,我一直在阅读一些问题和答案,但我还没有找到解决我的问题的办法。我试图更新在CMS中生成并存储在mysql中用于计数器的json文件。是一个多维数组,我需要更新的部分是key=>15下的这个嵌套部分: array (size=39) 'admin_label' => string 'C04' (length=3) 'number' => string '194' (length=3) 'number_addtext' => string '+' (length=1) 'durat

我一直在阅读一些问题和答案,但我还没有找到解决我的问题的办法。我试图更新在CMS中生成并存储在mysql中用于计数器的json文件。是一个多维数组,我需要更新的部分是key=>15下的这个嵌套部分:

array (size=39)
'admin_label' => string 'C04' (length=3)
'number' => string '194' (length=3)
'number_addtext' => string '+' (length=1)
'duration' => string '2400' (length=4)
'counter_title' => string 'PAGES ACTIVE' (length=14)
所以我只是对其进行json解码,并以这种方式更新值:

 $json[15]['columns'][1]['addons'][2]['settings']['number'] = $val1;
 $json[15]['columns'][2]['addons'][2]['settings']['number'] = $val2;
 $json[15]['columns'][3]['addons'][2]['settings']['number'] = $val3;
 $json[15]['columns'][4]['addons'][2]['settings']['number'] = $val4;
然后我对它进行编码并更新数据库。但是如果父密钥发生更改,脚本将无法工作

例如,我想找到一种方法来搜索值“C04”作为参考,甚至是计数器标题“PAGES ACTIVE”,然后更新嵌套数组的键“number”的值,这样,如果父键更改,脚本将正常工作。这可能吗?大概是这样的:

 function findarray($item, $key)
 {
 if($key == 'admin_label' && $item == 'C02') {
   // find next key named number and update that value
} else if($key == 'admin_label' && $item == 'C03') {
   // find next key named number and update that value
} else if($key == 'admin_label' && $item == 'C04') {
   // find next key named number and update that value
}
 }

 array_walk_recursive($json, 'findarray');
    <?php
    function myfunction($json, $field, $value)
    {
     foreach($json as $key11  => $pro1){
        foreach($pro1['columns'] as $key1  => $pro){
        foreach($pro as $k => $products){
       foreach($products as $key => $product)
       {
          if ( $product['settings'][$field] === $value ){
              $json[$key11]['columns'][$key1][$k][$key]['settings']['counter_title']="FOUND IT";
            break;
                  }
       }
        }
        }
}
       return $json;
    }


     $json[15]['columns'][1]['addons'][2]['settings']['number'] ='val1';
     $json[15]['columns'][2]['addons'][2]['settings']['number'] = 'val2';
     $json[15]['columns'][3]['addons'][2]['settings']['number'] = 'val3';
     $json[15]['columns'][4]['addons'][2]['settings']['number'] = 'val4';

        $x= myfunction($json, 'number', 'val3');


     print_r($x);

最好的问候

根据JSON的结构,您可以尝试以下方式:

 function findarray($item, $key)
 {
 if($key == 'admin_label' && $item == 'C02') {
   // find next key named number and update that value
} else if($key == 'admin_label' && $item == 'C03') {
   // find next key named number and update that value
} else if($key == 'admin_label' && $item == 'C04') {
   // find next key named number and update that value
}
 }

 array_walk_recursive($json, 'findarray');
    <?php
    function myfunction($json, $field, $value)
    {
     foreach($json as $key11  => $pro1){
        foreach($pro1['columns'] as $key1  => $pro){
        foreach($pro as $k => $products){
       foreach($products as $key => $product)
       {
          if ( $product['settings'][$field] === $value ){
              $json[$key11]['columns'][$key1][$k][$key]['settings']['counter_title']="FOUND IT";
            break;
                  }
       }
        }
        }
}
       return $json;
    }


     $json[15]['columns'][1]['addons'][2]['settings']['number'] ='val1';
     $json[15]['columns'][2]['addons'][2]['settings']['number'] = 'val2';
     $json[15]['columns'][3]['addons'][2]['settings']['number'] = 'val3';
     $json[15]['columns'][4]['addons'][2]['settings']['number'] = 'val4';

        $x= myfunction($json, 'number', 'val3');


     print_r($x);

嘿,谢谢!。但是如果父键改变了…,让我们假设它变为16而不是15,它不会破坏函数吗?它将更新其他内容。@Aramil嘿,我已经更新了答案。但是,如果每件事情都是动态的,那么您需要循环所有这些内容以找到匹配的值。在这种情况下,如果找到相同的键值对,它可能会更新多个键值对。谢谢Suchit!它工作得很好,我通过向函数myfunction添加一个额外变量($json,$field,$value,$val)来修改它,并修改$json[$key11]['columns'][$key1][$k][$key]['settings']['counter_title']=“FOUND It”;通过$json[$key11]['columns'][$key1][$k][$key]['settings']['number']=$val;,这样我就可以使用该函数通过ID键进行检查,比如说$x=myfunction($json,'admin_label','C02',$val);它将用传递的值更新数字键,brilliant bro谢谢!。