James
James

Reputation: 7

Taking values from Arrays inside an array

Below is the display of my array $arr[0]. Could you please tell me how to take the values of inner array?

Here I need to take only the value for the ID 656 which is 'John'.

Array
(
    [0] => xxxxxxxxx
        (
            [Users] => Array
                (
                    [0] => Array
                        (
                            [name] => 656
                            [value] => John

                        )

                    [1] => Array
                        (
                            [name] => 657
                            [value] =>Peter

                        )

                    [2] => Array
                        (
                            [name] => 658
                            [value] => Louie

                        )

                    [3] => Array
                        (
                            [name] => 659
                            [value] => Jim

                        )



                )

        )

Thanks in advance.

Upvotes: 0

Views: 109

Answers (3)

H2ONOCK
H2ONOCK

Reputation: 981

try running a:

 foreach($arr as $key=>$value){
      var_dump($value);
 }

And you'll probably be able to work out what to do from there. Hope that helps?

EDIT: if

$arr = array(
       0=>array(
        'Users'=>array(
            0=>array('name'=>656, 'value'=>'John'), 
            1=>array('name'=>656, 'value'=>'John'), 
            2=>array('name'=>658, 'value'=>'Louie')
            )
        )

);

Then you can use:

foreach($arr as $Users){
    foreach($Users as $k=>$v){
        var_dump($v[0]['value']);
    }
}

To get 'John'. Does that help?

Upvotes: 1

Will
Will

Reputation: 2333

If this isn't just a one-off, you could use a recursive array search function. If your data is in $arr, in the format you described:

$arr = array(array("Users"=>array(array("name"=>656,"value"=>"John"),array("name"=>657,"value"=>"Peter"))));

It might look like this:

print in_array_multi("656",$arr);
// ^-- This prints John

print in_array_multi("657",$arr);
// ^-- This prints Peter

function in_array_multi($item, $arr) {
    foreach ($arr as $key => $value) {
        if ($value==$item){
            return $arr['value'];
        } else if (is_array($value)){
            if($ret = in_array_multi($item, $value))
                 return $ret;
        }
    }
    return "";
}

Upvotes: 1

ZaoTaoBao
ZaoTaoBao

Reputation: 2615

for example:

i supose you have 3 arrays to arrive where you wanna ok? array_1[0].array_2[0].array_3[0]---> here you the information you need.

so you have 2 arrays inside array_1 at position 0.

for(){//to watch the first array all positions 
  for(){//top watch the second array idem
    for(){//to watch the 3 idem...
      here at teh position 0 take away the values... 
    }   
  }
}

it hope it helps.

Upvotes: 0

Related Questions