I am using an XML parser to get product info from a supplier site, I then break it into an array. What I need to be able to do here is to search for a specific model number code and it's accompanying data into a new single array. How do I go about achieving this goal?

For example, if I only needed to collect data for 'CP-iSi7Q2600' from the excerpt below, please keep in mind I need to search for a specfic 'productcode'.

Array
(
    [0] => Array
        (
            [productcode] => CP-ISI7Q2600
            [description] => Intel Sb i7 Quad 2600 3.4
            [price] => 2299
            [stock] => 5
            [eta] => 
        )

    [1] => Array
        (
            [productcode] => CP-ISI7Q2600K
            [description] => Intel Sb i7 Quad 2600K no fan
            [price] => 2299
            [stock] => -12
            [eta] => 
        )

    [2] => Array
        (
            [productcode] => CP-ISI7Q2600K
            [description] => Intel Sb i7 Quad 2600K no fan
            [price] => 2299
            [stock] => -12
            [eta] => 2011/01/31
        )

    [3] => Array
        (
            [productcode] => CP-ISI7Q2600K
            [description] => Intel Sb i7 Quad 2600K no fan
            [price] => 2299
            [stock] => -12
            [eta] => 2011/01/19
        )

)

I attempted something but it needs refinement:

$product = 'CP-iSi7Q2600';

foreach($product_info_array as $somearray)
{
 foreach($somearray as $key => $value)
 {
   
  if ($value == strtoupper($product)) { echo 'product found:' . $value; }
  
 }
}

Furthermore, if I am possible to do this, how would I go about lifting out the latest entry for a specific model number? Based on the above example I need to find 'CP-iSi7Q2600K' but the last array only.

I hope you can help, I have limited PHP knowledge as you can see.

I managed to get the result I needed with the following function:

function search($array, $key, $value)
{
    $results = array();

    if (is_array($array))
    {
        if ($array[$key] == $value)
            $results[] = $array;

        foreach ($array as $subarray)
            $results = array_merge($results, search($subarray, $key, $value));
    }

    return $results;
}

print_r(search($product_info_array, 'productcode', strtoupper($product)));

I still need to figure out how to get the latest entry of duplicate product codes, any help will be appreciated, thanks.

I employed a simple count logic to retrieve the last array in the batch:

$somearray = search($product_info_array, 'productcode', strtoupper($product));
$cnt = count($somearray)-1;


if ($cnt > 1) {
	print_r($somearray[$cnt]);
	} else {	
	$cnt = 0;
	print_r($somearray[$cnt]);
		}
	}

I hope this is an efficient way of doing it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.