0

i have an array that looks like that :

Array
(
    [0] => Array
        (
            [id] => Classify
            [classification] => Array
                (
                    [0] => Array
                        (
                            [class] => negative
                            [p] => 0.0650294
                        )

                    [1] => Array
                        (
                            [class] => positive
                            [p] => 0.934971
                        )

                )

            [text] => Understanding if a text is positive or negative is easy for humans, but a lot harder for computers. We can “read between the lines”, get jokes and identify irony. Computers aren’t quite there yet but the gap is quickly closing in.
        )

)

How can i access the positive & negative value?

1
  • yeah i know it's answer from an API so i can't do much about that Commented Mar 6, 2012 at 13:54

5 Answers 5

2

If you have multiple elements in the top-most array, you'd have to parse each element:

foreach($item in $array)
    foreach($classification in $item['classification'])
        echo $classification['class'];
Sign up to request clarification or add additional context in comments.

1 Comment

In that case, you can remove the first line, and just replace the second line, with this one: foreach($classification in $array[0]['classification'])
2

$array[0]['classification'][0]['p'] and $array[0]['classification'][1]['p'] to access the double values.

$array[0]['classification'][0]['class'] and $array[0]['classification'][1]['class'] to access the 'positive' and 'negative' strings

2 Comments

It's an array, not an object.
Yeah. Thats why I changed it.
2

The outer array is numerically indexed (having one element, an associative array), and those two values are components of the array elements contained in the array classification, which is also numerically indexed, whose elements are associative arrays.

// By iteration, assuming an unknown number of classifications
foreach ($array[0]['classification'] as $classification) {    
  echo $classification['class'];
  echo $classification['p'];
}

// Or direct access to the elements
echo $array[0]['classification'][0]['class']; // negative
echo $array[0]['classification'][0]['p']; // 0.0650294
echo $array[0]['classification'][1]['class']; // positive
echo $array[0]['classification'][1]['p']; // 0.934971

Comments

1
print $arr[0]['classification'][0]['class'];
print $arr[0]['classification'][1]['class'];

Comments

1
print $array[0]['classification'][0]['class'];
print $array[0]['classification'][1]['class'];

provided it's always the same amount of classifications...

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.