-4

Hi i have an array like that

Array ( [0] => Array ( [t_name] => London, Heathrow ) 
        [1] => Array ( [t_name]   => Lilongwe, Malawi ) 
        [2] => Array ( [t_name] => Harare,Zimbabwe ) 
        [3] => Array ( [t_name] => Lahore,Pakistan ) 
        [4] => Array ( [t_name] => Accra, Ghana ) 
        [5] => Array ( [t_name] => Entebbe, Uganda ) 
        [6] => Array ( [t_name] => Nairobi ) 
        [7] => Array ( [t_name] => Abuja, Nigeria )
)

and I want it to be converted to be used in JavaScript. its supposed to look like that I have tried json_encode() but it converts it something like that

[{"t_name":"London, Heathrow"},{"t_name":"Lilongwe, Malawi"},
{"t_name":"Harare,Zimbabwe"},{"t_name":"Lahore,Pakistan"},{"t_name":"Accra, Ghana"},
{"t_name":"Entebbe, Uganda"},{"t_name":"Nairobi"},{"t_name":"Abuja, Nigeria"}]

but i want it to be converted like that

[
 "London, Heathrow",
 "Lilongwe, Malawi",
 "Harare,Zimbabwe",
 "Lahore,Pakistan",
 "Accra, Ghana",
 "Entebbe, Uganda",
 "Nairobi",
 "Abuja, Nigeria",
 "Lagos, Nigeria"
 ]
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
Zohaib
  • 22
  • 5
  • 2
    If you want a different structure in the output, then adjust your input accordingly ... – Sirko Jul 07 '15 at 10:16
  • how to do that i am using code igniter and the getting the results from database and this is how it returns me the array and when i convert it, it does not work accordingly. – Zohaib Jul 07 '15 at 10:19

2 Answers2

4

Before json_encode your array you need to get first the values of that single column which is t_name using array_column and echo the resultant array. Try it as

$arr = Array (Array ( 't_name' => 'London, Heathrow' ),Array ( 't_name'   => 'Lilongwe, Malawi' ),Array ( 't_name' => 'Harare,Zimbabwe' ),Array (   't_name' => 'Lahore,Pakistan' ),Array ( 't_name' => 'Accra, Ghana' ),Array ( 't_name' => 'Entebbe, Uganda' ),Array ( 't_name' => 'Nairobi' ),Array ( 't_name' => 'Abuja, Nigeria' ));
echo json_encode(array_column($arr,'t_name'));

Fiddle

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • i am getting the result array from the database by using the codeigniters own methods and above is what it returns – Zohaib Jul 07 '15 at 10:20
  • You might be storing that array within variable just place that variable within as `echo json_encode(array_column($your_array,'t_name'));` and that's it if the value that you have posted within your question looks like that then it'll surely work – Narendrasingh Sisodia Jul 07 '15 at 10:22
  • okay both your and @Barmar way work gives me the arrays but there are spaces in them now how to remove them i am basically want to use them in auto-complete input feild. – Zohaib Jul 07 '15 at 10:29
  • Can you please show some text where you were getting space – Narendrasingh Sisodia Jul 07 '15 at 10:33
  • its done i have managed it. BTW how do i delete my post. ? – Zohaib Jul 07 '15 at 10:39
  • You can't for explanation [how-can-i-delete-my-post-on-stack-overflow](http://meta.stackexchange.com/questions/25088/how-can-i-delete-my-post-on-stack-overflow) . _(FYI:You can accept one of these answer or upvote an answer which really helped you. Thank You)_ – Narendrasingh Sisodia Jul 07 '15 at 10:42
0

Use a loop to extract the names into a simple array:

$new_array = array();
foreach ($array as $obj) {
    $new_array[] = str_replace(' ', '', $obj['t_name']);
}
echo json_encode($new_array);
Barmar
  • 741,623
  • 53
  • 500
  • 612