我有这种数组: -
$result=Array([city] => xyz [name] => Array( [0] => Xyz1 [1] => xyz2 ) [email] => Array ( [0] => xyz1@gmail.com [1] => xyz2@gmail.com ) [phone] => Array ( [0] => 23423423-1 [1] => 23423423-2 ) [address] => Array ( [0] => xyz1 [1] => xyz2 ) [dis] => xyz);
我想要这个
$r1=Array ( [city] => xyz [name] => xyz1 [email] => xyz1@gmail.com [phone] => 23423423-1 [address] => xyz1 [dis] => xyz );
$r2=Array ([city] => xyz [name] => xyz2 [email] => xyz2@gmail.com [phone] => 23423423-2 [address] => xyz2 [city] => xyz );
答案 0 :(得分:0)
这假设您将要处理多个数组,并简单地将您的元素排序为新的格式化数组。
<?php
$results = array(
array(
'city' => 'xyz',
'name' => array( 'Xyz1', 'xyz2' ),
'email' => array( 'xyz1@gmail.com', 'xyz2@gmail.com' ),
'phone' => array( '23423423-1', '23423423-2' ),
'address' => array( 'xyz1', 'xyz2' ),
'dis' => 'xyz'
)
);
$formatted_results = array();
foreach( $results as $result ) {
foreach( array(0,1) as $key ) {
$formatted_results[][$key] = array(
'city' => $result['city'],
'name' => $result['name'][$key],
'email' => $result['email'][$key],
'phone' => $result['phone'][$key],
'address' => $result['address'][$key],
'dis' => $result['dis']
);
}
}
echo "<pre>";
print_r($formatted_results);
这将输出
Array
(
[0] => Array
(
[0] => Array
(
[city] => xyz
[name] => Xyz1
[email] => xyz1@gmail.com
[phone] => 23423423-1
[address] => xyz1
[dis] => xyz
)
)
[1] => Array
(
[1] => Array
(
[city] => xyz
[name] => xyz2
[email] => xyz2@gmail.com
[phone] => 23423423-2
[address] => xyz2
[dis] => xyz
)
)
)
然后,您可以像这样访问新值:
foreach( $formatted_results as $value ) {
$r1 = $value[0];
$r2 = $value[1];
}
或者如果您只有单个数组:
$r1 = $formatted_results[0][0];
$r2 = $formatted_results[0][1];