[["10","荔枝好吃"],["11","葡萄好吃"],["18","菠萝好吃"]] 数据库存的是这样的数据 也就是从数据库读取出来的时候已经变字符串了 "[["10","荔枝好吃"],["11","葡萄好吃"],["18","菠萝好吃"]]"
请问怎样变成 php数组 arr=array("10"=>"荔枝好吃","11"=>"葡萄好吃","18"=>"菠萝好吃");
var_dump(json_decode('[["10","荔枝好吃"],["11","葡萄好吃"],["18","菠萝好吃"]]'));
Anda tahu apa yang perlu dilakukan seterusnya
Hanya keluarkan petikan berganda$arr = trim($str,'"');$res = $arr[0];
$arr = [["10","荔枝好吃"],["11","葡萄好吃"],["18","菠萝好吃"]]; echo "<pre>"; print_r(array_column($arr, 1, 0));
<?php $array = [["10","荔枝好吃"],["11","葡萄好吃"],["18","菠萝好吃"]]; $data = array(); foreach ($array as $item) { $data[$item[0]] = $item[1]; } echo '<pre>'; var_dump($data);
$str = '[["10","荔枝好吃"],["11","葡萄好吃"],["18","菠萝好吃"]]';// 字符串 $arr = array_reduce(json_decode($str,true),function($c,$v){$c[$v[0]]=$v[1];return $c;},[]); var_dump($arr); // 对应数组 /* array(3) { [10]=> string(12) "荔枝好吃" [11]=> string(12) "葡萄好吃" [18]=> string(12) "菠萝好吃" } */
Anda tahu apa yang perlu dilakukan seterusnya
Hanya keluarkan petikan berganda
$arr = trim($str,'"');
$res = $arr[0];