tags:

views:

65

answers:

2

Can you please show me an example in php on how to grab specific information in a loop of some kind, I want to access [language] and [answer] => [question] => everytime it is there. Thanks

Array    (

    [0] => stdClass Object
        (
            [type] => text
            [cue] => stdClass Object
                (
                    [type] => text
                    [text] => pri
                    [sound] => 
                    [language] => eo
                    [part_of_speech] => Preposition
                )

            [responses] => Array
                (
                    [0] => stdClass Object
                        (
                            [type] => meaning
                            [text] => about, concerning
                            [language] => en
                            [quizzes] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [type] => multiple_choice
                                            [answer] => pri
                                            [question] => about, concerning
                                        )

                                )

                        )

                )

            [author] => stdClass Object
                (
                    [profile] => stdClass Object
                        (
                            [name] => Tomo
                            [profile_url] => http://smart.fm/users/yubizume
                            [icon_url] => http://assets2.smart.fm/assets/users/yubizume/e1b0e7f8_medium.jpg
                        )

                    [username] => yubizume
                )

            [language] => eo
            [dc_creator] => yubizume
            [href] => http://smart.fm/items/830187
            [id] => 830187
        )

    [1] => stdClass Object
        (
            [type] => text
            [cue] => stdClass Object
                (
                    [type] => text
                    [text] => pri
                    [sound] => 
                    [language] => eo
                    [part_of_speech] => Preposition
                )

            [responses] => Array
                (
                    [0] => stdClass Object
                        (
                            [type] => meaning
                            [text] => about, concerning (prep)
                            [sound] => http://assets0.smart.fm/assets/generated_sounds/2009111923/a857356fb588022411fa21371a96e494.mp3
                            [language] => en
                            [quizzes] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [type] => multiple_choice
                                            [answer] => pri
                                            [question] => about, concerning (prep)
                                        )

                                )

                        )

                )

            [author] => stdClass Object
                (
                    [profile] => stdClass Object
                        (
                            [name] => AriadneAranea
                            [profile_url] => http://smart.fm/users/AriadneAranea
                            [icon_url] => http://a1.twimg.com/profile_images/489977042/2009-01_-_New_Year_at_Slimbridge_02_bigger.jpg
                        )

                    [username] => AriadneAranea
                )

            [language] => eo
            [dc_creator] => AriadneAranea
            [href] => http://smart.fm/items/1773996
            [id] => 1773996
        )
