473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

array_join

Hi,
I'm trying to do this:

$array1 = array("a" => 1, "b" => 2);
$array2 = array("a" => 2, "b" => 4);
$array3 = array_join($array1, $arra2);

Output should be:
$array3 = (
[a] => 3
[b] => 6
)

with this:
function array_join($array1, $array2) {
foreach($array2 as $key => $value) {
if(array_key_exists($key, $array1)) {
$array1[$key] .= $array2[$key];
}
else {
$array[$key] = $array2[$key];
}
}
return $array1;
}
But it doesn't quite work. Can somebody help a bit?
It gives a warning like this:

Warning: Invalid argument supplied for foreach() in
C:\Programs\work\jobs\index.php on line 63

Shmuel.
Jul 17 '05 #1
2 8035
"Shmuel" <sd*@nic.fi> wrote in message
news:R7****************@reader1.news.jippii.net...
Hi,
I'm trying to do this:

$array1 = array("a" => 1, "b" => 2);
$array2 = array("a" => 2, "b" => 4);
$array3 = array_join($array1, $arra2);

Output should be:
$array3 = (
[a] => 3
[b] => 6
)

with this:
function array_join($array1, $array2) {
foreach($array2 as $key => $value) {
if(array_key_exists($key, $array1)) {
$array1[$key] .= $array2[$key];
}
else {
$array[$key] = $array2[$key];
}
}
return $array1;
}
But it doesn't quite work. Can somebody help a bit?
It gives a warning like this:

Warning: Invalid argument supplied for foreach() in
C:\Programs\work\jobs\index.php on line 63

Shmuel.


you misspelled the variable name $array2 when you called the array_join
function ("$array2 = array_join($array1,$arra2);") you spelled $array2 like
$arra2
Jul 17 '05 #2

"kingofkolt" <je**********@comcast.net> wrote in message
news:dzpyc.77$Hg2.19@attbi_s04...
"Shmuel" <sd*@nic.fi> wrote in message
news:R7****************@reader1.news.jippii.net...
Hi,
I'm trying to do this:

$array1 = array("a" => 1, "b" => 2);
$array2 = array("a" => 2, "b" => 4);
$array3 = array_join($array1, $arra2);

Output should be:
$array3 = (
[a] => 3
[b] => 6
)

with this:
function array_join($array1, $array2) {
foreach($array2 as $key => $value) {
if(array_key_exists($key, $array1)) {
$array1[$key] .= $array2[$key];
}
else {
$array[$key] = $array2[$key];
}
}
return $array1;
}
But it doesn't quite work. Can somebody help a bit?
It gives a warning like this:

Warning: Invalid argument supplied for foreach() in
C:\Programs\work\jobs\index.php on line 63

Shmuel.
you misspelled the variable name $array2 when you called the array_join
function ("$array2 = array_join($array1,$arra2);") you spelled $array2

like $arra2


Moreover, .= is string concatenation, not addition. The result would be

$array3 = (
[a] => 12
[b] => 24
)

+= is needed in this case.

function array_join($array1, $array2) {
foreach($array2 as $key => $value) {
@$array1[$key] += $value;
}
return $array1;
}
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Java script Dude | last post by:
In many languages, it is necessary to string together multiple strings into one string for use over multiple lines of code. Which one is the most efficient from the interpreters perspective: ...
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.