我有一个名为$ genderAge的字符串值数组,在回显时看起来像这样: -
F,0-4,327607378
M,0-4,392700793
F,15-24,887438943
M,15-24,525132614
M,25-34,621410857
因此对于上面的数组,$ ageRange [0]为“F”。 $ ageRange [1]是“0-4”,依此类推。
我想将它分成两个数组,$ male_array和带有键值对的$ female_array。关键应该是年龄范围,价值应该是成本。
$male_array = [];
$female_array = [];
for ($i = 0; $i < count($genderAge); $i++) {
if ($genderAge[i] == 'M') {
$male_array[$genderAge[i+1]] = $genderAge[i+2];
}
elseif ($genderAge[i] == 'F') {
$female_array[$genderAge[i+1]] = $genderAge[i+2];
}
}
foreach($male_array as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
以上内容并未回应任何内容。我想让它回应一下:
Key = 0-4,Value = 392700793
Key = 15-25,Value = 525132614
Key = 25-34,Value = 621410857
答案 0 :(得分:1)
您的$genderAge
应该是此类数组,以便您可以在单独的变量中执行值的存储,并且可以将其用于进一步的目的。
建议数组:
$genderAge = array("F", "0-4", "327607378", "M", "0-4", "392700793", "F", "15-24", "887438943","M", "15-24", "525132614","M","25-34", "621410857");
对阵列进行循环操作以保存值。
在这里,你必须使用
$i
增量运算符+1。您不得单独使用i
,因为您已使用过。
PHP代码:
<?php
$genderAge = array("F", "0-4", "327607378", "M", "0-4", "392700793", "F", "15-24", "887438943","M", "15-24", "525132614","M","25-34", "621410857");
$male_array = [];
$female_array = [];
for ($i = 0; $i < count($genderAge); $i++) {
if ($genderAge[$i] == 'M') {
$male_array[$genderAge[$i+1]] = $genderAge[$i+2];
}
elseif ($genderAge[$i] == 'F') {
$female_array[$genderAge[$i+1]] = $genderAge[$i+2];
}
}
echo 'M Values'.'<br>';
foreach($male_array as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
echo '<br>';
echo 'F Values'.'<br>';
foreach($female_array as $y=> $y_value) {
echo "Key=" . $y . ", Value=" . $y_value;
echo "<br>";
}
echo '<br>';
?>
<强>输出:强>
M Values
Key=0-4, Value=392700793
Key=15-24, Value=525132614
Key=25-34, Value=621410857
F Values
Key=0-4, Value=327607378
Key=15-24, Value=887438943
答案 1 :(得分:0)
如果您的阵列经常是3对的组合:性别,年龄范围,成本,您可能希望在循环中增加每3对。它将为您节省相对于数组索引之外的错误:
<?php
$GENDER_MALE = 'M';
$GENDER_FEMALE = 'F';
$genderAge = ['F', '0-4', '327607378', 'M', '0-4', '392700793', 'F', '15-24', '887438943', 'M', '15-24', '525132614', 'M', '25-34', '621410857'];
$male_array = [];
$female_array = [];
$size = count($genderAge);
# Format the male/female array
for( $i = 0; $i < $size; $i += 3 ) {
$gender = $genderAge[$i]; // M or F
$ageRange = $genderAge[$i + 1]; // 0-4, 14-18, ...
$cost = $genderAge[$i + 2]; // 897726, 10927, ...
if( $gender == $GENDER_MALE ) {
$male_array[$ageRange] = $cost;
}
else if( $gender == $GENDER_FEMALE ) {
$female_array[$ageRange] = $cost;
}
}
# Print the male/female formatted array
foreach( $male_array as $key => $value ) {
echo "Age range : $key - cost = $value <br />";
}
?>
请注意,在第一个for
循环中$i += 3
的使用可以理解为:
在我的阵列上循环每3个项目
这样,您可以将索引+ n相对于您需要的对($i + 1
应用于年龄范围,等等)。
答案 2 :(得分:0)
首先,您可能希望获得所有这些键值对,包括关联数组中的Sex。以下是使用
for(){}
循环完成的方法:
<?php
$arr = array("F"," 0-4", "327607378",
"M", "0-4", "392700793",
"F", "15-24", "887438943",
"M", "15-24", "525132614",
"M", "25-34", "621410857");
$all = array();
for($a=0, $b=1, $c=2; $a <= (count($arr)-3); $a+=3, $b+=3, $c+=3){
$temp = array(
"sex" => $arr[$a],
"key" => $arr[$b],
"value" => $arr[$c]
);
$all[] = $temp;
}
var_dump($all);
对
var_dump($all);
的调用产生:
array (size=5)
0 =>
array (size=3)
'sex' => string 'F' (length=1)
'key' => string ' 0-4' (length=4)
'value' => string '327607378' (length=9)
1 =>
array (size=3)
'sex' => string 'M' (length=1)
'key' => string '0-4' (length=3)
'value' => string '392700793' (length=9)
2 =>
array (size=3)
'sex' => string 'F' (length=1)
'key' => string '15-24' (length=5)
'value' => string '887438943' (length=9)
3 =>
array (size=3)
'sex' => string 'M' (length=1)
'key' => string '15-24' (length=5)
'value' => string '525132614' (length=9)
4 =>
array (size=3)
'sex' => string 'M' (length=1)
'key' => string '25-34' (length=5)
'value' => string '621410857' (length=9)
现在,为了回应一些数据,你可以这样做:
<?php
foreach($all as $iKey=>$arrKv){
echo "sex : " . $arrKv["sex"] . "<br />" . PHP_EOL;
echo "Key : " . $arrKv["key"] . "<br />" . PHP_EOL;
echo "Value : " . $arrKv["value"] . "<br /><br />" . PHP_EOL;
}
上面循环中每个
echo()
的复合结果产生:
sex : F
Key : 0-4
Value : 327607378
sex : M
Key : 0-4
Value : 392700793
sex : F
Key : 15-24
Value : 887438943
sex : M
Key : 15-24
Value : 525132614
sex : M
Key : 25-34
Value : 621410857