Transformasi tatasusunan PHP sangat membakar otak
学习ing
学习ing 2017-06-20 10:07:12
0
5
731
[["10","荔枝好吃"],["11","葡萄好吃"],["18","菠萝好吃"]]
数据库存的是这样的数据 
也就是从数据库读取出来的时候已经变字符串了 "[["10","荔枝好吃"],["11","葡萄好吃"],["18","菠萝好吃"]]"

请问怎样变成  php数组 arr=array("10"=>"荔枝好吃","11"=>"葡萄好吃","18"=>"菠萝好吃");
学习ing
学习ing

membalas semua(5)
世界只因有你
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) "菠萝好吃"
}
*/
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan