Asim Zaidi
Asim Zaidi

Reputation: 28284

array manipulation idea

I have two arrays.

$Array1 = array("Maza", "Nissan","Tacoma","Cobalt","Explorer");

and second array is

array(
    (int) 0 => array(
        (int) 0 => 'In Stock',
        (int) 1 => 'Cars'
    ),
    (int) 1 => array(
        (int) 0 => 'In stock',
        (int) 1 => 'Cars/Toyota/Tacoma'
    ),
    (int) 2 => array(
        (int) 0 => 'Out of Stock',
        (int) 1 => 'Cars/Toyota/Celica'
    ),
    (int) 3 => array(
        (int) 0 => 'In Stock',
        (int) 1 => 'Cars/Ford/Fusion'
    ),
    (int) 4 => array(
        (int) 0 => 'Out of Stock',
        (int) 1 => 'Cars/Ford/Explorer'
    ),
    (int) 5 => array(
        (int) 0 => 'In Stock',
        (int) 1 => 'Cars/Chevy/Cobalt'
    ),
    (int) 6 => array(
        (int) 0 => 'In Stock',
        (int) 1 => 'Cars/Nissan'
    )

)

Now I want to see what cars in stock or not based on the first and second array. So for Cobalt, it will return me In Stock, where as for Explorer, it will return me out of stock. And for Mazda it can retun "Do not carry" Where I am confused is wheher or not to explode the array[1] by slash"/" and then lookinto it. Is there another easier/faster way to do it. thanks

Upvotes: 0

Views: 81

Answers (2)

Dion
Dion

Reputation: 3335

Maybe

$Array1 = array("Maza", "Nissan","Tacoma","Cobalt","Explorer");
$secondarray = array(
    array('In Stock', 'Cars'),
    array('In stock', 'Cars/Toyota/Tacoma'),
    array('Out of Stock', 'Cars/Toyota/Celica'),
    array('In Stock', 'Cars/Ford/Explorer'),
    array('Out of Stock', 'Cars/Ford/Explorer'),
    array('In Stock', 'Cars/Chevy/Cobalt'),
    array('In Stock', 'Cars/Nissan'));
function findcar($car, $secondarray) {
    for($c = 0; $c <= count($secondarray); $c++) {
        if(strpos(strtolower($secondarray[$c][1]), strtolower($car)) > 0) {
                return $secondarray[$c][0];
        }
    }
}

echo findcar($Array1[1], $secondarray);

this would print out In Stock, if a car wasn't found it just returns nothing.

Upvotes: 2

Songo
Songo

Reputation: 5726

Try this:

$cars = array("Maza", "Nissan","Tacoma","Cobalt","Explorer");
$stocks=array(
    (int) 0 => array(
        (int) 0 => 'In Stock',
        (int) 1 => 'Cars'
    ),
    (int) 1 => array(
        (int) 0 => 'In stock',
        (int) 1 => 'Cars/Toyota/Tacoma'
    ),
    (int) 2 => array(
        (int) 0 => 'Out of Stock',
        (int) 1 => 'Cars/Toyota/Celica'
    ),
    (int) 3 => array(
        (int) 0 => 'In Stock',
        (int) 1 => 'Cars/Ford/Fusion'
    ),
    (int) 4 => array(
        (int) 0 => 'Out of Stock',
        (int) 1 => 'Cars/Ford/Explorer'
    ),
    (int) 5 => array(
        (int) 0 => 'In Stock',
        (int) 1 => 'Cars/Chevy/Cobalt'
    ),
    (int) 6 => array(
        (int) 0 => 'In Stock',
        (int) 1 => 'Cars/Nissan'
    )

);
$output=array();
foreach ($cars as $car) {
   foreach ($stocks as $stock) {
    if(in_array($car, explode('/', $stock[1]))){
        $output[$car]=$stock[0]; 
        break;
        }else{$output[$car]='Do not care';}
} 
}
echo '<pre>';
print_r($output);

This will print:

Array
(
    [Maza] => Do not care
    [Nissan] => In Stock
    [Tacoma] => In stock
    [Cobalt] => In Stock
    [Explorer] => Out of Stock
)

Upvotes: 2

Related Questions