/    Sign up×
Community /Pin to ProfileBookmark

element of a multi_D array

Hey,

I have a database that I call some data from and store it in a 2 column array….[id, name]

I have another call to a different database that stores its data…[date, to, duration, rate]

now the ‘to’ column is an id number that relates to the id column of the 1st array.

I need to populate a table, but want the name from array1 to show up.

What is the best method for retreiving this?

I’m sure there is a common method, but as both these are written to a table that needs to appear almost instantaneously, what is the cleanest method?

Thx for your help.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@chazzyDec 22.2005 — is it a 1:1 relationship? 1 id to many to's? (1:n)? what columns do you want to insert? you can do this:
INSERT INTO <span><code>your_new_table</code></span> SELECT a.name,b.date,b.duration,b.rate,b.to FROM <span><code>first_table</code></span> a, <span><code>second_table</code></span> b
Copy linkTweet thisAlerts:
@CrazyMerlinauthorDec 22.2005 — no, I don't want to deal with it on the server side.

I could simply execute an sql statement passing it the id and retrieving the name from the database, but if I do this from remote location and the call log has many entries it will take too long to populate the table.

I think the best way would be to load the ids and names into an array and grab them from there.

So what I need to do is find array2_column2 where array2_column1 = array1_column 1

see what I mean?

I was wondering if there is a way to find the element in the array without using a for loop to traverse the array.

thx
Copy linkTweet thisAlerts:
@NogDogDec 22.2005 — [code=php]
<?php
# test data:
$array1 = array(array('id' => 1, 'name' => 'Manny'),
array('id' => 2, 'name' => 'Moe'),
array('id' => 3, 'name' => 'Jack'));
$array2 = array(array('date' => '2005/12/21',
'to' => 1,
'duration' => 11,
'rate' => 111),
array('date' => '2005/12/22',
'to' => 2,
'duration' => 12,
'rate' => 112),
array('date' => '2005/12/23',
'to' => 3,
'duration' => 33,
'rate' => 333));
# merge the two arrays on the id/to columns:
foreach($array1 as $data)
{
$merged[$data['id']]['name'] = $data['name'];
}
foreach($array2 as $data)
{
foreach(array('date', 'duration', 'rate') as $name)
{
$merged[$data['to']][$name] = $data[$name];
}
}
# test result:
echo "<pre>";print_r($merged);echo "</pre>n";
?>
[/code]
Copy linkTweet thisAlerts:
@CrazyMerlinauthorDec 22.2005 — thanks, but I was trying to do it without having to loop through the array.

basically what I wanted was something like.....$row2[$row1['id'],2]

so it would get column2 of $row2

if you have a matrix:

[1, 'a']

[2, 'b']

[3, 'c']

and you asked for element 2,2 of that matrix you would get 'b'

well that is what I want, a function that I can specify row and column of a multi dimensional array, and it will return the value at that position.

thx
Copy linkTweet thisAlerts:
@NogDogDec 22.2005 — Is this all you're looking for?
[code=php]
$row = 2;
$col = 2;
$value = $array[$row][$col];
[/code]
Copy linkTweet thisAlerts:
@CrazyMerlinauthorDec 22.2005 — yes, but as I just found out, a call to a mysql database does not return the entire result set, but just a row, so it won't work for me.

thx
Copy linkTweet thisAlerts:
@SheldonDec 22.2005 — It can do, try posting your code
Copy linkTweet thisAlerts:
@chazzyDec 23.2005 — Just to point out a few things...
no, I don't want to deal with it on the server side.[/quote]
PHP is a server side language.

I could simply execute an sql statement passing it the id and retrieving the name from the database, but if I do this from remote location and the call log has many entries it will take too long to populate the table.

I think the best way would be to load the ids and names into an array and grab them from there.[/quote]
You seem to be implying that there could be a lot of data. What happens when PHP runs out of memory? Don't forget, databases are designed to search and match up data. It kind of seems like you want to write your own matching system, it cannot run any faster than any inherent database joiner.

So what I need to do is find array2_column2 where array2_column1 = array1_column 1

see what I mean?

I was wondering if there is a way to find the element in the array without using a for loop to traverse the array.

thx[/QUOTE]


I would say there isn't. The best way you can do this is to order them matching, so that your first ID would match the first row, and keep matching until they don't match, then increment your other array. But that requires you to use your database to sort. Is that ok?
×

Success!

Help @CrazyMerlin 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.13,
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,
)...