简体   繁体   English

从数组中检索值

[英]Retrieve value from array

While retrieving values from array without index in PHP I am getting nothing. 从PHP中没有索引的数组中检索值时,我什么也没得到。 From my database value is stored like ["SCHOOL"] and I want to get just SCHOOL from this. 从我的数据库中存储的值像[“ SCHOOL”]一样,我只想从中获取SCHOOL。 It's not treated as array instead it's treated as a string. 它不被视为数组,而是被视为字符串。 What will be the solution if want to treat this as array and get value of that array. 如果要将其视为数组并获取该数组的值,将是解决方案。

My Code is as follows : 我的代码如下:

$stream_arr = $res_tbl['streams']; $ stream_arr = $ res_tbl ['streams']; which gives result as ["SCHOOL"] 结果为[“ SCHOOL”]

and I want just SCHOOL from this. 我只想要这个

I am using code as $stream = $stream_arr[0]; 我使用的代码为$ stream = $ stream_arr [0]; and I get '[' this as result. 结果是“ [”。

If I assign manual value to $stream_arr = ["SCHOOL"], above code works and returns SCHOOL as expected. 如果我将手动值分配给$ stream_arr = [“ SCHOOL”],则上述代码可以正常工作并按预期返回SCHOOL。 But for results from my database is not working where result is same as the one I manually assigned. 但是由于我的数据库中的结果不起作用,结果与我手动分配的结果相同。

To eliminate braces from the string you can use below code. 要从字符串中消除括号,您可以使用以下代码。

$stream_arr = '["SCHOOL"]';
$temp=explode('"',$stream_arr);
$val=$temp[1];
echo $val; //it will dispaly 'SCHOOL'.

But check the insert query(from the place you store data into db) that why it is storing with braces? 但是检查插入查询(从您将数据存储到db的位置)中为什么用大括号存储? if it is done by you as required then proceed else first fix that issue instead of eliminating braces in php code. 如果它是由您根据需要完成的,则继续进行其他操作,首先解决该问题,而不要消除php代码中的花括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM