2

I tried to use the function array_search but can't get it work..

I got a php session with an array.

array(2) {
  [0]=>
    array(6) {
       ["ProductId"]=>string(2) "34"
       ["ProductName"]=>string(9) "Best ever"
       ["ProductPrice"]=>string(6) "453.00"
       ["ProductColor"]=>string(4) "Blue"
       ["ProductSize"]=>string(1) "S"
       ["Image"]=>string(36) "d12f95895c9130da8e52a7ff5b9216c9.png"
    }
 [1]=>
    array(6) {
       ["ProductId"]=>string(2) "33"
       ["ProductName"]=>string(5) "Vespa"
       ["ProductPrice"]=>string(7) "1789.00"
       ["ProductColor"]=>string(4) "Blue"
       ["ProductSize"]=>string(1) "S"
       ["Image"]=>string(36) "678e25ea94a7fa94bc6fa427ff29bc6c.png"
    }

now I do an array_search()

session_start();
include '_sqlclean.php';
(isset($_POST['product_id'])) ? $p_id = clean_string_save($_POST['product_id']) : $p_id = 0;
$array = $_SESSION['wishList'];

$key = array_search($p_id, $array);

if I do a

var_dump($_SESSION['wishList']);

I got what I showed you above. But I always got the message "Key not found"

Why ?? What's my mistake ? I tried already to do

$p_id = "34" // for try
$p_id = intval(34); // for try also
$key = array_search("34", $_SESSION['wishList']); // to see if it works

but nothing worked.. :( Thanks in advance

vishuB
  • 4,173
  • 5
  • 31
  • 49
Pierre
  • 675
  • 1
  • 8
  • 18

5 Answers5

1

array_search will not work for multidimensional arrays. Rather this might work -

$key = array_search($p_id, array_column($array, 'ProductId')); 

This will extract all the ProductId from that array then do the search.

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • Thanks works perfectly.. 2hours I've been searching before posting... :( and thanks for the Tips !! I didn't know that.. – Pierre Sep 10 '15 at 12:20
  • One more thing.. so to unset() "delete the row after" I need to do: unset($_SESSION['wishList'][$key]); Or is there something special to do because it's a multidimensional array ? – Pierre Sep 10 '15 at 12:34
1

Try with alternative for array_search().For example:

function searchForId($id, $array) {
   foreach ($array as $key => $val) {
      if ($val['uid'] === $id) {
        return $key;
    }
  }
 return null;
}

OR

$key = array_search($p_id, array_column($array, 'ProductId'));
Insane Skull
  • 9,220
  • 9
  • 44
  • 63
0

Aparently you are mistyping the variable (key) name

in the array it is ProductId and in your code it is Product_Id

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
Julio Soares
  • 1,200
  • 8
  • 11
0

It happen cause you search into outer array, which contains only [0] and [1] keys.

Try to use

$key = array_search("34", $_SESSION['wishList'][1]);
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
Nitromoon
  • 378
  • 1
  • 2
  • 11
0

You can try an alternative way like

<?php

$p_id = "34" // for try
$p_id = intval(34); // for try also

if(in_array($p_id, array_values($_SESSION['wishList']))) {
    // Product Id found in your wishList
}

?>

It will works only for single dimension array. For multidimensional use foreach loop.

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
Suman Singh
  • 1,379
  • 12
  • 20