1

I have an array like

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [org_name] => name
            [field_name] => fullname
            [new_name] => Name
            [index] => 3
            [modified] => 2016-05-17 10:45:17
        )

    [1] => stdClass Object
        (
            [id] => 3
            [org_name] => reception_no
            [field_name] => reception_no
            [new_name] => Reception No.
            [index] => 1
            [modified] => 2016-05-17 10:45:17
        )

    [2] => stdClass Object
        (
            [id] => 4
            [org_name] => pno
            [field_name] => pno
            [new_name] => Personel No.
            [index] => 0
            [modified] => 2016-05-17 10:45:17
        )

and i want for example where object has 'pno' get value of 'index' in this example for example '0' is there possible to do that ?

11
  • Try this echo $arr[0]->index, This is an object array, so you need to use -> for access the array. and use foreach loop for all. Commented May 24, 2016 at 6:38
  • Do you mean that you want to return the value of the 'index' property of the object that has the property 'pno'? What do you want to occur if more than one object has that property? Commented May 24, 2016 at 6:38
  • @oroboto yes exactly , but the key is unique Commented May 24, 2016 at 6:40
  • elaborate your question "where object has 'pno' get value of 'index' in this example for example '0' is there possible to do that " - paraphrase it to make it clear Commented May 24, 2016 at 6:40
  • foreach ($array as $data) { if ($data->org_name == 'pno') { echo $data->index; } } Commented May 24, 2016 at 6:41

6 Answers 6

2
foreach ($arr as $items)
{
   if ($items->org_name=='pno')
     $index=$items->index;
 }
Sign up to request clarification or add additional context in comments.

1 Comment

you need to break the loop at match, No need to loop through entire array after getting the result.
1

As i am asking you about the pno, I think it was the first index like 0,1,2..., But after some conversation this is clear that it is inside the sub array.

So you need a loop here and check for the pno, if matched then echo the index. Let your array is $array

foreach ($array as $key => $val){
    if($val->org_name == 'pno'){
        echo $index = $val->index;
        break;
    }
}

Comments

1

yes it is possible try this

var_dump($arr[0]->index);

OR

print_r($arr->index['0']);

Comments

0

You can use array_search

$key1 = array_search('pno', array_column($your_array, 'field_name'));


$key2 = array_search('pno', array_column($your_array, 'org_name'));

Comments

0

Use array_search since you may not know the index

array_search

Comments

0

Tested with ur Array

$array = array(
        array('id'=>1,'org_name'=>'name','field_name'=>'fullname','new_name'=>'Name','index'=>3,'modified'=>'2016-05-17 10:45:17'),
        array('id'=>3,'org_name'=>'reception_no','field_name'=>'reception_no','new_name'=>'Reception No.','index'=>1,'modified'=>'2016-05-17 10:45:17'),
        array('id'=>4,'org_name'=>'pno','field_name'=>'pno','new_name'=>'Personel No.','index'=>0,'modified'=>'2016-05-17 10:45:17')
        );

This Code should do what u want

$index = '';    
foreach($array as $key => $value)
{
    if($value['org_name']=='pno')
    {
        $index = $value['index'];
    }
}
print $index;

Script only Loops through ur Array and sets $index on the last found pno index value

U can check $index in an IF, if ist empty.

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.