+1  A: 
// $objects is the array with all those objects
foreach($objects as $object)
{
  echo $object->cue->language; // language

  foreach($objet->responses as $response)
  {
    // if there are no quizzes, we skip the part below
    // we skip it because $object->quizzes will produce a warning or a notice
    // if "quizess" is not a member of the $object
    if(!isset($object->quizzes))
    {
      continue;
    }

    // quizess
    foreach($response->quizzes as $quiz)
    {
      echo $quiz->question; // question
      echo $quiz->answer; // answer
    }
  }
}
Balon
thank you but for some reason I am getting <b>Parse error</b>: syntax error, unexpected T_AS, expecting ';' in <b>/Users/briancarpenter/Sites/Vortoj/preniVortoj.php</b> on line <b>101</b><br /> That is " for($object->quizzes as $quiz) " line and it's not me, when your code is commented out no errors
Klanestro
I've updated the code. Try now.
Balon
Notice: Undefined variable: objects in /Users/briancarpenter/Sites/Vortoj/preniVortoj.php on line 91Warning: Invalid argument supplied for foreach() in /Users/briancarpenter/Sites/Vortoj/preniVortoj.php on line 91now the error just jumped up, to foreach($objects as $object)
Klanestro
Like I have written: `$objects is the array with all those objects`. What is the name of the variable which holds the array?
Balon
i have the code here http://github.com/klanestro/Vortoj/blob/master/preniVortoj.php
Klanestro
Then change `foreach($objects as $object)` to `foreach($SFvorto as $object)`
Balon
<b>Parse error</b>: syntax error, unexpected T_AS, expecting ';' in <b>/Users/briancarpenter/Sites/Vortoj/preniVortoj.php</b> on line <b>104</b><br /> thats for($object->quizzes as $quiz) I changed $object to $SFvorto in for($object->quizzes as $quiz) same error
Klanestro
Read Álvaro G. Vicario's comment to your question and post the variable, so we can test it.
Balon
Oh, my bad. I wrote `for($object->quizzes as $quiz)` instead of `foreach($object->quizzes as $quiz)`. `for` instead of `foreach`. I've corrected this.
Balon
Just use the code in my answer and the only thing yuo need to change is `foreach($objects as $object)` to `foreach($SFvorto as $object)`
Balon
Ok thank you so much http://github.com/klanestro/Vortoj/blob/master/varexport
Klanestro
I don't think we need the varexport anymore :) I've corrected the code in my answer and it should work properly now.
Balon
Thank you now it's not echoing foreach($SFvorto as $object)
Klanestro
no output with this too$tmp1 = $quiz->question; $tmp2 = $quiz->answer; print_r($tmp1); print_r($tmp2);
Klanestro
You did a mistake. I said that you need to change `foreach($objects as $object)` to `foreach($SFvorto as $object)` but you've replaced `foreach($object->quizzes as $quiz)` and you were not supposed to. Second foreach should be the same like in the code I've posted.
Balon
Although i did do http://github.com/klanestro/Vortoj/blob/master/varexport
Klanestro
Now my code is foreach($SFvorto as $object) and I don't get anything out.I have fully updated my git
Klanestro
I've made a big mistake. I totally forgot about `responses`. I've corrected my code.
Balon
<html> <body> Your Direct search was preni <br></br> <b>preni</b>: get, lay hold of, pick up, takeeo<br /> <b>Notice</b>: Undefined variable: objet in <b>/Users/briancarpenter/Sites/Vortoj/preniVortoj.php</b> on line <b>95</b><br /> <br /> <b>Notice</b>: Trying to get property of non-object in <b>/Users/briancarpenter/Sites/Vortoj/preniVortoj.php</b> on line <b>95</b><br /> <br /> <b>Warning</b>: Invalid argument supplied for foreach() in <b>/Users/briancarpenter/Sites/Vortoj/preniVortoj.php</b> on line <b>95</b><br /> eo<br /> <b>Notice</b>: Undefined variable: objet in <b>/Us
Klanestro
new commit at http://github.com/klanestro/Vortoj/blob/master/preniVortoj.php
Klanestro
http://github.com/klanestro/Vortoj/blob/master/error
Klanestro
No more errors,(typo) but still no echo
Klanestro
is it because the information we are looking for is in another object nested 1 deeper
Klanestro
if(!isset($object->responses)) Fixed it see http://stackoverflow.com/questions/2117893/echo-can-you-hear-me/2118475#2118475
Klanestro
A: 

You want something lke this:

<?php

foreach($foo as $i){
    echo 'Language (cue): ' . $i['cue']['language'] . "\n";
    foreach($i['responses'] as $j){
        echo 'Language (response): ' . $j['language'] . "\n";
        foreach($j['quizzes'] as $k){
            echo 'Answer: ' . $k['answer'] . "\n";
            echo 'Question: ' . $k['question'] . "\n";
        }
    }
}

?>

Two remarks:

(1) I suggested using var_export() to generate a copy and paste sample to test with it but apparently stdClass::__set_state() cannot be read back easily. Sorry if I mislead anyone.

(2) It's not difficult to build it yourself if you do it step by step.

<?php

// First
foreach($foo as $c => $v){
    var_dump($c, $v);
    exit;
}

// Second
foreach($foo as $i){
    echo 'Language (cue): ' . $i['cue']['language'] . "\n";
    foreach($i['responses'] as $c => $v){
        var_dump($c, $v);
        exit;
    }
}

// Etc.

?>
Álvaro G. Vicario