Results 1 to 3 of 3

Thread: Check if value is in sub array without specifying which sub array.

  1. #1
    Join Date
    Dec 2005
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Check if value is in sub array without specifying which sub array.

    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.

    PHP Code:
    $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] => ...
      )
    [
    3ect.


    Last edited by Snookerman; 04-23-2009 at 03:00 PM. Reason: added “Resolved” prefix

  2. #2
    Join Date
    Dec 2005
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    I've come up with a solution to this. If anybody is interest in it, here it is:

    PHP Code:
    $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";

    Last edited by toe_head2001; 04-23-2009 at 01:20 PM.

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    PHP Code:
    <?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';
        }
    }

  4. The Following User Says Thank You to JasonDFR For This Useful Post:

    toe_head2001 (04-24-2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •