php-windows Digest 16 Oct 2005 09:42:46 -0000 Issue 2796
Topics (messages 26405 through 26409):
Re: [PHP] Removing Items from an Array
26405 by: Paul Menard
26406 by: Jochem Maas
Newbie
26407 by: Schalk
µç×Ó´«µ¥,×îÁ®¼ÛµÄÐû´«·½Ê½
26408 by: KOKOµç×Ó´«µ¥
Is this a PHP bug?
26409 by: Ross Honniball
Administrivia:
To subscribe to the digest, e-mail:
[EMAIL PROTECTED]
To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
Well to give my .02 here.
Why even go through the second foreach? By doing this you are making your code
very inefficient in
that it must read through the entire set of arrays. Sure it works fine if you
have only a few. But
if there are a few thousand it takes longer.
As the arrays are structures you have the key (Apples", "Oranges", "Apricots",
"Couches",
"Chairs", "Benches")
so why not just something like:
<?php
$arrset1 = array("Apples" => 3, "Oranges" => 5, "Apricots" => 1);
$arrset2 = array("Couches" => 6, "Chairs" => 2, "Benches" => 5);
$alldataarr["Fruits"] = $arrset1;
$alldataarr["Furniture"] = $arrset2;
//Say we want to remove "Chairs", and let's do it the hard way:
// Debug
//echo "initial array<pre>";
//print_r($alldataarr);
//echo "</pre>";
$delete_key = "Chairs";
foreach ($alldataarr as $key => $data)
{
if (isset($alldataarr[$key][$delete_key]))
{
unset($alldataarr[$key][$delete_key]);
}
}
// Debug
//echo "after array<pre>";
//print_r($alldataarr);
//echo "</pre>";
?>
--- [EMAIL PROTECTED] wrote:
> Thanks for the addition Jochem.. one reason I post here, even if it's a
> basic example, is
> because if there's a better way or a chance to learn something new, I want to
> have that
> opportunity. Thanks again!
>
> -TG
>
> = = = Original message = = =
>
> Id like to continue where TG left off ...
>
> hth.
>
> [EMAIL PROTECTED] wrote:
> > If I understand what you're asking, then maybe this will help:
> >
> > $arrset1 = array("Apples" => 3, "Oranges" => 5, "Apricots" => 1);
> > $arrset2 = array("Couches" => 6, "Chairs" => 2, "Benches" => 5);
> >
> > $alldataarr["Fruits"] = $arrset1;
> > $alldataarr["Furniture"] = $arrset2;
> >
> > Say we want to remove "Chairs", and let's do it the hard way:
> >
> > foreach ($alldataarr as $key => $data)
> > foreach ($data as $subkey => $subdata)
> > if ($subkey == "Chairs)
> > unset($alldataarr[$key][$subkey]);
> >
> >
> >
> >
> > using foreach $arr as $key => $data you can get the key/index name as well
> > as the actual value
> stored in that part of your array. Then all you have to do is refer back up
> to the main array
> using the current $key/$subkey values as your indexes.
> >
>
> $filter = array(
> ~'Fruits' => array('Apples' => 1, 'Oranges' => 1),
> ~'Furniture' => array('Couches' => 1, 'Chairs' => 1),
> );
>
> $alldataarr = array();
> $alldataarr["Fruits"] = array("Apples" => 3, "Oranges" => 5, "Apricots" => 1);
> $alldataarr["Furniture"] = array("Couches" => 6, "Chairs" => 2, "Benches" =>
> 5);
>
> foreach ($alldataarr as $key => $data)
> if (!isset($filter[$key])
> ~// we want it all;.
> ~continue;
>
> $alldataarr[$key]= array_intersect_keys($data, $filter[$key]);
>
>
>
> // heres one I prepared earlier:
>
>
> /**
> * array_intersect_keys()
> * ^--- the internal function (php5.x+?) has no 's'
> *
> * returns the all the items in the 1st array whose keys are found in any of
> the other arrays
> *
> * @return array()
> */
> function array_intersect_keys()
>
> $args = func_get_args();
> $originalArray = $args[0];
> $res = array();
>
> if(!is_array($originalArray)) return $res;
>
> for($i=1;$i<count($args);$i++)
> if(!is_array($args[$i])) continue;
> foreach ($args[$i] as $key => $data)
> if (isset($originalArray[$key]) && !isset($res[$key]))
> $res[$key] = $originalArray[$key];
>
>
>
>
> return $res;
>
>
>
>
>
> >
> > Basic example, but I think you can modify this to work with what you're
> > doing.
> >
> > Let me know if you have any questions about this example.
> >
> > -TG
> >
> >
> >
> > = = = Original message = = =
> >
> > Hi all,
> >
> > I'm really struggling here! I have a large, multi-dimensional array that
> > I want to "clean-up" a bit before committing to a database.
> >
> > I want to remove quite a bit of the array but using the KEYs not the
> > values. I know the keys I want to keep and I know the keys I want to get
> > rid of. I want to keep the structure and sequence of the array in tact.
> >
> > All of the array traversing functions in PHP seem to either: only work
> > on the values, or do not allow the removal of elements of the array!
> >
> > Can anyone offer a clue bat to a tired old array walker!
> >
> > Thanks
> >
> > Alan
> >
> >
> > ___________________________________________________________
> > Sent by ePrompter, the premier email notification software.
> > Free download at http://www.ePrompter.com.
> >
>
>
> ___________________________________________________________
> Sent by ePrompter, the premier email notification software.
> Free download at http://www.ePrompter.com.
>
> --
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--- End Message ---
--- Begin Message ---
[EMAIL PROTECTED] wrote:
Thanks for the addition Jochem.. one reason I post here, even if it's a basic
example, is because if there's a better way or a chance to learn something new,
I want to have that opportunity. Thanks again!
TG,
its a pleasure. I often find it fun just to write stuff like this ...
and in this particular case your first suggestion seemed to fit the bill _and_
it was
implemented in a way that the OP had a very good chance of understanding ...
I figured I'd come over the top and give him something in the same vein
that might give him a challenge :-) based on your initial post he hopefully
had enough to go on (I gather he managed to solve his problem, maybe he
is a better programmer now as well).
rgds,
Jochem
--- End Message ---
--- Begin Message ---
Greetings All
I am very new to PHP and have so far been using Dreamweaver MX to
generate the PHP code for me, please do not stone me :)
My current problem is this. Above the DOCTYPE of my document I have the
following lines to connect to the database and retrieve the results:
<?php require_once('../../Connections/jpa.php'); ?>
<?php
$colname_side_content = "-1";
if (isset($_GET['side_content'])) {
$colname_side_content = (get_magic_quotes_gpc()) ?
$_GET['side_content'] : addslashes($_GET['side_content']);
}
mysql_select_db($database_jpa, $jpa);
$query_side_content = sprintf("SELECT content_type, page_heading,
content FROM ab_side_content WHERE content_type = '%s'",
$colname_side_content);
$side_content = mysql_query($query_side_content, $jpa) or
die(mysql_error());
$row_side_content = mysql_fetch_assoc($side_content);
$totalRows_side_content = mysql_num_rows($side_content);
mysql_select_db($database_jpa, $jpa);
$query_general_clients = "SELECT id, client_name, category FROM
ab_client_list WHERE category = 'general' ORDER BY client_name ASC";
$general_clients = mysql_query($query_general_clients, $jpa) or
die(mysql_error());
$row_general_clients = mysql_fetch_assoc($general_clients);
$totalRows_general_clients = mysql_num_rows($general_clients);
mysql_select_db($database_jpa, $jpa);
$query_specialist_clients = "SELECT id, client_name, category FROM
ab_client_list WHERE category = 'Public Policy' ORDER BY client_name ASC";
$specialist_clients = mysql_query($query_specialist_clients, $jpa) or
die(mysql_error());
$row_specialist_clients = mysql_fetch_assoc($specialist_clients);
$totalRows_specialist_clients = mysql_num_rows($specialist_clients);
mysql_free_result($side_content);
mysql_free_result($general_clients);
mysql_free_result($specialist_clients);
?>
Then in my document body I have the following:
<div id="content-left-frame1">
<?php do { ?>
<?php echo $row_general_clients['client_name']; ?>
<?php } while ($row_general_clients =
mysql_fetch_assoc($general_clients)); ?>
</div>
<div id="content-left-frame2">
<h3>Public Policy & Research</h3>
<?php do { ?>
<?php echo $row_specialist_clients['client_name']; ?>
<?php } while ($row_specialist_clients =
mysql_fetch_assoc($specialist_clients)); ?>
</div>
Now, when I remove the code that is supposed to create a repeat region
everything is fine and the first result of each are printed out however,
when including the lines as above I get the following errors generated:
<div id="content-left-frame1">
42nd Street Development Corporation <br />
<b>Warning</b>: mysql_fetch_assoc(): 5 is not a valid MySQL result resource in <b>C:\Program
Files\Apache Group\Apache2\htdocs\japarker\about_us\client_list\index.php</b> on line <b>52</b><br
/>
</div>
<div id="content-left-frame2">
<h3>Public Policy & Research</h3>
American Public Transportation Association
<br />
<b>Warning</b>: mysql_fetch_assoc(): 6 is not a valid MySQL result resource in <b>C:\Program
Files\Apache Group\Apache2\htdocs\japarker\about_us\client_list\index.php</b> on line <b>59</b><br
/>
</div>
Also on the a side bar, does not get affected by the code mentioned
above, it generates:
<a href="..//japarker<br />
<b>Notice</b>: Undefined variable: row_content in <b>C:\Program Files\Apache
Group\Apache2\htdocs\japarker\about_us\client_list\index.php</b> on line <b>65</b><br />
" title="<br />
<b>Notice</b>: Undefined variable: row_content in <b>C:\Program Files\Apache
Group\Apache2\htdocs\japarker\about_us\client_list\index.php</b> on line <b>65</b><br />
">
My apologies for all of the code I just wanted to be as clear as
possible. I am not so much worried about the sidebar problem, my main
issue is the main content. Can anyone see where this is going wrong? I
would appreciate any help, thank you in advance.
--
Kind Regards
Schalk Neethling
Web Developer.Designer.Programmer.President
Volume4.Business.Solution.Developers
--- End Message ---
--- Begin Message ---
钟万仇也站了起来,道:“是!”突然转头,狠狠瞪了段正淳一眼,叹道:“段正淳,你已有了这样的好老婆、好儿子,怎地兀自贪心不足?今日声名扫地,丢尽脸面,是你自作自受,须怪我钟万仇不得。”
小船在碧琉璃般的湖面上滑过,舟中五个少女中三人十五六岁上下,另外两个都只九岁。两个幼女是中表之亲,表姊姓程,单名一个英字,表妹姓陆,名无双。两人相差半岁。
--- End Message ---
--- Begin Message ---
$x = 0; // Numeric zero
$y = 'Some kind of string';
if ($x == $y) echo 'they equal using ==';
if ($x === $y) echo 'they equal using ===';
The above will echo 'they equal using =='.
The values don't look very equal to me.
Can anyone explain the logic behind this?
I'm heading home now but look forward to your explanations tomorrow.
PS
Incidently, to 'fix' it so it behaves as it should, you can code:
if ($x.'' == $y.'') echo 'this will not print and all is good.';
Regards .. Ross
--- End Message ---