I'm trying to make a foreach loop from string value which comes from a JSON value like:
string '["userdomain.ltd"], ["test.com"]'
I want to make a foreach loop and echo URL under the loop
I have tried using this but it returns PHP Warning: Invalid argument supplied for foreach()
foreach( $device_url as $url ){
echo $url;
}
Your input is a string, so we need transform it to array for iterate:
<?php
$str = '["userdomain.ltd"], ["test.com"]';
$arr = explode(',', $str);
foreach ($arr as $el) {
echo json_decode($el)[0];
echo "\n";
}
Here you can try working code: PHPize.online
My guess is, you did not decode you string before passing it to your loop. And you can't loop on a string. You should use the json_decode
function to decode it first, and then use your loop.
<?php
$input = '{"key": "value"}';
$decodedIntput = json_decode($input);
foreach( $decodedIntput as $url ){
echo $url;
}
I did not used your json because it did not look valid.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.