0

I want to know how can I limit the values of array to be displayed. Assuming my $color is equals to Indigo

I tried implementing this code by it doesn't work

foreach($data as $item){
    if($item['color'] == $color){
        echo $item['size'].'-'.$item['color'].'-'.$item['price'].'<br>';
    }
}

And if I remove the if statement the var_dump($data)

    array(16) {
  [0]=>
  array(3) {
    ["size"]=>
    string(1) "2"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(11) "Black Rinse"
  }
  [1]=>
  array(3) {
    ["size"]=>
    string(1) "2"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(6) "Indigo"
  }
  [2]=>
  array(3) {
    ["size"]=>
    string(1) "4"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(11) "Black Rinse"
  }
  [3]=>
  array(3) {
    ["size"]=>
    string(1) "4"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(6) "Indigo"
  }
  [4]=>
  array(3) {
    ["size"]=>
    string(1) "6"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(11) "Black Rinse"
  }
  [5]=>
  array(3) {
    ["size"]=>
    string(1) "6"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(6) "Indigo"
  }
  [6]=>
  array(3) {
    ["size"]=>
    string(1) "8"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(11) "Black Rinse"
  }
  [7]=>
  array(3) {
    ["size"]=>
    string(1) "8"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(6) "Indigo"
  }
  [8]=>
  array(3) {
    ["size"]=>
    string(2) "10"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(11) "Black Rinse"
  }
  [9]=>
  array(3) {
    ["size"]=>
    string(2) "10"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(6) "Indigo"
  }
  [10]=>
  array(3) {
    ["size"]=>
    string(2) "12"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(11) "Black Rinse"
  }
  [11]=>
  array(3) {
    ["size"]=>
    string(2) "12"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(6) "Indigo"
  }
  [12]=>
  array(3) {
    ["size"]=>
    string(2) "14"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(11) "Black Rinse"
  }
  [13]=>
  array(3) {
    ["size"]=>
    string(2) "14"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(6) "Indigo"
  }
  [14]=>
  array(3) {
    ["size"]=>
    string(2) "16"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(11) "Black Rinse"
  }
  [15]=>
  array(3) {
    ["size"]=>
    string(2) "16"
    ["price"]=>
    string(6) "$59.00"
    ["color"]=>
    string(6) "Indigo"
  }
}

I want only to display values which is the color is equal to $color. How should I do this the correct way?

3 Answers 3

2

You might be dealing with case sensitivity or white space .. you can try :

$data = array(
        0 => array("size" => "2","price" => "$59.00","color" => "Black Rinse"),
        1 => array("size" => "2","price" => "$59.00","color" => "Indigo"));


$color = "Indigo";
foreach ( $data as $item ) {
    if (strtolower($item['color']) == strtolower($color)) {
        echo $item['size'] . '-' . $item['color'] . '-' . $item['price'] . '<br>';
    }
}

Output

2-Indigo-$59.00

See Live Demo

1
  • 2
    can you var_dump($data) like to see what am dealing with Commented Oct 17, 2012 at 0:57
1

This should fix your issue (assuming you're comparing two strings, I suggest you use strcasecmp- for case insensitive string comparison in your if loop)

foreach($data as $item){
    if(strcasecmp(trim($item['color']),trim($color))==0){
        echo $item['size'].'-'.$item['color'].'-'.$item['price'].'<br>';
    }
}
3
  • and what is the exact value of $item['color'] ? Commented Oct 17, 2012 at 1:02
  • It depends..because it is base on user input. Commented Oct 17, 2012 at 1:03
  • Works perfectly for me as well with the above code, see codepad.org/sgeVhEDw Commented Oct 17, 2012 at 1:09
1

Be elegant, be simple:

    $items = array(
        array('size'=>'4', 'color'=>'Indigo', 'price'=>'$59.00'), 
        array('size'=>'5', 'color'=>'black', 'price'=>'$59.00'), 
        array('size'=>'5', 'color'=>'green', 'price'=>'$54.00'),
        array('size'=>'7', 'color'=>'Indigo', 'price'=>'$50.00'),
        array('size'=>'5', 'color'=>'purple', 'price'=>'$51.00'));

   print_r(array_filter($items, function($i){
          return in_array('Indigo', $i);
      })
   );

or PHP < 5.3:

print_r(array_filter($items, 
    create_function('$i', 'return in_array("Indigo", $i);'))
);

Take advantage of features that PHP offers.

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.