Jump to content

foreach loop - multidimensional array


Go to solution Solved by requinix,

Recommended Posts

Hi guys,

How can I modify the code below to only echo $val where  "num_owned" is greater than 0?

Unfortunately, “if (empty($val))”  echos the values where “"is_locked": true” is present as one which is something I’d like to avoid.

 

json data:

{
	"user_cards": {
		"1": {
			"num_owned": "9",
			"num_used": "0"
		},
		"2": {
			"num_owned": "0",
			"num_used": "0"
		},
		"3": {
			"num_owned": "0",
			"num_used": "0",
            "is_locked": true
		},
		"4": {
			"num_owned": "8",
			"num_used": "0"
		},
		"5": {
			"num_owned": "11",
			"num_used": "0"
		}	}
}

PHP script

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);

include_once('db.txt');

$json = file_get_contents("TU.json");

$json = json_decode($json, true);



 foreach ($json['user_cards'] as $row => $v)
        {
         foreach ($v as $key => $val){
             
             if (empty($val))   {continue;}
             
             
 echo $d[$row][0] . "-" . $d[$row][1] . "(" . $val . ")" . PHP_EOL;
             echo "<br>";

         }

       }   
?>
Link to comment
https://forums.phpfreaks.com/topic/303339-foreach-loop-multidimensional-array/
Share on other sites

  • Solution

Seems you're a little confused here. $row is the keys in the user_cards object ("1", "2", etc.), $v is the corresponding object; $key is "num_owned/num_used/is_locked", and $val is the corresponding value.

 

So looking at $val doesn't make sense.

 

Get rid of the inner foreach loop. It gets you $key and $val and those are worthless. Instead, decide what to do with $v if $v['num_owned'] > 0.

Hi, thanks for taking the time to reply – I appreciate it but I’m still confused so I thought it best to describe what I’m after in full as I think it’ll be more beneficial.

 

I want to produce an output that looks like this:

 

Infantry-1(9) 
Bazooka Marine-1(8) 
Medic-1(11) 

 

But the code above produces

:

Infantry-1(9) 
Infantry-3(1) 
Bazooka Marine-1(8) 
Medic-1(11) 

 

I don’t want to display “Infantry-3(1)” because the value in "num_owned” is 0. 

 

I’m substituting the keys in the user_cards object ("1", "2", etc.) with the values in db.txt and then appending the "num_owned” value in brackets.

 

db.txt

<?php
$d[1] = array("Infantry",1);
$d[2] = array("Infantry",2);
$d[3] = array("Infantry",3);
$d[4] = array("Bazooka Marine",1);
$d[350] = array("Bazooka Marine",2);
$d[351] = array("Bazooka Marine",3);
$d[5] = array("Medic",1);
?>

Do I still need to get rid of the inner for loop?

Yes. Yes you do. Did you understand what I said? Do you know what those two loops are doing? I'm confused too because if you knew what that inner foreach did then I'd expect you would understand why you need to remove it.

 

Try what I said, and if it still doesn't work then post your code and explain what's happening.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.