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?
It should be something like this
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';
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;
here is the structure
$list = array(
1 => array(
'URL_ID' => 1
'ID' => 1
'TAG' => 'hiiiii'
),
2 => array(
'URL_ID' => 1
'ID' => 2
'TAG' => 'hello'
),
);
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.