#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2016
    Posts
    109
    Rep Power
    3

    Array comparrison


    I am having trouble trying to get two array to compare with each other array element.

    PHP Code:

    I have first 
    array $arr1 = ['blue''red''yellow'];

    my second array is coming from database as :

    foreach (
    $data['pods'] as $pods) {

       
    print_r($pods);
    }

    output

    Array
    (
        [
    pods] => blue
        
    [0] => blue
    )
    Array
    (
        [
    pods] => red
        
    [0] => red
    )
    Array
    (
        [
    pods] => yellow
        
    [0] => yellow

    How do I compare the "$pods" with "$arr1" and output it to "$result"?

    I am expecting output to look like this:

    $result = ['blue', 'yellow'];

    Thanks in advance.
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,893
    Rep Power
    9646
    Start with $result being an empty array. In your loop use in_array to check if the pod is in your array. If it is, append it to $result.
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,958
    Rep Power
    4575
    $pods is an array, based on that output. $pods['pod'] or $pods[0] is the value you're looking to match in $arr1 with in_array().

    if that's just more of an example of output, then nevermind. But either way, in_array() is what you're looking for.

    -John
    -- Cigars, whiskey and wild, wild women. --
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2016
    Posts
    109
    Rep Power
    3
    Originally Posted by requinix
    Start with $result being an empty array. In your loop use in_array to check if the pod is in your array. If it is, append it to $result.

    I am doing a project, just trying to learn bit of php and raw javascript.

    What I am trying to achieve, I have a form with drop down list, which has "blue", "red", "yellow" as options. I have another drop down box with Pen 1, Pen 2 , Pen 3 etc

    someone could select Pen 1 & Blue,

    this then goes in to database table.

    Next time when Pen 1 is selected the colour blue should not appear since colour blue is already taken. So pen 1 could potentially have all 3 colours consumed.

    When all three colours are taken for pen 1 the drop down box listing pen 1 should disappear from the list.

    and I thought by comparing database result with define array I could somehow get this to work dynamically?

    for example if database result is 'blue'

    the drop down list should show
    coming from an array?

    Red
    Yellow

    I am using ajax to update the option list.

    But I am having difficulty getting the logic right.

    I am kind of clueless on how to go on about doing this.

    Can I get a example on how to use in_array()?

    Thanks in advance.
  8. #5
  9. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,958
    Rep Power
    4575
    If you want to do this via AJAX, you're talking a different animal. Your "in array" comparison would likely be done in JavaScript, not PHP.

    To answer your PHP question about in_array(), though, the manual explains it pretty well with examples. PHP: in_array - Manual

    in_array($needle, $haystack) will return true if the $needle value is in the $haystack array. So if(in_array($pods, $arr1)) in your case.
    -- Cigars, whiskey and wild, wild women. --
  10. #6
  11. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,893
    Rep Power
    9646
    Yeah, doing this with Javascript and AJAX means it's a bit more complicated.

    First make the HTML form. The pen list should have all the pen options and the color list should have all the color options that are valid for the first pen in the list. This could be when you use in_array().

    Then make a second PHP script that follows this template:
    PHP Code:
    <?php

    header
    ("Content-Type: application/json");

    if (!isset(
    $_GET["pen"])) {
        echo 
    "[]";
        exit;
    }

    /* now use $_GET["pen"] to get all the color options that are valid for that pen */
    /* put them into $result like $result[] = the color */

    echo json_encode($result);
    When that's done you can work on the Javascript...

IMN logo majestic logo threadwatch logo seochat tools logo