0

I'm having trouble understanding two dimensional php arrays, In my database I have a table that stores URLs, it's related to another table that stores tags by a 1 to N relation, the tables look like this:

URL         
ID    --->  URL_ID
            ID
            TAG

I'm trying to fetch this structure and store it in a PHP class, but as i do not understand

class Url extends databaseobject{

    $list = [

        "url" =>,
        "tags" => []


    ];
                                }

Can someone help me build the correct structure to store my database?

3 Answers 3

1

It should be something like this

   db_url           db_tags
id  url         id  url_id  tag
 |---------------------|
class UrlTags
{
    public $url_id;
    public $url;
    public $tags = array();
}

So when you need to save list of URLs with their corresponding tags.

$url_tags = new UrlTags();

$url_tags->$id = 1;
$url_tags->$url = 'http://www.example.com';
$url_tags->$tags[0] = 'zero';
$url_tags->$tags[1] = 'one';
$url_tags->$tags[2] = 'two';

or with much more Object Oriented style:

class Url
{
    public $url_id;
    public $url;
    public $tags = array();
}

class Tags
{
    public $tag_id;
    public $tag_name;
}

$url_list = new Url();

$url_list->$id = 1;
$url_list->$url = 'http://www.example.com';

$tag = new Tags();
$tag->$tag_id = 1;
$tag->$tag_name = 'one';

//Now store the tags
$url_list->$tags[0] = $tag;

$tag = new Tags();
$tag->$tag_id = 2;
$tag->$tag_name = 'two';

$url_list->$tags[1] = $tag;
Sign up to request clarification or add additional context in comments.

Comments

0

here is the structure

$list  = array(
          1 => array(
            'URL_ID' => 1
            'ID' => 1
            'TAG' => 'hiiiii'
          ),  
          2 => array(
            'URL_ID' => 1
            'ID' => 2
            'TAG' => 'hello'
          ),
       ); 

2 Comments

each url has several tags, does this mean, tag => would also be an array? I need to fill it dinamically after a mysql query
I think the response $list is an array of all of the "tag" records, with a reference to which URL it belongs to. I assume there would be a second array, $URL_List = array(1 => "someUrl", 2 => "someOtherUrl") ...
0

Assuming you are are iterating over a result set in which the two tables are joined, so each row in the result set looks something like this:

url, url_id, tag, tag_id

Also assuming that whatever adaptor you're using creates some kind of iteratable result set, then as you iterate (this is kind of pseudo-codey without knowing what adapter you're using):

$list = array();
while($arr = $your_adapter's_current_row){
    if(empty($list[$arr[$url_id]]) {
        $list = array(
            'URL_ID' => $url_id,
            'URL' => $url,  
            'TAGS' => array()
        }
        $list[$arr[$url_id]]['TAGS'][$tag_id] = $tag;
}

You can then add the array as a property of your class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.