Noob: Searching a multidimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wilbo
Forum Newbie
Posts: 22
Joined: Fri Jul 25, 2008 5:45 am

Noob: Searching a multidimensional array

Post by Wilbo »

Hi,

Lets say I have an array called $productPrices into which I'm putting values in pairsto represent a product code and a price:

Code: Select all

$productPrices = array();
$productPrices[] = array("AB001"=>"10",);
$productPrices[] = array("AB002"=>"23",);
$productPrices[] = array("AB003"=>"14",);
etc

What I want to do is search through the array for a certain ProductCode and return the corresponding Price.
And I know that each product code is unique, so I want it to break out once its found the ProductCode.

I've read and re-read the manual and tried dozen things but I just can't figure it out!

Thanks in advance!
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: Noob: Searching a multidimensional array

Post by Peter Kelly »

Try

Code: Select all

<?php
$product_code = "AB001";

foreach($productPrices as $id){
     if($productPrices[$id]['0'] == $product_code){
          echo "Price: " . $productPrices[$id]['0'];
          break(1);
     }
}
?>
I'm not sure if it will work it was just quickly while I was in work.
Last edited by Peter Kelly on Wed Feb 02, 2011 7:27 am, edited 2 times in total.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Noob: Searching a multidimensional array

Post by klevis miho »

Code: Select all

<?php 		
$productPrices = array();
$productPrices[] = array("AB001"=>"10");
$productPrices[] = array("AB002"=>"23");
$productPrices[] = array("AB003"=>"14");


//product code that will be searched
$productCode = 'AB003';

foreach($productPrices as $productPrice) {

	foreach($productPrice as $pCode => $price) {
		
		if($pCode == $productCode) {
			echo $productPrice[$pCode];
		}
	}
}
?>
Wilbo
Forum Newbie
Posts: 22
Joined: Fri Jul 25, 2008 5:45 am

Re: Noob: Searching a multidimensional array

Post by Wilbo »

Thanks for the answers but I made an error in my original question, the array that I have is actually this:

$productPrices = array();
$productPrices[] = array("productCode"=>"AB001", "price"=>"10");
$productPrices[] = aarray("productCode"=>"AB002", "price"=>"10");
$productPrices[] = array("productCode"=>"AB003", "price"=>"10");

Sorry to have wasted anyone's time.
Any ideas about how to search this array?
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: Noob: Searching a multidimensional array

Post by Peter Kelly »

Wilbo wrote:Thanks for the answers but I made an error in my original question, the array that I have is actually this:

$productPrices = array();
$productPrices[] = array("productCode"=>"AB001", "price"=>"10");
$productPrices[] = array("productCode"=>"AB002", "price"=>"10");
$productPrices[] = array("productCode"=>"AB003", "price"=>"10");

Sorry to have wasted anyone's time.
Any ideas about how to search this array?

Code: Select all

<?php
$product_code = "AB001";

foreach($productPrices as $products){
     if($products[productCode] == $product_code){
          echo "Price: " . $products[price];
          break(1);
     }
}
?>
Wilbo
Forum Newbie
Posts: 22
Joined: Fri Jul 25, 2008 5:45 am

Re: Noob: Searching a multidimensional array

Post by Wilbo »

Thanks Peter, that works a treat!
Post Reply