Log in

View Full Version : Resolved Check if value is in sub array without specifying which sub array.



toe_head2001
04-21-2009, 09:31 PM
Is there a way to use in_array() to check if a given value is present as [id] in any the sub arrays with out specifying [0], [1], ect first?

I can't do in_array($value, $stuff[0][id]), in_array($value, $stuff[1][id]), ect. I number of arrays with in $stuff will always to different.



$stuff = Array
(
[0] => Array
(
[id] => 546
[quant] => 671
[notes] => none
)
[1] => Array
(
[id] => 123
[quant] => 1
[notes] => ninguno
)
[2] => Array
(
[id] => 542
[quant] => 15
[notes] => ...
)
[3] ect.

)

toe_head2001
04-23-2009, 01:02 PM
I've come up with a solution to this. If anybody is interest in it, here it is:



$stuff = Array
(
[0] => Array
(
[id] => 546
[quant] => 671
[notes] => none
)
[1] => Array
(
[id] => 123
[quant] => 1
[notes] => ninguno
)
[2] => Array
(
[id] => 542
[quant] => 15
[notes] => ...
)
)


$id_array = array();
foreach($stuff as $item)
{
$id_array[] = $item[id];
}


if (in_array($id,$id_array))
{
echo "Yup";
}

JasonDFR
04-23-2009, 07:53 PM
<?php

$id = 546;

$stuff = array(
array(
'id' => 546,
'quant' => 671,
'notes' => 'none'
),
array(
'id' => 123,
'quant' => 1,
'notes' => 'ninguno'
),
array(
'id' => 542,
'quant' => 15,
'notes' => '...'
),
);

foreach ($stuff as $array) {
if ( $id == $array['id'] ) {
echo 'yup';
}
}