Page 1 of 1

[Solved]Array comparison

Posted: Tue Nov 16, 2004 7:40 pm
by Steveo31
What I'm doing is taking the QUERY_STRING and breaking it apart like so:

Code: Select all

$url = $_SERVER['QUERY_STRING'];
$vs = preg_split("/\W+/", $url);
What I'd like to do is check the values of the resulting array to this one:

Code: Select all

$categories = array(
              "Furniture" => array("Art", "Pictures", "Antiquities", "Item Managers", "Decoration", "Movable")
             , "Family" => array("Toys", "Motherhood", "Jewels and Perfumes", "Pharmacy")
             , "Tools" => array("Tools", "Heavy Equipment", "Generators", "Pro Equipment", "Rentals", "Agricultural Tools")
             , "Animals" => array("Accessories", "Bedding")
             , "Real Estate" => array("Commerce", "Business", "Residential", "Industrial/Machine")
             , "Hardware" => array("Electrical", "Plumbing", "Electrical", "Sanitation")
             , "Education" => array("Books", "Computers", "Printers/Peripherals", "School/Office Supplies")
             , "Data Processing" => array("Software", "Games", "Computer Accessories")
             , "Electronics" => array("Audio", "Video", "Video Game Consoles", "Phones", "Cell Phone Accessories")
             , "Leisures" => array("Sports Equipment", "Photography", "Music", "Musical Instruments", "Movies", "Collections", "Hobbies")
             , "Community" => array("Sharing")
             , "Automobiles" => array("Directories", "Accessories", "Cars", "Equipment")
             , "Employment" => array("Job Fairs", "Seminars", "Frameworks")
         );
Both arrays are what I need them to be, but I can't get what code I need. I've used various combinations of foreach(), for(), in_array(), and a bunch others.

So when the URL contains "Automobiles", I'd like it to recogize it, and from there display a photo.

Posted: Tue Nov 16, 2004 8:31 pm
by John Cartwright
i dont think you properly explained what you are trying to do..

what I got out of that is that you are passing the type of category through the url... ie: ?category=automobile


and then you want to do what exackly? get the array inside the array specific to the category?

Re: Array comparison

Posted: Tue Nov 16, 2004 8:45 pm
by neophyte
I'm with Phenom need more info.
Steveo31 wrote: What I'd like to do is check the values of the resulting array to this one:
Why do you need to check the values and what specifically do you need from the array. Category...?

Posted: Wed Nov 17, 2004 3:24 am
by Steveo31
Got it, my bad.

I've got to the point where I have the $_SERVER['QUERY_STRING'] in an array, broken up by non-word boundaries (preg_split). From there, I'd like to see if any of the values in that array == any values in the array I posted.

Posted: Wed Nov 17, 2004 8:20 pm
by Steveo31
bump :)

Posted: Wed Nov 17, 2004 8:25 pm
by John Cartwright

Code: Select all

<?php

