0

var_dump($resultData);

gives me this

array(3) {
  [0]=>
  object(stdClass)#1 (2) {
    ["name"]=>
    string(12) "filterName_1"
    ["value"]=>
    string(8) "language"
  }
  [1]=>
  object(stdClass)#2 (2) {
    ["name"]=>
    string(9) "country_1"
    ["value"]=>
    string(0) ""
  }
  [2]=>
  object(stdClass)#3 (2) {
    ["name"]=>
    string(10) "language_1"
    ["value"]=>
    string(4) "UAE1"
  }
}

How to itrate and get the values ?

tried

echo $resultData["name"];

and some other combinations but cannot make it work.

EDIT : NOTE :

As i have to get the loop of 3 items and its values i need to get the value in the loop as

 for ($i=0; $i+3 <= count($resultData); $i=$i+3) 
 { 
 }

is there a way without foreach looping?

1
  • use foreach() and then access the value for "name" Commented Jul 21, 2014 at 6:38

3 Answers 3

7

May you should try

<?php

foreach($resultData as $key => $value) {
    echo $value->name; // Give you all names
}

Another example:

<?php
// Set keys you need to print
$requiredKeys = array('name', 'value');

// Iterate the array
foreach($resultData as $key => $value) {

    // Iterate the required keys
    foreach($requiredKeys as $reqVal) {

        // Check, if propertie exists in current object
        if(isset($value->{$reqVal}) {
              echo $value->{$reqVal}; // Give you all names
        }
    }
}

Example without foreach (for-loop):

<?php
// Set keys you need to print
$requiredKeys = array('name', 'value');

// Iterate the array
for($currentElement = 0; $currentElement <= count($resultData) as $currentElement++) {

    // Iterate the required keys
    for($reqCounter = 0; $reqCounter <= count($requiredKeys); $reqCounter++) {

        // Check, if propertie exists in current object
        if(isset($resultData[$currentElement]->{$requiredKeys[$reqCounter]}) {
              echo $resultData[$currentElement]->{$requiredKeys[$reqCounter]}; // Give you all names
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

please see the edit in the question
does my edit helped you?
is there a way without foreach looping?
@LithuT.V Just change foreach into for ($i = 0; $i < count($resultData); $i++) and change $value into $resultData[$i]. Same thing for the inner foreach as well.
yeah got it from Timosta's answer.
|
1

$resultData is an array containing multiple objects, therefore the first object can be accessed with $resultData[0] and so on. The objects have a property named name, which can be accessed with $object->name If you have more than one object in the array, then you can loop through the values contained in the array with foreach. For example:

foreach ($resultData as $object) {
    if($object->name === MY_NAME) {
        echo $object->value;
    }
}

This will take each object in the array and display its value if its name equals MY_NAME.

Comments

0
echo $resultData[0]->name;

Give that a try

2 Comments

It is $resultData[0]->name for the first entry of the array
It's a multidimensional array. Your example will fail. You have to loop through the result set to print out the name for each of the items.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.