mysql - Search Php array for string and extract adjacent text
Get the solution ↓↓↓Solution:
Assuming Marc B is right and it's valid JSON in your params field, you should be able to do something like this:
while( $row = mysql_fetch_array($result)) {
$user_params = json_decode($row['params'], true);
$arr = "{$arr}\n
<table border=1 cellspacing=1 cellpadding=2>
<col width=200px />
<col width=300px />
<col width=150px />
<tr>
<td>{$row['Name']}</td>
<td>{$row['Email']}</td>
<td>{$user_params['phone']}</td>
</tr>
</table>
\n</BODY>\n</HTML>\n";
}
return $arr;
See the PHP Doc for json_decode
Answer
Solution:
It's a json string, use json_decode to decode it to array and then you can check if phone variable isset and access it like in normal array.
Answer
Solution:
EDIT: based on the additional code you posted, the row is an array returned from mysql, but the params field is json. Code below is updated:
Seems like you have an array you can just look up stuff from. I'm not sure what code came before. If it's' already an array great, if not use json_decode() to turn string into an array. Then:
function getPhone($row) {
$params = json_decode($row[5], true);
return $params["phone"];
}
I might point out that mysql is deprecated and you should be using mysqli (or PDO). Also, the hard-coded array index [5] will get you in trouble if your query changes, so good practice is to get an associative array back and name the fields instead of numbering them. See http://us3.php.net/mysqli_fetch_assoc.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: undefined array key php
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.