foreach ($query_string_array as $var)
{

if (in_array($var,$categories)
{

foreach ($categories[$var] as $var2)
{

echo $var2.'<br />';

}
}

?>
Still not quite sure what you are after?

Posted: Thu Nov 18, 2004 2:04 am
by Steveo31
Ok, didn't wanna do this cause of the length, but I'm not explaining it correctly without it :)

Query String Array (all words present in $_SERVER['QUERY_STRING']):

Code: Select all

Array
(
    &#1111;0] => do
    &#1111;1] => browse
    &#1111;2] => cat
    &#1111;3] => Automobiles
)
Categories:

Code: Select all

Array
(
    &#1111;Furniture] => Array
        (
            &#1111;0] => Art
            &#1111;1] => Pictures
            &#1111;2] => Antiquities
            &#1111;3] => Item Managers
            &#1111;4] => Decoration
            &#1111;5] => Movable
        )

    &#1111;Family] => Array
        (
            &#1111;0] => Toys
            &#1111;1] => Motherhood
            &#1111;2] => Jewels and Perfumes
            &#1111;3] => Pharmacy
        )

    &#1111;Tools] => Array
        (
            &#1111;0] => Tools
            &#1111;1] => Heavy Equipment
            &#1111;2] => Generators
            &#1111;3] => Pro Equipment
            &#1111;4] => Rentals
            &#1111;5] => Agricultural Tools
        )

    &#1111;Animals] => Array
        (
            &#1111;0] => Accessories
            &#1111;1] => Bedding
        )

    &#1111;Real Estate] => Array
        (
            &#1111;0] => Commerce
            &#1111;1] => Business
            &#1111;2] => Residential
            &#1111;3] => Industrial/Machine
        )

    &#1111;Hardware] => Array
        (
            &#1111;0] => Electrical
            &#1111;1] => Plumbing
            &#1111;2] => Electrical
            &#1111;3] => Sanitation
        )

    &#1111;Education] => Array
        (
            &#1111;0] => Books
            &#1111;1] => Computers
            &#1111;2] => Printers/Peripherals
            &#1111;3] => School/Office Supplies
        )

    &#1111;Data Processing] => Array
        (
            &#1111;0] => Software
            &#1111;1] => Games
            &#1111;2] => Computer Accessories
        )

    &#1111;Electronics] => Array
        (
            &#1111;0] => Audio
            &#1111;1] => Video
            &#1111;2] => Video Game Consoles
            &#1111;3] => Phones
            &#1111;4] => Cell Phone Accessories
        )

    &#1111;Leisures] => Array
        (
            &#1111;0] => Sports Equipment
            &#1111;1] => Photography
            &#1111;2] => Music
            &#1111;3] => Musical Instruments
            &#1111;4] => Movies
            &#1111;5] => Collections
            &#1111;6] => Hobbies
        )

    &#1111;Community] => Array
        (
            &#1111;0] => Sharing
        )

    &#1111;Automobiles] => Array
        (
            &#1111;0] => Directories
            &#1111;1] => Accessories
            &#1111;2] => Cars
            &#1111;3] => Equipment
        )

    &#1111;Employment] => Array
        (
            &#1111;0] => Job Fairs
            &#1111;1] => Seminars
            &#1111;2] => Frameworks
        )

)
You'll see that "Automobiles" is in both arrays, but due to the structure of the arrays, I can't get the code right to show that.

Posted: Thu Nov 18, 2004 8:18 am
by emperor
I have to admit I'm still not too sure what you're after, but it looks like you could use a bit of [php_man]array_intersect[/php_man], see if this is what you want:

Code: Select all

<?php
    if ($cat = current (array_intersect (array_keys ($categories), $query_string_array)))
        print_r ($categories[$cat]);
?>
(PS in case you think I stole that code from the guy who submitted it into the PHP manual for array_intersect - that was me too!)

Posted: Thu Nov 18, 2004 12:09 pm
by Steveo31
I don't see how I'm not explaining this well enough... I must be too tired. :D

But dude, you hit the nail on the head. Perfecto :)

Thanks a ton.

edit... here's the end product:

Code: Select all

include 'stuff.php'; //contains the categories array
//header("Content-type: image/gif");

$stock = array();
$url = $_SERVER['QUERY_STRING'];

$vs = preg_split("/\W+/", $url);

$keys = array_keys($categories);
if($cat = current(array_intersect($keys, $vs))){
    shuffle($categories[$cat]);
    echo current($categories[$cat]); 
}
It will eventually output an image (chosen by the value output by the above) to the browser and the page used as the src in <img src...>

Posted: Thu Nov 18, 2004 3:51 pm
by emperor
Ah good, glad to see it was what you were after. Just a quickie, there's a function to retrieve a random array value, so instead of this

Code: Select all

<?php
    shuffle($categories[$cat]);
    echo current($categories[$cat]); 
?>
You could try this

Code: Select all

<?php
    echo $categories[$cat][array_rand($categories[$cat])];
?>