php - Why the foreach array is not executing as expected and not generating the desired output? What mistake I'm making? -


i've big array titled $allfeeds follows :

array ( [custom_data_cache] => array         ( [answer] => array                 (                     [0] => array                         (                             [answer_id] => 289                             [poll_id] => 115                             [answer] => let's meet                             [total_votes] => 0                             [ordering] => 1                             [vote_percentage] => 0                         )                      [1] => array                         (                             [answer_id] => 290                             [poll_id] => 115                             [answer] => let's plan sometime later                             [total_votes] => 0                             [ordering] => 2                             [vote_percentage] => 0                         )                  )          ) ) 

i want make value in inner ['answer'] key, not outer ['answer'] key blank(in above array 2 such elements exist) i'm not able it.

following code tried array not changing @ all.

foreach ($allfeeds['custom_data_cache']['answer'] $key => $value) {       $key[$value]['answer'] = '';               } print_r($allfeeds); 

again same array prints. expected output follows :

array ( [custom_data_cache] => array         ( [answer] => array                 (                     [0] => array                         (                             [answer_id] => 289                             [poll_id] => 115                             [answer] =>                              [total_votes] => 0                             [ordering] => 1                             [vote_percentage] => 0                         )                      [1] => array                         (                             [answer_id] => 290                             [poll_id] => 115                             [answer] =>                              [total_votes] => 0                             [ordering] => 2                             [vote_percentage] => 0                         )                  )          ) ) 

please me correcting mistake i'm making in array manipulation.

your local (for loop) variable $key hold index , not reference element (array) under index , $value holds copy of element (array). assignment $key[$value]['answer'] = ''; wrong on many levels (syntactically semantically). have error_reporting turned off way?
change

foreach ($allfeeds['custom_data_cache']['answer'] $key => $value) {     $key[$value]['answer'] = '';           } 

to

foreach ($allfeeds['custom_data_cache']['answer'] $key => $value) {     $allfeeds['custom_data_cache']['answer'][$key]['answer'] = '';           } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -