web-dev-qa-db-fra.com

Comment mettre à jour une valeur unique dans Multi Dimension Post Meta?

Post Meta Data contient certaines valeurs stockées sous forme de tableau multidimensionnel. Je veux mettre à jour certaines de leurs données.

Voici la valeur Post Meta affichée en utilisant <?php the_meta(); ?>

voter: a:1:{s:5:"voter";a:5:
     {s:7:"post_id";s:6:"219585";s:8:"voter_id";s:4:"1540";s:8:"voter_ip";s:13:"182.4
     8.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:1:"1";}}, 

     a:1:{s:5:"voter";a:5:          
     {s:7:"post_id";s:6:"219585";s:8:"voter_id";s:3:"832";s:8:"voter_ip";s:13:"182.48
     .238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:2:"-1";}}, 

     a:1:{s:5:"voter";a:5:
     {s:7:"post_id";s:6:"219585";s:8:"voter_id";s:2:"10";s:8:"voter_ip";s:13:"182.48.
     238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:1:"1";}}

Maintenant, je veux mettre à jour la vote ou voter_ipuser_id est 832 ou 1540. J'ai essayé d'utiliser update_post_meta() mais sa mise à jour complète.

Alors, comment mettre à jour la méta de post de valeur unique stockée dans un tableau multi-dimension?

Mettre à jour:

tableau utilisant the-meta()

voter: a:1:{s:5:"voter";a:5:{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:3:"832";s:8:"voter_ip";s:13:"182.48.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:1:"1";}}, 
 a:1:{s:5:"voter";a:5:{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:4:"1540";s:8:"voter_ip";s:13:"182.48.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:2:"-1";}}, , , 
a:1:{s:5:"voter";a:5:{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:3:"832";s:8:"voter_ip";s:13:"182.48.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:1:"1";}}, 
a:1:{s:5:"voter";a:5:{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:4:"1540";s:8:"voter_ip";s:13:"182.48.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:2:"-1";}}

tableau utilisant print_r()

Array( [0] => Array ( [voter] => Array ( [post_id] => 219585 [voter_id] =>
832 [voter_ip] => 182.48.238.86 [author_id] => 1540 [vote] => 1 ) ) [1] =>
Array ( [voter] => Array ( [post_id] => 219585 [voter_id] => 1540 [voter_ip]
=> 182.48.238.86 [author_id] => 1540 [vote] => -1 ) ) [2] => [3] => [4] =>
Array ( [voter] => Array ( [post_id] => 219585 [voter_id] => 832 [voter_ip]
=> 182.48.238.86 [author_id] => 1540 [vote] => 1 ) ) [5] => Array ( [voter] 
=> Array ( [post_id] => 219585 [voter_id] => 1540 [voter_ip] => 
182.48.238.86 [author_id] => 1540 [vote] => -1 ) )) 
3
Ramesh Pardhi

Vous devrez peut-être désérialiser les données pour obtenir le tableau et le parcourir ainsi:

$userid = 832; // or 1540
$votes = get_post_meta($postid,'voter');
$votes = maybe_unserialize($votes);

if (is_array($votes)) {
    // votes is the array, key is numeric index, vote is subarray
    foreach ($votes as $key => $vote) {
         // subarray values are in another array with key 'voter'
         if ($vote['voter']['voter_id'] == $userid) {
             $votes[$key]['voter']['vote'] = $newvote;
             $votes[$key]['voter']['voter_ip'] = $newvoterip;
         }                 
    }
    update_post_meta($postid,'voter',$voter);
}
1
majick