/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Adding keys to new elements.

I have a script that adds elements to PHP arrays.

Some arrays are 2-dimensional, and to this array, I want to add keys so I can sort the array later.

So far I have

[code=php]$link_arr[$sec_slot] = Array()[/code]

Where can I add the key?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 09.2009 — You can either add them all in one go with:
[code=php]
$link_arr[$sec_slot] = array('key1' => 'value1', 'key2' => 'value2);
[/code]

Or you can add them one at a time with:
[code=php]
$link_arr[$sec_slot] = array();
$link_arr[$sec_slot]['key1'] = 'value1';
$link_arr[$sec_slot]['key2'] = 'value2';
[/code]
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorFeb 09.2009 — Ummm... that's not entirely what I want.

What I want is to be able to use either

[code=php]
$link_arr[1]
$link_arr[2]
$link_arr[3]
[/code]


OR

[code=php]
$link_arr[key1]
$link_arr[key2]
$link_arr[key3]
[/code]


($link_arr[0] doesn't get a key, and is dealt with elsewhere.)

Essentially, I want to be able to sort whole sub-arrays with ksort().
Copy linkTweet thisAlerts:
@NogDogFeb 09.2009 — I guess I don't understand. ?
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorFeb 09.2009 — Okay. If I were creating the elements to $link_arr all at once, I would try something like this:

[code=php]
$link_arr = Array(
'Key1' => Array('Element1', 'Element2'),
'Key2' => Array('Element1', 'Element2'),
'Key3' => Array('Element1', 'Element2'),
'Key4' => Array('Element1', 'Element2'),
'Key5' => Array('Element1', 'Element2')
)
[/code]


However, $link_arr has to be created dynamically! When I stick on a new element (which is in itself an array), I want to know how to assign a key to that new element.
Copy linkTweet thisAlerts:
@NogDogFeb 09.2009 — Do you mean:
[code=php]
$link_arr = array();
$link['Key1'] = array('Element1', 'Element2');
[/code]

?
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorFeb 09.2009 — I think I've got it:

[code=php]...

for($sec_name_num=0; $sec_name_num < count($sec_name_arr); $sec_name_num++){
if($sec_name_arr[$sec_name_num] == $section_name){
$unique=false;
$sec_id=$sec_name_num;
}
}
if($unique){
$sec_slot = count($sec_name_arr);
array_push($sec_name_arr, $section_name);
$link_arr[$section_name] = Array();
$link_arr[$section_name][0] = ('<li><a href="' . $url->item(0)->nodeValue . '">' . $name->item(0)->nodeValue . '</a></li>');

} else {
$link_sec_id = $sec_name_arr[$sec_id];
array_push($link_arr[$link_sec_id], ('<li><a href="' . $url->item(0)->nodeValue . '">' . $name->item(0)->nodeValue . '</a></li>'));
}
...

sort($sec_name_arr);

for ($i=0; $i < count($sec_name_arr); $i++){
echo ($t3 . (($i > 0)?('<h3>' . $sec_name_arr[$i] . '</h3>' . $n . $t3):('')) . '<ul>' . $n);
if ($i==0){
for($l=0; $l < count ($link_arr[$i]); $l++){
echo ($t4 . $link_arr[$i][$l] . $n);
}
} else {
for($l=0; $l < count ($link_arr[$sec_name_arr[$i]]); $l++){
echo ($t4 . $link_arr[$sec_name_arr[$i]][$l] . $n);

}
}
echo ($t3 . '</ul>' . $n);
}
[/code]



If you'd like to see my whole PHP file, just let me know.


By the way, what's an "Illegal Offset Type" error?

EDIT: Never mind, I just figured it out. I was using an object as a key. :eek:
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorFeb 09.2009 — *Marks thread resolved, with copy of PHP file so others can see what I did.*

[code=php]<?php
header('Content-type: application/xhtml+xml; charset=utf-8');

if(empty($_GET['keyword'])){
$kwd = 'Welcome';
} else {
$kwd = $_GET['keyword'];
}
$xml_dom = new DOMDocument();
$xml_dom -> loadXML(file_get_contents('./links.xml'));


//Other Stuff.
$t3 = "ttt";
$t4 = "tttt";
$n = "rn";

?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="application/javascript" />
<link type="text/css" rel="stylesheet" href="./setup.css" />
<title>HomePage</title>
</head>
<body>
<h1>Homepage</h1>
<ul id="Menu">
<li><a href="about:blank" class="blank">Blank Page</a></li>
<?php

$key_list = $xml_dom -> getElementsByTagName('kwd');
$key_arr = Array();

foreach($key_list as $keyword){
$kword = $keyword -> nodeValue;
$key_arr[$kword] = '<li><a href="index.php?keyword=' . $kword . '">' . $kword . '</a></li>';
}

ksort($key_arr);

foreach($key_arr as $keylink){
echo ($t3 . $keylink . $n);
}
?>
<li><a href="about:blank" class="blank">Blank Page</a></li>
</ul>
<div id="Main">
<h2><?php echo $kwd; ?></h2>
<?php
if($kwd=='Welcome'){
echo $t3 . '<p>All Systems Are Go.</p>' . $n;
} else {

}
?>
<?php


// Set up Link Array
$link_arr = Array();
$link_arr[0] = Array();

// Set Up Section Array
$sec_name_arr = Array();
$sec_name_arr[0]='';

$links = $xml_dom -> getElementsByTagName ('link');

foreach ($links as $link){
$link_kwds = $link -> getElementsByTagName ('kwd');
foreach ($link_kwds as $link_kwd){
if($link_kwd->nodeValue == $kwd){
$url = $link->getElementsByTagName('url')->item(0)->nodeValue;
$name = $link->getElementsByTagName('name')->item(0)->nodeValue;
$sections = $link->getElementsByTagName('sec');
$sec_elements=0;
foreach($sections as $section){
if($section->getElementsByTagName('kword')->item(0)->nodeValue==$kwd){
$sec_elements++;
}
}
if($sec_elements==0){
$link_arr[0][$name] = ('<li><a href="' . $url . '">' . $name . '</a></li>');
} else {
foreach($sections as $section){
if($section->getElementsByTagName('kword')->item(0)->nodeValue==$kwd){
$section_name = $section->getElementsByTagName('s')->item(0)->nodeValue;
$unique=true;
$sec_id=0;
for($sec_name_num=0; $sec_name_num < count($sec_name_arr); $sec_name_num++){
if($sec_name_arr[$sec_name_num] == $section_name){
$unique=false;
$link_sec_id = $sec_name_arr[$sec_name_num];
$link_arr[$link_sec_id][$name] = ('<li><a href="' . $url . '">' . $name . '</a></li>');
}
}
if($unique){
$sec_slot = count($sec_name_arr);
array_push($sec_name_arr, $section_name);
$link_sec_id = $section_name;
$link_arr[$link_sec_id] = Array();
$link_arr[$link_sec_id][$name] = ('<li><a href="' . $url . '">' . $name . '</a></li>');
}
}
}
}
}
}
}


sort($sec_name_arr);

for ($i=0; $i < count($sec_name_arr); $i++){
echo ($t3 . (($i > 0)?('<h3>' . $sec_name_arr[$i] . '</h3>' . $n . $t3):('')) . '<ul>' . $n);
if ($i==0){
ksort($link_arr[$i]);
foreach($link_arr[$i] as $hyperlink){
echo ($t4 . $hyperlink . $n);
}
} else {
ksort($link_arr[$sec_name_arr[$i]]);
foreach($link_arr[$sec_name_arr[$i]] as $hyperlink){
echo ($t4 . $hyperlink . $n);

}
}
echo ($t3 . '</ul>' . $n);
}
?>

</div>
<script type="application/javascript">
//<![CDATA[
var a1 = document.getElementById('Main').getElementsByTagName('a');
for(var i = 0; i < a1.length; i++){a1[i].setAttribute('target', '_blank');}
var a2 = document.getElementById('Menu').getElementsByTagName('a');
for(var i=0; i<a2.length; i++){(a2[i].getAttribute('class') == 'blank')?a2[i].setAttribute('target', '_blank'):''}
//]]>
</script>
</body>
</html>[/code]
×

Success!

Help @Mr_Initial_Man spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...