0

I'm trying to parse data from an TV Shows API, but I'm having trouble figuring out how to get the index numbers.

Example:

{
"data": {
    "0": {
        "1": {
            "param1": "value1", 
            "param2": "warning", 
        }
    }, 
    "1": {
        "1": {
            "param1": "value1", 
            "param2": "value2", 
        }, 
        "2": {
            "param1": "value1", 
            "param2": "warning", 
        }, 
        "3": {
            "param1": "value1", 
            "param2": "warning", 
        }, 
    }
}
}

The "params" and "values" I'm able to get just fine, but I can't 'echo' the index numbers.

This is how I'm trying to do it:

$string = file_get_contents(api);
$json_a = json_decode($string, true);
foreach ($json_a['data'] as $seasons) {
    foreach ($seasons as $episodes) {
            if ($episodes['param2']=="warning") {
            // echo index number of season
            // echo index number of episode

            }
    }
}

I believe I've read that using foreach I'm not able to get the index numbers? If so, what would be the solution?

I tried 'manually' incrementing variables for season number and episodes, but index numbers may vary. They may not be sequential and some start with index "1" instead of "0".

1
  • Don't forget to accept the answer here that most helped you - seems to have been answered by several people. Commented Sep 25, 2012 at 14:45

4 Answers 4

1

Use foreach (array_expression as $key => $value) instead of just foreach (array_expression as $value). The $key is what you're missing.

PHP: foreach

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much everyone, I was indeed missing the '$key' parameter in foreach. It's working perfectly now :)
1

You don't need a counter, just add the key in your definition of the foreach

eg.

$test = array('foo' => 'bar');

foreach($test as $key => $value) {
    echo $key . "\n";
    echo $value;
}

Outputs

foo
bar

http://php.net/manual/en/control-structures.foreach.php

Comments

0

Try this:

foreach ($json_a['data'] as $index1 => $seasons) {}

and similarly:

foreach ($seasons as $index2 => $episodes) {}

Comments

0

i couldnt encode your json but,

used an array like:

array
  0 => 
    array
      1 => 
        array
          'param1' => string 'value1' (length=6)
          'param2' => string 'warning' (length=7)
  1 => 
    array
      1 => 
        array
          'param1' => string 'value1' (length=6)
          'param2' => string 'warning' (length=7)
      2 => 
        array
          'param1' => string 'value1' (length=6)
          'param2' => string 'value2' (length=6)

and here is the code:

$a[0][1] = array("param1"=>"value1","param2"=>"warning");
$a[1][1] = array("param1"=>"value1","param2"=>"warning");
$a[1][2] = array("param1"=>"value1","param2"=>"value2");

foreach($a as $key=>$val)
{
    foreach($val as $skey => $sval)
    {
        if($sval['param2']=="warning")
            echo $key."-".$skey."<br>";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.