我有一个像这样的数组
Array
(
[solar] => Array
(
[0] => sun
[1] => moon
)
[SolarGraph] => Array
(
[0] => Array
(
[y] => 89
[url] => sun
)
[1] => Array
(
[y] => 57
[url] => moon
)
)
)
但我需要更改y和url名称,如下所示:
Array
(
[solar] => Array
(
[0] => sun
[1] => moon
)
[SolarGraph] => Array
(
[0] => Array
(
[solarvalue] => 89
[solarname] => sun
)
[1] => Array
(
[solarvalue] => 57
[solarname] => moon
)
)
)
我试图改变,但我只得到一个数组......继承人代码
foreach($Rows as $key=>$val){
$aUse[] = $val['rt_solargraph'];
foreach($aUse as $key => $value)
{
$aUser['SolarGraph'][$key]['SolarValue'] = $value[$key]['y'];
$aUser['SolarGraph'][$key]['SolarName'] = $value[$key]['url'];
}
}
请帮帮我修复此问题,因为我是php和array的新手。我不知道如何迭代内部数组中的元素。
答案 0 :(得分:3)
您可以这样使用:
<?php
use Symfony\Component\HttpFoundation\Request;
$login = $app['controllers_factory'];
$login->post('/apiv1/user/login', function (Request $request) use ($login) {
// ^^^^^^^^
// this should be $app, not $login as this function is using the container
$userinfo = array(
'email' => $request->request->get('email'),
'mode' => $request->request->get('mode'),
'password' => $request->request->get('password'),
);
$passwordcoding = sha1($userinfo['email']."66643asd");
$emailverification = "SELECT email,password FROM user WHERE email='".$userinfo['email']."'";
// here you're using the $login as the container and it's a controller collection, so in the signature use the container not the $login
// vvvvvvvvvvvv
$selectemail = $login['db']->$fetchAll('SELECT * FROM user');
var_dump($emailverification);
});
return $login;
<强>结果:强>
// initialize array
$newArr = array();
foreach ($yourArr as $key => $value) {
// if $value is array , you can also add SolarGraph key check.
if(is_array($value) && $key == 'SolarGraph'){
foreach ($value as $mykey => $finalVal) {
// assign values to $newArr
$newArr[$key][$mykey]['solarvalue'] = $finalVal['y'];
$newArr[$key][$mykey]['solarname'] = $finalVal['url'];
}
}
else{
// if not is array store as same
$newArr[$key] = $value;
}
}
echo "<pre>";
print_r($newArr);
答案 1 :(得分:2)
试试这个。
<?php
$solar = array('sun','moon');
$SolarGraph = array(array('y' => 89,'url'=>'sun' ),array('y' => 57,'url'=>'moon' ));
$Rows = array('solar'=>$solar, 'SolarGraph'=>$SolarGraph );
echo '<pre>';
$newArray = [];
foreach ($Rows as $key => $value) {
if($key =='SolarGraph'){
foreach ($value as $lkey => $finalvalue) {
$newArray[$key][$lkey]['solarvalue'] = $finalvalue['y'];
$newArray[$key][$lkey]['solarname'] = $finalvalue['url'];
}
}else{
$newArray[$key] = $value;
}
}
print_r($newArray);
echo '</pre>';
?>