Home > Backend Development > PHP Tutorial > 这个数组怎么拆?

这个数组怎么拆?

WBOY
Release: 2016-06-06 20:43:25
Original
1223 people have browsed it

<code>Array ( [0] => Array ( [country] => 越南 [dausum] => 300 ) [1] => Array ( [country] => 中国 [dausum] => 500 ) [2] => Array ( [country] => 香港 [dausum] => 600 ) )
</code>
Copy after login
Copy after login

这个大数组拆成

<code> $country=('越南','中国','香港');
 $data=('越南'=>'300','中国'=>300,'香港'=>'600);
</code>
Copy after login
Copy after login

回复内容:

<code>Array ( [0] => Array ( [country] => 越南 [dausum] => 300 ) [1] => Array ( [country] => 中国 [dausum] => 500 ) [2] => Array ( [country] => 香港 [dausum] => 600 ) )
</code>
Copy after login
Copy after login

这个大数组拆成

<code> $country=('越南','中国','香港');
 $data=('越南'=>'300','中国'=>300,'香港'=>'600);
</code>
Copy after login
Copy after login

<code class="lang-php">$arr = 
[
  ['country' => '越南', 'dausum' => 300],
  ['country' => '中国', 'dausum' => 500],
  ['country' => '香港', 'dausum' => 600]
];

$country = $data = array();
foreach($arr as $elem)
{
  $country[] = $elem['country'];
  $data[$elem['country']] = $elem['dausum'];
}
</code>
Copy after login

<code>$arr = 
[
  ['country' => '越南', 'dausum' => 300],
  ['country' => '中国', 'dausum' => 500],
  ['country' => '香港', 'dausum' => 600]
];

$country = array_column($arr, 'country');
$data = array_combine($country, array_column($arr, 'dausum'));
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template