dounao1856 2014-01-29 10:53
浏览 163
已采纳

使用方括号并在php中分配变量

Hello guys I have seen a source like

$something = $sql['value']

I have searched a lot about it and I found that it's from arrays. But I didnt understand the exact meaning.

For ex ..

$people = [


 'Susan' => [
    'Age' => 24,
'Phone' => '555-123-4567'
],

  'Jack' => [


  'Age' => 27,


  'Phone' => '555-9876-5432'

  ]
];

echo $people['Jack']['Age']; // 27

My question is that can we write a code like this ..

if(!empty($people)

    $something = $people['a value']

I Just need to know how can we declare a variable and give a value in square brackets ..ny help would be appreciated. Thanks. :)

  • 写回答

4条回答 默认 最新

  • dongtiannai0654 2014-01-29 10:55
    关注

    If you are using $something = $people['a value'] means you are assigning a value of $people array having an index of a value

    So you don't have that and so it will throw you undefined index error.

    What you are using is a nested associative array and you have to output using something like

    echo $people['Jack']['Age'];
    

    As you wanted a brief example, say you have an array like

    $people = array('name'=>'Jack');
    

    Now, when you want to store the name in a variable, you use

    $store_name = $people['name'];
    
    echo $store_name; //echoes Jack
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?