Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从PHP数组中获取数据_Php_Arrays - Fatal编程技术网

如何从PHP数组中获取数据

如何从PHP数组中获取数据,php,arrays,Php,Arrays,我必须回显数组中的特定值。我们有2个字段,我只需要获取名称值 我的密码是 $fields = array( 'name' => $name, 'age' => $age, ); 我只需要在回显结果中获取名称。我尝试了以下方法,但它显示了数组 foreach ($fields as $key => $value) { echo $value; } 如果$name和$age都是数组,请在$fields['name']上使用循环 试试这个: foreach

我必须回显数组中的特定值。我们有2个字段,我只需要获取名称值

我的密码是

$fields = array(
    'name' => $name,
    'age' => $age,
);
我只需要在回显结果中获取名称。我尝试了以下方法,但它显示了数组

foreach ($fields as $key => $value) {
    echo $value;
}

如果
$name
$age
都是数组,请在
$fields['name']
上使用
循环

试试这个:

foreach ($fields['name'] as $key => $value) {
    echo $value;
}

我将假定您的
$name
$age
数组长度相同,并且在相同的序列中包含彼此相关的信息

在这种情况下,你最好这样组织

for($i=0;$i<count($names);$i++){
    $fields[$i]['name'] = $names[$i];
    $fields[$i]['age'] = $age[$i];}

这将让您知道姓名值是否多于年龄值,反之亦然

,以及
打印什么($value)
为您提供了什么?存储在
$name
age
中的是什么?这两个都是数组吗?是的,这两个都是数组…@Joe那么您只需要使用
$fields[“name”]
来循环值。
foreach($fields as $field){
    foreach($field as $key => $value){
        echo $key. ' = ' .$value. '<br/>';}}
if(count($names)!=(count($age))){
    echo 'error';}
else{
    *insert rest of code*}