FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » how to join two arrays?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
how to join two arrays? [message #185704] Sat, 03 May 2014 17:25 Go to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
I am building an array by scannning ten directories.
With each scanned directory, the output is stored in one array.
How do I properly join that array with a second array?

Without duplicate keys!

Array_merge works fine but keys are duplicated.
Re: how to join two arrays? [message #185710 is a reply to message #185704] Sat, 03 May 2014 20:07 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
On 03/05/14 19:25, richard wrote:
> I am building an array by scannning ten directories.
> With each scanned directory, the output is stored in one array.
> How do I properly join that array with a second array?
>
> Without duplicate keys!
>
> Array_merge works fine but keys are duplicated.

Keys are always unique, see example:


<?php

$a1 = array("a" => 1, "b" => 2, "c" => 3);

$a2 = array("a" => 10, "d" => 4, "e" => 5);

$a3 = array_merge($a1, $a2);

var_dump($a1);
var_dump($a2);
var_dump($a3);

echo $a3["a"]."\n";

?>

If you haven't named the cells, then it will just merge the arrays
(append one after the other).


--

//Aho
Re: how to join two arrays? [message #185713 is a reply to message #185704] Sat, 03 May 2014 22:23 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sat, 03 May 2014 13:25:10 -0400, richard wrote:

> I am building an array by scannning ten directories.
> With each scanned directory, the output is stored in one array.
> How do I properly join that array with a second array?
>
> Without duplicate keys!
>
> Array_merge works fine but keys are duplicated.

scandir creates numerically indexed arrays. Merging these arrays will
create a larger numerically indexed array, eg if you merge two arrays
with keys [0] through [9], you end up with a single array with keys [0]
through [19].

eg if the first dir generated:

$arr1

[0] "."
[1] ".."
[2] "foo.bar"
[3] "really fubar"

and the second dir generated

$arr2

[0] "."
[1] ".."
[2] "bah bah.blacksheep"
[3] "fufu.fubar"

then: array_merge ( $arr1, $arr2 );

will generate:

[0] "."
[1] ".."
[2] "foo.bar"
[3] "really fubar"
[4] "."
[5] ".."
[6] "bah bah.blacksheep"
[7] "fufu.fubar"

How you then tell which file is where, that's your headache at this point.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: how to join two arrays? [message #185714 is a reply to message #185713] Sun, 04 May 2014 17:56 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Denis McMahon wrote:

> then: array_merge ( $arr1, $arr2 );
>
> will generate:
>
> [0] "."
> [1] ".."
> [2] "foo.bar"
> [3] "really fubar"
> [4] "."
> [5] ".."
> [6] "bah bah.blacksheep"
> [7] "fufu.fubar"
>
> How you then tell which file is where, that's your headache at this point.

array_map() serves here.


PointedEars
--
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
-- Richard Cornford, comp.lang.javascript, 2011-11-14
Re: how to join two arrays? [message #185715 is a reply to message #185714] Sun, 04 May 2014 19:17 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/4/2014 1:56 PM, Thomas 'PointedEars' Lahn wrote:
> Denis McMahon wrote:
>
>> then: array_merge ( $arr1, $arr2 );
>>
>> will generate:
>>
>> [0] "."
>> [1] ".."
>> [2] "foo.bar"
>> [3] "really fubar"
>> [4] "."
>> [5] ".."
>> [6] "bah bah.blacksheep"
>> [7] "fufu.fubar"
>>
>> How you then tell which file is where, that's your headache at this point.
>
> array_map() serves here.
>
>
> PointedEars
>

And how can array_map() tell which file is where?


--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: how to join two arrays? [message #185716 is a reply to message #185715] Sun, 04 May 2014 19:47 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Jerry Stuckle wrote:

> On 5/4/2014 1:56 PM, Thomas 'PointedEars' Lahn wrote:
>> Denis McMahon wrote:
>>> then: array_merge ( $arr1, $arr2 );
>>>
>>> will generate:
>>>
>>> [0] "."
>>> [1] ".."
>>> [2] "foo.bar"
>>> [3] "really fubar"
>>> [4] "."
>>> [5] ".."
>>> [6] "bah bah.blacksheep"
>>> [7] "fufu.fubar"
>>>
>>> How you then tell which file is where, that's your headache at this
>>> point.
>>
>> array_map() serves here.
>
> And how can array_map() tell which file is where?

It cannot. However, if you apply array_map() to the arrays returned by
scandir() before merging them, the problem at hand does not occur in the
first place:

$modi = max(
array_map('filemtime',
array_diff(
array_merge(
scandir(__DIR__),
array_map(
function ($e) {
return 'sections' . DIRECTORY_SEPARATOR . $e;
},
scandir('sections')
)
),
array('.', '..')
)
)
);


HTH

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Re: how to join two arrays? [message #185717 is a reply to message #185716] Sun, 04 May 2014 20:48 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/4/2014 3:47 PM, Thomas 'PointedEars' Lahn wrote:
> Jerry Stuckle wrote:
>
>> On 5/4/2014 1:56 PM, Thomas 'PointedEars' Lahn wrote:
>>> Denis McMahon wrote:
>>>> then: array_merge ( $arr1, $arr2 );
>>>>
>>>> will generate:
>>>>
>>>> [0] "."
>>>> [1] ".."
>>>> [2] "foo.bar"
>>>> [3] "really fubar"
>>>> [4] "."
>>>> [5] ".."
>>>> [6] "bah bah.blacksheep"
>>>> [7] "fufu.fubar"
>>>>
>>>> How you then tell which file is where, that's your headache at this
>>>> point.
>>>
>>> array_map() serves here.
>>
>> And how can array_map() tell which file is where?
>
> It cannot.

Then why did you say it could?

However, if you apply array_map() to the arrays returned by
> scandir() before merging them, the problem at hand does not occur in the
> first place:
>
> $modi = max(
> array_map('filemtime',
> array_diff(
> array_merge(
> scandir(__DIR__),
> array_map(
> function ($e) {
> return 'sections' . DIRECTORY_SEPARATOR . $e;
> },
> scandir('sections')
> )
> ),
> array('.', '..')
> )
> )
> );
>
>
> HTH
>
> PointedEars
>

What a convoluted mess. Glad you're not working on any code I have to
touch!

But then you always did have a knack for making a mountain out of a
molehill.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: how to join two arrays? [message #185718 is a reply to message #185717] Sun, 04 May 2014 21:02 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Jerry Stuckle wrote:

> On 5/4/2014 3:47 PM, Thomas 'PointedEars' Lahn wrote:
>> Jerry Stuckle wrote:
>>> On 5/4/2014 1:56 PM, Thomas 'PointedEars' Lahn wrote:
>>>> Denis McMahon wrote:
>>>> > then: array_merge ( $arr1, $arr2 );
>>>> >
>>>> > will generate:
>>>> >
>>>> > [0] "."
>>>> > [1] ".."
>>>> > [2] "foo.bar"
>>>> > [3] "really fubar"
>>>> > [4] "."
>>>> > [5] ".."
>>>> > [6] "bah bah.blacksheep"
>>>> > [7] "fufu.fubar"
>>>> >
>>>> > How you then tell which file is where, that's your headache at this
>>>> > point.
>>>>
>>>> array_map() serves here.
>>>
>>> And how can array_map() tell which file is where?
>>
>> It cannot.
>
> Then why did you say it could?

I did not.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
Re: how to join two arrays? [message #185719 is a reply to message #185718] Sun, 04 May 2014 21:51 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/4/2014 5:02 PM, Thomas 'PointedEars' Lahn wrote:
> Jerry Stuckle wrote:
>
>> On 5/4/2014 3:47 PM, Thomas 'PointedEars' Lahn wrote:
>>> Jerry Stuckle wrote:
>>>> On 5/4/2014 1:56 PM, Thomas 'PointedEars' Lahn wrote:
>>>> > Denis McMahon wrote:
>>>> >> then: array_merge ( $arr1, $arr2 );
>>>> >>
>>>> >> will generate:
>>>> >>
>>>> >> [0] "."
>>>> >> [1] ".."
>>>> >> [2] "foo.bar"
>>>> >> [3] "really fubar"
>>>> >> [4] "."
>>>> >> [5] ".."
>>>> >> [6] "bah bah.blacksheep"
>>>> >> [7] "fufu.fubar"
>>>> >>
>>>> >> How you then tell which file is where, that's your headache at this
>>>> >> point.
>>>> >
>>>> > array_map() serves here.
>>>>
>>>> And how can array_map() tell which file is where?
>>>
>>> It cannot.
>>
>> Then why did you say it could?
>
> I did not.
>
>
> PointedEars
>

You can't even read what you wrote... Even though it's part of this
message above, I'll repeat it for you:

>> How you then tell which file is where, that's your headache at this
>> point.
>
> array_map() serves here.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: how to join two arrays? [message #185721 is a reply to message #185719] Sun, 04 May 2014 22:55 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Jerry Stuckle wrote:

> On 5/4/2014 5:02 PM, Thomas 'PointedEars' Lahn wrote:
>> Jerry Stuckle wrote:
>>> On 5/4/2014 3:47 PM, Thomas 'PointedEars' Lahn wrote:
>>>> Jerry Stuckle wrote:
>>>> > On 5/4/2014 1:56 PM, Thomas 'PointedEars' Lahn wrote:
>>>> >> Denis McMahon wrote:
>>>> >>> then: array_merge ( $arr1, $arr2 );
>>>> >>>
>>>> >>> will generate:
>>>> >>>
>>>> >>> [0] "."
>>>> >>> [1] ".."
>>>> >>> [2] "foo.bar"
>>>> >>> [3] "really fubar"
>>>> >>> [4] "."
>>>> >>> [5] ".."
>>>> >>> [6] "bah bah.blacksheep"
>>>> >>> [7] "fufu.fubar"
>>>> >>>
>>>> >>> How you then tell which file is where, that's your headache at this
>>>> >>> point.
>>>> >> array_map() serves here.
>>>> > And how can array_map() tell which file is where?
>>>> It cannot.
>>> Then why did you say it could?
>> I did not.
>
> You can't even read what you wrote...

I know what I wrote. I also know that – granting you the benefit of a
doubt one more time – you are not trolling, but that you have misinterpreted
what I wrote. I have written (as can be read in the quotation above, so
there is no need for you or me to repeat it below) that “array_map() serves
here”. It does. I have, upon your query, given an example showing what I
meant with “serves here” from a real-life example (hence “here”).
Apparently you have not bothered to understand the example, as you had
decided to deride it in yet another ad-hominem fallacy of yours.

I have made myself clear now to all who can read carefully and think
clearly. Further misinterpretation on your part will be considered
deliberate, aka trolling, and dealt with accordingly.


HTH

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
solved (was: how to join two arrays?) [message #185722 is a reply to message #185704] Sun, 04 May 2014 23:54 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Sat, 3 May 2014 13:25:10 -0400, richard wrote:

> I am building an array by scannning ten directories.
> With each scanned directory, the output is stored in one array.
> How do I properly join that array with a second array?
>
> Without duplicate keys!
>
> Array_merge works fine but keys are duplicated.


$lo=0;
$hi=0;

for ($i = 59; $i <= 69; $i++) {
$yr="19$i";
echo $i;
echo $yr;
echo "<br>";

$dir='../audio/'.$yr.'/';
$files = scandir($dir);
$number=count($files);
echo $number;
echo "<br>";
sort($files);
$hi=$hi+$number;
echo $hi;
echo "<br>";

for ($x=0;$x<=$number;$x++){echo $files[$x];
$master[$lo]=$files[$x];
$lo=$lo+1;

}
$lo=$hi;
}

for ($x=0;$x<=$hi;$x++){echo $master[$x]."<br>";}

the echos are there primarily to show what the progress is.
they will be removed for final output.
Re: how to join two arrays? [message #185723 is a reply to message #185721] Mon, 05 May 2014 01:31 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/4/2014 6:55 PM, Thomas 'PointedEars' Lahn wrote:
> Jerry Stuckle wrote:
>
>> On 5/4/2014 5:02 PM, Thomas 'PointedEars' Lahn wrote:
>>> Jerry Stuckle wrote:
>>>> On 5/4/2014 3:47 PM, Thomas 'PointedEars' Lahn wrote:
>>>> > Jerry Stuckle wrote:
>>>> >> On 5/4/2014 1:56 PM, Thomas 'PointedEars' Lahn wrote:
>>>> >>> Denis McMahon wrote:
>>>> >>>> then: array_merge ( $arr1, $arr2 );
>>>> >>>>
>>>> >>>> will generate:
>>>> >>>>
>>>> >>>> [0] "."
>>>> >>>> [1] ".."
>>>> >>>> [2] "foo.bar"
>>>> >>>> [3] "really fubar"
>>>> >>>> [4] "."
>>>> >>>> [5] ".."
>>>> >>>> [6] "bah bah.blacksheep"
>>>> >>>> [7] "fufu.fubar"
>>>> >>>>
>>>> >>>> How you then tell which file is where, that's your headache at this
>>>> >>>> point.
>>>> >>> array_map() serves here.
>>>> >> And how can array_map() tell which file is where?
>>>> > It cannot.
>>>> Then why did you say it could?
>>> I did not.
>>
>> You can't even read what you wrote...
>
> I know what I wrote. I also know that – granting you the benefit of a
> doubt one more time – you are not trolling, but that you have misinterpreted
> what I wrote. I have written (as can be read in the quotation above, so
> there is no need for you or me to repeat it below) that “array_map() serves
> here”. It does. I have, upon your query, given an example showing what I
> meant with “serves here” from a real-life example (hence “here”).
> Apparently you have not bothered to understand the example, as you had
> decided to deride it in yet another ad-hominem fallacy of yours.
>
> I have made myself clear now to all who can read carefully and think
> clearly. Further misinterpretation on your part will be considered
> deliberate, aka trolling, and dealt with accordingly.
>
>
> HTH
>
> PointedEars
>

There was nothing to misinterpret. array_map() does Not solve the
problem of "How you then tell which file is where, that's your headache
at this point.", which is the question it is response to.

But we already know you're too stoopid to understand, and too
egotistical to admit you screwed up (again).

And yes, you made yourself clear. You proved you have no idea how to
solve this problem.

So, do everyone a favor and find another newsgroup to troll. You're
know as one in enough newsgroups that shouldn't be a problem.

As for suggestion someone else is a troll? That is the best laugh I've
heard of all year. It's even better than suggesting Richard is competent.


--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: solved [message #185724 is a reply to message #185722] Mon, 05 May 2014 02:00 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Sun, 4 May 2014 19:54:32 -0400, richard wrote:

> On Sat, 3 May 2014 13:25:10 -0400, richard wrote:
>
>> I am building an array by scannning ten directories.
>> With each scanned directory, the output is stored in one array.
>> How do I properly join that array with a second array?
>>
>> Without duplicate keys!
>>
>> Array_merge works fine but keys are duplicated.
>
>
> $lo=0;
> $hi=0;
>
> for ($i = 59; $i <= 69; $i++) {
> $yr="19$i";
> echo $i;
> echo $yr;
> echo "<br>";
>
> $dir='../audio/'.$yr.'/';
> $files = scandir($dir);
> $number=count($files);
> echo $number;
> echo "<br>";
> sort($files);
> $hi=$hi+$number;
> echo $hi;
> echo "<br>";
>
> for ($x=0;$x<=$number;$x++){echo $files[$x];
> $master[$lo]=$files[$x];
> $lo=$lo+1;
>
> }
> $lo=$hi;
> }
>
> for ($x=0;$x<=$hi;$x++){echo $master[$x]."<br>";}
>
> the echos are there primarily to show what the progress is.
> they will be removed for final output.

Questio I have now is, how do I get it so the leading "." and ".." are not
in the final array?
I tried using a conditional with !="." and that did not work.
Re: how to join two arrays? [message #185725 is a reply to message #185723] Mon, 05 May 2014 02:13 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/4/2014 9:31 PM, Jerry Stuckle wrote:
> On 5/4/2014 6:55 PM, Thomas 'PointedEars' Lahn wrote:
>> Jerry Stuckle wrote:
>>
>>> On 5/4/2014 5:02 PM, Thomas 'PointedEars' Lahn wrote:
>>>> Jerry Stuckle wrote:
>>>> > On 5/4/2014 3:47 PM, Thomas 'PointedEars' Lahn wrote:
>>>> >> Jerry Stuckle wrote:
>>>> >>> On 5/4/2014 1:56 PM, Thomas 'PointedEars' Lahn wrote:
>>>> >>>> Denis McMahon wrote:
>>>> >>>>> then: array_merge ( $arr1, $arr2 );
>>>> >>>>>
>>>> >>>>> will generate:
>>>> >>>>>
>>>> >>>>> [0] "."
>>>> >>>>> [1] ".."
>>>> >>>>> [2] "foo.bar"
>>>> >>>>> [3] "really fubar"
>>>> >>>>> [4] "."
>>>> >>>>> [5] ".."
>>>> >>>>> [6] "bah bah.blacksheep"
>>>> >>>>> [7] "fufu.fubar"
>>>> >>>>>
>>>> >>>>> How you then tell which file is where, that's your headache at
>>>> >>>>> this
>>>> >>>>> point.
>>>> >>>> array_map() serves here.
>>>> >>> And how can array_map() tell which file is where?
>>>> >> It cannot.
>>>> > Then why did you say it could?
>>>> I did not.
>>>
>>> You can't even read what you wrote...
>>
>> I know what I wrote. I also know that – granting you the benefit of a
>> doubt one more time – you are not trolling, but that you have
>> misinterpreted
>> what I wrote. I have written (as can be read in the quotation above, so
>> there is no need for you or me to repeat it below) that “array_map()
>> serves
>> here”. It does. I have, upon your query, given an example showing
>> what I
>> meant with “serves here” from a real-life example (hence “here”).
>> Apparently you have not bothered to understand the example, as you had
>> decided to deride it in yet another ad-hominem fallacy of yours.
>>
>> I have made myself clear now to all who can read carefully and think
>> clearly. Further misinterpretation on your part will be considered
>> deliberate, aka trolling, and dealt with accordingly.
>>
>>
>> HTH
>>
>> PointedEars
>>
>
> There was nothing to misinterpret. array_map() does Not solve the
> problem of "How you then tell which file is where, that's your headache
> at this point.", which is the question it is response to.
>
> But we already know you're too stoopid to understand, and too
> egotistical to admit you screwed up (again).
>
> And yes, you made yourself clear. You proved you have no idea how to
> solve this problem.
>
> So, do everyone a favor and find another newsgroup to troll. You're
> know as one in enough newsgroups that shouldn't be a problem.
>
> As for suggestion someone else is a troll? That is the best laugh I've
> heard of all year. It's even better than suggesting Richard is competent.
>
>

Looking over my last comment, I have to apologize. It wasn't fair to
Richard. Sorry, Richard, I shouldn't have insulted you like that.



--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: solved [message #185726 is a reply to message #185724] Mon, 05 May 2014 02:27 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/4/2014 10:00 PM, richard wrote:
> On Sun, 4 May 2014 19:54:32 -0400, richard wrote:
>
>> On Sat, 3 May 2014 13:25:10 -0400, richard wrote:
>>
>>> I am building an array by scannning ten directories.
>>> With each scanned directory, the output is stored in one array.
>>> How do I properly join that array with a second array?
>>>
>>> Without duplicate keys!
>>>
>>> Array_merge works fine but keys are duplicated.
>>
>>
>> $lo=0;
>> $hi=0;
>>
>> for ($i = 59; $i <= 69; $i++) {
>> $yr="19$i";
>> echo $i;
>> echo $yr;
>> echo "<br>";
>>
>> $dir='../audio/'.$yr.'/';
>> $files = scandir($dir);
>> $number=count($files);
>> echo $number;
>> echo "<br>";
>> sort($files);
>> $hi=$hi+$number;
>> echo $hi;
>> echo "<br>";
>>
>> for ($x=0;$x<=$number;$x++){echo $files[$x];
>> $master[$lo]=$files[$x];
>> $lo=$lo+1;
>>
>> }
>> $lo=$hi;
>> }
>>
>> for ($x=0;$x<=$hi;$x++){echo $master[$x]."<br>";}
>>
>> the echos are there primarily to show what the progress is.
>> they will be removed for final output.
>
> Questio I have now is, how do I get it so the leading "." and ".." are not
> in the final array?
> I tried using a conditional with !="." and that did not work.
>

Richard,

If written correctly a conditional for !="." would get rid of the '.'
entry; !=".." would get rid of the ".." entry.

But you didn't show your entire code, so it's hard to say what could be
wrong.


--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: solved [message #185727 is a reply to message #185726] Mon, 05 May 2014 03:07 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Sun, 04 May 2014 22:27:01 -0400, Jerry Stuckle wrote:

> On 5/4/2014 10:00 PM, richard wrote:
>> On Sun, 4 May 2014 19:54:32 -0400, richard wrote:
>>
>>> On Sat, 3 May 2014 13:25:10 -0400, richard wrote:
>>>
>>>> I am building an array by scannning ten directories.
>>>> With each scanned directory, the output is stored in one array.
>>>> How do I properly join that array with a second array?
>>>>
>>>> Without duplicate keys!
>>>>
>>>> Array_merge works fine but keys are duplicated.
>>>
>>>
>>> $lo=0;
>>> $hi=0;
>>>
>>> for ($i = 59; $i <= 69; $i++) {
>>> $yr="19$i";
>>> echo $i;
>>> echo $yr;
>>> echo "<br>";
>>>
>>> $dir='../audio/'.$yr.'/';
>>> $files = scandir($dir);
>>> $number=count($files);
>>> echo $number;
>>> echo "<br>";
>>> sort($files);
>>> $hi=$hi+$number;
>>> echo $hi;
>>> echo "<br>";
>>>
>>> for ($x=0;$x<=$number;$x++){echo $files[$x];
>>> $master[$lo]=$files[$x];
>>> $lo=$lo+1;
>>>
>>> }
>>> $lo=$hi;
>>> }
>>>
>>> for ($x=0;$x<=$hi;$x++){echo $master[$x]."<br>";}
>>>
>>> the echos are there primarily to show what the progress is.
>>> they will be removed for final output.
>>
>> Questio I have now is, how do I get it so the leading "." and ".." are not
>> in the final array?
>> I tried using a conditional with !="." and that did not work.
>>
>
> Richard,
>
> If written correctly a conditional for !="." would get rid of the '.'
> entry; !=".." would get rid of the ".." entry.
>
> But you didn't show your entire code, so it's hard to say what could be
> wrong.


for ($x=0;$x<=$number;$x++){

if ($files[x] !=".") {
$master[$lo]=$files[$x];
$lo=$lo+1;
}
}
Re: solved [message #185728 is a reply to message #185727] Mon, 05 May 2014 10:11 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
richard <noreply(at)example(dot)com> writes:

> On Sun, 04 May 2014 22:27:01 -0400, Jerry Stuckle wrote:
<snip>
>> But you didn't show your entire code, so it's hard to say what could be
>> wrong.
>
> for ($x=0;$x<=$number;$x++){
>
> if ($files[x] !=".") {
^ not $x

> $master[$lo]=$files[$x];
> $lo=$lo+1;
> }
> }

You really need to find some what to work which lets you see all the
notices and warnings that PHP can give you.

By the way, writing $master[] = ... puts an element at the next unused
numerical index in the array which might be what you are doing here with
$lo. I'd write the above like this:

foreach ($files as $file)
if ($file !== '.')
$master[] = $file;

--
Ben.
Re: solved [message #185729 is a reply to message #185727] Mon, 05 May 2014 13:36 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sun, 04 May 2014 23:07:55 -0400, richard wrote:

> On Sun, 04 May 2014 22:27:01 -0400, Jerry Stuckle wrote:

>> If written correctly a conditional for !="." would get rid of the '.'
>> entry; !=".." would get rid of the ".." entry.
>>
>> But you didn't show your entire code, so it's hard to say what could be
>> wrong.

> for ($x=0;$x<=$number;$x++){
> if ($files[x] !=".") {

As Ben pointed out, missing $ from [$x] in $files[x], but that's just
your usual broken code.

> $master[$lo]=$files[$x];
> $lo=$lo+1;
> }
> }

for ( $x=0; $x <= $number; $x++ )
if ( $files[$x] != "." && $files[$x] != ".." )
$master[$lo++] = $files[$x];

The line:

$master[$lo++] = $files[$x];

does the following:

First it assigns the value of $files[$x] to $master[$lo]
Then it increments $lo by 1

And as Ben pointed out, it's possible that the following will work too:

for ( $x=0; $x <= $number; $x++ )
if ( $files[$x] != "." && $files[$x] != ".." )
$master[] = $files[$x];

As it just increments the numeric index of $master by 1 each time it
assigns another element.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: solved [message #185730 is a reply to message #185728] Mon, 05 May 2014 13:42 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Mon, 05 May 2014 11:11:35 +0100, Ben Bacarisse wrote:

> richard <noreply(at)example(dot)com> writes:
>
>> On Sun, 04 May 2014 22:27:01 -0400, Jerry Stuckle wrote:
> <snip>
>>> But you didn't show your entire code, so it's hard to say what could be
>>> wrong.
>>
>> for ($x=0;$x<=$number;$x++){
>>
>> if ($files[x] !=".") {
> ^ not $x
>
>> $master[$lo]=$files[$x];
>> $lo=$lo+1;
>> }
>> }
>
> You really need to find some what to work which lets you see all the
> notices and warnings that PHP can give you.
>
> By the way, writing $master[] = ... puts an element at the next unused
> numerical index in the array which might be what you are doing here with
> $lo. I'd write the above like this:
>
> foreach ($files as $file)
> if ($file !== '.')
> $master[] = $file;

Thanks. Works like a charm.
Now how do I also include the ".."?
foreach ($files as $file)
if ($file !== '.' || $file!=="..")
$master[] = $file;
This does not seem to work.

Time to implement the formidable goto eh?
Re: solved [message #185731 is a reply to message #185730] Mon, 05 May 2014 13:51 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
richard wrote:

> On Mon, 05 May 2014 11:11:35 +0100, Ben Bacarisse wrote:
>
>> richard <noreply(at)example(dot)com> writes:
>>
>>> On Sun, 04 May 2014 22:27:01 -0400, Jerry Stuckle wrote:
>> <snip>
>>>> But you didn't show your entire code, so it's hard to say what could be
>>>> wrong.
>>>
>>> for ($x=0;$x<=$number;$x++){
>>>
>>> if ($files[x] !=".") {
>> ^ not $x
>>
>>> $master[$lo]=$files[$x];
>>> $lo=$lo+1;
>>> }
>>> }
>>
>> You really need to find some what to work which lets you see all the
>> notices and warnings that PHP can give you.
>>
>> By the way, writing $master[] = ... puts an element at the next unused
>> numerical index in the array which might be what you are doing here with
>> $lo. I'd write the above like this:
>>
>> foreach ($files as $file)
>> if ($file !== '.')
>> $master[] = $file;
>
> Thanks. Works like a charm.
> Now how do I also include the ".."?
> foreach ($files as $file)
> if ($file !== '.' || $file!=="..")
> $master[] = $file;
> This does not seem to work.

I assume that it does what it should: it will append any file whose name
is not equal to '.' *or* not equal to '..', i.e. every file. You want
to use the && operator here.

Another option which excludes all hidden files:

if ($file[0] != '.')

> Time to implement the formidable goto eh?

Fortunately not.

--
Christoph M. Becker
Re: solved [message #185733 is a reply to message #185730] Mon, 05 May 2014 14:26 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/5/2014 9:42 AM, richard wrote:
> On Mon, 05 May 2014 11:11:35 +0100, Ben Bacarisse wrote:
>
>> richard <noreply(at)example(dot)com> writes:
>>
>>> On Sun, 04 May 2014 22:27:01 -0400, Jerry Stuckle wrote:
>> <snip>
>>>> But you didn't show your entire code, so it's hard to say what could be
>>>> wrong.
>>>
>>> for ($x=0;$x<=$number;$x++){
>>>
>>> if ($files[x] !=".") {
>> ^ not $x
>>
>>> $master[$lo]=$files[$x];
>>> $lo=$lo+1;
>>> }
>>> }
>>
>> You really need to find some what to work which lets you see all the
>> notices and warnings that PHP can give you.
>>
>> By the way, writing $master[] = ... puts an element at the next unused
>> numerical index in the array which might be what you are doing here with
>> $lo. I'd write the above like this:
>>
>> foreach ($files as $file)
>> if ($file !== '.')
>> $master[] = $file;
>
> Thanks. Works like a charm.
> Now how do I also include the ".."?
> foreach ($files as $file)
> if ($file !== '.' || $file!=="..")
> $master[] = $file;
> This does not seem to work.
>
> Time to implement the formidable goto eh?
>

You're close. You want to add it if $file != "." AND $file != "..".

If $file equals ".", it cannot equal "..", and vice versa. So the test
you have is always true.

So what you want is

if ($file !== "." && $file !== "..")
....


--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: solved [message #185735 is a reply to message #185733] Mon, 05 May 2014 14:34 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Mon, 05 May 2014 10:26:51 -0400, Jerry Stuckle wrote:

> On 5/5/2014 9:42 AM, richard wrote:
>> On Mon, 05 May 2014 11:11:35 +0100, Ben Bacarisse wrote:
>>
>>> richard <noreply(at)example(dot)com> writes:
>>>
>>>> On Sun, 04 May 2014 22:27:01 -0400, Jerry Stuckle wrote:
>>> <snip>
>>>> > But you didn't show your entire code, so it's hard to say what could be
>>>> > wrong.
>>>>
>>>> for ($x=0;$x<=$number;$x++){
>>>>
>>>> if ($files[x] !=".") {
>>> ^ not $x
>>>
>>>> $master[$lo]=$files[$x];
>>>> $lo=$lo+1;
>>>> }
>>>> }
>>>
>>> You really need to find some what to work which lets you see all the
>>> notices and warnings that PHP can give you.
>>>
>>> By the way, writing $master[] = ... puts an element at the next unused
>>> numerical index in the array which might be what you are doing here with
>>> $lo. I'd write the above like this:
>>>
>>> foreach ($files as $file)
>>> if ($file !== '.')
>>> $master[] = $file;
>>
>> Thanks. Works like a charm.
>> Now how do I also include the ".."?
>> foreach ($files as $file)
>> if ($file !== '.' || $file!=="..")
>> $master[] = $file;
>> This does not seem to work.
>>
>> Time to implement the formidable goto eh?
>>
>
> You're close. You want to add it if $file != "." AND $file != "..".
>
> If $file equals ".", it cannot equal "..", and vice versa. So the test
> you have is always true.
>
> So what you want is
>
> if ($file !== "." && $file !== "..")
> ...

thank you sir that worked perfectly.
Re: how to join two arrays? [message #185749 is a reply to message #185725] Mon, 05 May 2014 19:45 Go to previous messageGo to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
On Sun, 04 May 2014 22:13:32 -0400, Jerry Stuckle
<jstucklex(at)attglobal(dot)net> wrote:

> On 5/4/2014 9:31 PM, Jerry Stuckle wrote:
>> On 5/4/2014 6:55 PM, Thomas 'PointedEars' Lahn wrote:
>>> Jerry Stuckle wrote:
>>>
>>>> On 5/4/2014 5:02 PM, Thomas 'PointedEars' Lahn wrote:
>>>> > Jerry Stuckle wrote:
>>>> >> On 5/4/2014 3:47 PM, Thomas 'PointedEars' Lahn wrote:
>>>> >>> Jerry Stuckle wrote:
>>>> >>>> On 5/4/2014 1:56 PM, Thomas 'PointedEars' Lahn wrote:
>>>> >>>>> Denis McMahon wrote:
>>>> >>>>>> then: array_merge ( $arr1, $arr2 );
>>>> >>>>>>
>>>> >>>>>> will generate:
>>>> >>>>>>
>>>> >>>>>> [0] "."
>>>> >>>>>> [1] ".."
>>>> >>>>>> [2] "foo.bar"
>>>> >>>>>> [3] "really fubar"
>>>> >>>>>> [4] "."
>>>> >>>>>> [5] ".."
>>>> >>>>>> [6] "bah bah.blacksheep"
>>>> >>>>>> [7] "fufu.fubar"
>>>> >>>>>>
>>>> >>>>>> How you then tell which file is where, that's your headache at
>>>> >>>>>> this
>>>> >>>>>> point.
>>>> >>>>> array_map() serves here.
>>>> >>>> And how can array_map() tell which file is where?
>>>> >>> It cannot.
>>>> >> Then why did you say it could?
>>>> > I did not.
>>>>
>>>> You can't even read what you wrote...
>>>
>>> I know what I wrote. I also know that – granting you the benefit of a
>>> doubt one more time – you are not trolling, but that you have
>>> misinterpreted
>>> what I wrote. I have written (as can be read in the quotation above, so
>>> there is no need for you or me to repeat it below) that “array_map()
>>> serves
>>> here”. It does. I have, upon your query, given an example showing
>>> what I
>>> meant with “serves here” from a real-life example (hence “here”).
>>> Apparently you have not bothered to understand the example, as you had
>>> decided to deride it in yet another ad-hominem fallacy of yours.
>>>
>>> I have made myself clear now to all who can read carefully and think
>>> clearly. Further misinterpretation on your part will be considered
>>> deliberate, aka trolling, and dealt with accordingly.
>>>
>>>
>>> HTH
>>>
>>> PointedEars
>>>
>>
>> There was nothing to misinterpret. array_map() does Not solve the
>> problem of "How you then tell which file is where, that's your headache
>> at this point.", which is the question it is response to.
>>
>> But we already know you're too stoopid to understand, and too
>> egotistical to admit you screwed up (again).
>>
>> And yes, you made yourself clear. You proved you have no idea how to
>> solve this problem.
>>
>> So, do everyone a favor and find another newsgroup to troll. You're
>> know as one in enough newsgroups that shouldn't be a problem.
>>
>> As for suggestion someone else is a troll? That is the best laugh I've
>> heard of all year. It's even better than suggesting Richard is competent.
>>
>>
>
> Looking over my last comment, I have to apologize. It wasn't fair to
> Richard. Sorry, Richard, I shouldn't have insulted you like that.

Case sensitivity please!
Re: solved [message #185751 is a reply to message #185730] Tue, 06 May 2014 00:55 Go to previous message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
richard <noreply(at)example(dot)com> wrote in news:1u66bqszrlp57$.2ce1bbokt163.dlg@
40tude.net:

[...]
> if ($file !== '.' || $file!=="..")
> $master[] = $file;
> This does not seem to work.

Depends on your definition of "work", I suppose. It definitely does work, in the sense that it
certainly is doing what you told it to. I'm pretty sure it's not doing what you expected it to do,
but that's because your expectations don't match your code.

Consider what happens when $file is equal to "." The first part of your compound condition
is FALSE. The second part is TRUE.

FALSE || TRUE is TRUE.

Now what happens when $file is equal to "..". The first part is TRUE, and the second part is
FALSE.

TRUE || FALSE is TRUE.

Finally, if $file is equal to, say, "abc". Now both parts of the condition are TRUE.

TRUE || TRUE is TRUE.

The results you *want* are FALSE, FALSE, TRUE respectively -- that is, you want the result
to be FALSE when *either* of its inputs is FALSE, and TRUE only if *both* of its inputs are
TRUE. That describes &&, not ||.

>
> Time to implement the formidable goto eh?

No, it's time to fix your broken logic. Google "De Morgan's Laws" for more information.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: count() problem
Next Topic: part two same issue - unwanted empty values in array
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed May 29 04:25:04 GMT 2024

Total time taken to generate the page: 0.02868 seconds