php - how to replace values in array with values from another array? -
i'm fetching array:
$sql = select id, name, state table order name $result = mysqli_query($conn, $sql); $rows = array(); $dict = ["a","b","c"]; while ($row = mysqli_fetch_array($result)) { //replace state value here before next line $rows[] = $row; }
values in state field can 0,1,2. want replace value in key=state of $row
value $dict
0=>a, 1=>b, 2=>c. value in state field equals position of $dict array.
ex. if $row=["id"=>"1","name"=>"john", "state"=>"1"]
new $row=["id"=>"1","name"=>"john", "state"=>"b"]
you can use that:
$dict = array("a","b","c"); $i = 0; while ($row = mysqli_fetch_array($result)) { $rows[$i]['id'] = $row['id']; $rows[$i]['name'] = $row['name']; $rows[$i]['state'] = $dict[$value['state']]; $i++; }
if $dict
index fixed 3 index work perfectly.
explanation:
$dict[$value['state']]
value per index value.
like if $value['state'] == 1
"b" $dict
array.
for safe hand can use that:
$rows[$i]['state'] = (isset($dict[$value['state']]) ? $dict[$value['state']] : ''); // if not set empty else want.
Comments
Post a Comment