I have array like this

stdClass Object (
[0] => stdClass Object ( [field_id] => 10 [value] => 46426 [value_integer] => 0 )
[1] => stdClass Object ( [field_id] => 9 [value] => 1987 [value_integer] => 0 )
[2] => stdClass Object ( [field_id] => 8 [value] => 558/65 [value_integer] => 0 )
[3] => stdClass Object ( [field_id] => 7 [value] => Sturpolj [value_integer] => 0 )
[4] => stdClass Object ( [field_id] => 6 [value] => 1 [value_integer] => 0 )
[5] => stdClass Object ( [field_id] => 5 [value] => 6 [value_integer] => 0 )
[6] => stdClass Object ( [field_id] => 3 [value] => [value_integer] => 0 )
[7] => stdClass Object ( [field_id] => 4 [value] => 4 [value_integer] => 0 ) )

and for another row looks like this

stdClass Object (
[0] => stdClass Object ( [field_id] => 11 [value] => 555 [value_integer] => 0 )
[1] => stdClass Object ( [field_id] => 10 [value] => 19574 [value_integer] => 0 )
[2] => stdClass Object ( [field_id] => 9 [value] => 1972 [value_integer] => 0 )
[3] => stdClass Object ( [field_id] => 8 [value] => 192/34 [value_integer] => 0 )
[4] => stdClass Object ( [field_id] => 7 [value] => Munchen [value_integer] => 0 )
[5] => stdClass Object ( [field_id] => 6 [value] => 3 [value_integer] => 0 )
[6] => stdClass Object ( [field_id] => 5 [value] => 2 [value_integer] => 0 )
[7] => stdClass Object ( [field_id] => 4 [value] => 1 [value_integer] => 0 )
[8] => stdClass Object ( [field_id] => 3 [value] => [value_integer] => 249000 )
[9] => stdClass Object ( [field_id] => 2 [value] => p+1+2 [value_integer] => 0 )
[10] => stdClass Object ( [field_id] => 1 [value] => 30% [value_integer] => 0 ) )

and I want to print only value where [field_id] is equal to 9.
So the results would be for the first row 1987 and for the second 1972.
The problem is that number of results in each row are different so I can't use "echo $results[2]->value".
I should use something like this instead "echo $results[field_id][9]->value"
How can I print this?

Many thanks.

Member Avatar for diafol

If you had an array instead of stdObject:

$data = [['field_id'=>1,'value'=>345675],['field_id'=>2,'value'=>39945],['field_id'=>7,'value'=>565],
    ['field_id'=>9,'value'=>5],['field_id'=>4,'value'=>345],['field_id'=>12,'value'=>1345]];

function callback($v)
{
    return ($v['field_id'] == 9) ? true : false;
}

$d2 = array_filter($data, 'callback');
echo array_values($d2)[0]['value'];

ANy reason why you're using stdObject?

commented: wonder if this code is suitable if we cast the Object into array using $data = (array) stdObject? +4

return your array as a result_array or an array of arrays as a good practice.
then
//not tested but it's something like this

foreach($result as $k=>$v){
    //then add your condition here
    //example
    if(field_id==9) ? '' : '';//you can use ternary operation
    //or
    if(field_id==9){
        echo '<pre>';
        print_r($k['value']);
    }
}

now for your codes above you can print it like this

`echo '<pre>';
print_r($results1[1]['field_id']);//to get the value of field_id for the first array
echo '<pre>';
print_r($results2[2]['field_id']);//to get the value of field_id for the 2nd array`

Thanks very much philjen. You helped me a lot.

This is the final working result. Acctually it's pretty simple in the end.

foreach ($results as $row) {
    if ($row->field_id == 9) echo $row->value;
}

@diafol

I'm using Joomla 3.4.1 and it uses loadObjectList() which acctualy returns stdClass Object which is slower than array but this is Joomla standard query code.

$db->setQuery($query);
$row = $db->loadObjectList();
print_r($row);
Member Avatar for diafol

Why don't you specify this in the SQL query? with somefield = 9 ?

I could have added it in MySQL for each row "where 'field_id'=9" and print it but then I need to query MySQL "where 'field_id'=11" and then "where 'field_id'=8" etc. so I would end up having 20 queries per row multiply by Instead I have only one query now at the beggining and loading administration works really fast.

Can't you specify to get another class returned? If you can you could write your own class to handle that logic for you (just wondering).

Member Avatar for diafol

So you do want every field_id in a separate var (or as array items)?

There are many return types possible in Joomla.

As you could see I have already sorted it out.
Yes, you're right, I can see now I could have aldo used array with Joomla like $db->loadAssocList() instead of $db->loadObjectList().
Thanks.

Member Avatar for diafol

OK, mark as solved please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.