3

this way how i get array from db:

$sql = "SELECT * FROM table_name";
$query = mysqli_query($conn, $sql);

$db_array = array();
// start fetch for words
if (mysqli_num_rows($query) > 0){
    while ($row = mysqli_fetch_assoc($query)){
        $db_array[] = $row;
    }
}

My array looked like this:

Array
(
    [cat cute] => animal#cute animal
    [cat women] => film
)

How to add '{' and '}' to all values? I wish i can get new array like this:

Array
(
    [cat cute] => {animal#cute animal}
    [cat women] => {film}
)

It's hard for me, i am new in php development.

3
  • This syntax doesn't look a lot like PHP. Commented Mar 6, 2017 at 10:49
  • Can u tell why you need { and } around your values, maybe you using template engine or js framework? Commented Mar 6, 2017 at 10:57
  • @Nerfair i have to deal with synonim Commented Mar 6, 2017 at 11:01

2 Answers 2

3

Try this,

$arr =  array
(
    'cat cute' => 'animal#cute animal',
    'cat women' => 'film'
);
array_walk($arr, function(&$item){
   $item = '{'.$item.'}'; 
});
print_r($arr);

Here is the link for array_walk()

Sign up to request clarification or add additional context in comments.

Comments

2

Since you want a new array, try this:

<?php
array_map(function($value)
{
    return '{' . $value . '}';
}, $arr);

array_walk() in rahul_m's answer modifies your array, while array_map() creates a new one.

1 Comment

Thank you for the way you answer in detail based from my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.