PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

[FIXED] How to work json string on foreach loop in php

 April 28, 2022     foreach, loops, php, warnings     No comments   

Issue

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;
}

Solution

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



Answered By - Slava Rozhnev
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing