[ create a new paste ] login | about

Link: http://codepad.org/BxpathBq    [ raw code | output | fork ]

PHP, pasted on Jul 17:
<?php

$items = array(
  (object) array(
    "itemId" => 560639000019,
    "name" => "Item no1",
    "code" => 00001,
    "qty" => 5,
    "id" => 2
  ),
  (object) array(
    "itemId" => 470639763471,
    "name" => "Second item",
    "code" => 76347,
    "qty" => 9,
    "id" => 4
  ),
  (object) array(
    "itemId" => 56939399632,
    "name" => "Item no 3",
    "code" => 39963,
    "qty" => 6,
    "id" => 7
  )
);

function search($items, $id, &$key = null) {
  foreach( $items as $item ) {
    if( $item->id == $id ) {
      $key = key($item);
      return $item;
    }
  }

  return null;
}

$item = search($items, 4, $key);

echo "## Item Found\n";
print_r($item);

echo "\n\n REMOVE ITEM \n\n";
unset($items[$key]);

echo "## New Items\n";
print_r($items);


Output:
## Item Found
stdClass Object
(
    [itemId] => 470639763471
    [name] => Second item
    [code] => 76347
    [qty] => 9
    [id] => 4
)


 REMOVE ITEM 

## New Items
Array
(
    [0] => stdClass Object
        (
            [itemId] => 560639000019
            [name] => Item no1
            [code] => 1
            [qty] => 5
            [id] => 2
        )

    [1] => stdClass Object
        (
            [itemId] => 470639763471
            [name] => Second item
            [code] => 76347
            [qty] => 9
            [id] => 4
        )

    [2] => stdClass Object
        (
            [itemId] => 56939399632
            [name] => Item no 3
            [code] => 39963
            [qty] => 6
            [id] => 7
        )

)


Create a new paste based on this one


Comments: