Php Array to Table Rows - php

I have some form values in an array, and need to get them into table rows and could use some guidance/help. Below is what I have and am not sure if I am on the right path or not:
//These are my form values
$part = $_POST['part'];
$rel = $_POST['rel'];
$chart = $_POST['chart'];
$dob = $_POST['dob'];
$age = $_POST['age'];
$gender = $_POST['gender'];
//If participant (part) is not empty, start building the table
if ($part != "") {
//The table header (not worried about <td>/<th> semantics right now
$participants = "Participants<table border='1'>
<tr><td>Name</td><td>Relationship</td><td>Chart #</td><td>DOB</td> <td>Age</td><td>Gender</td></tr>";
//This is where I am lost...looping over and outputting on a row by row basis
foreach($part as $row) {
$participants_table = "<tr><td>". $part . "</td><td>". $rel ."</td><td>". $chart ."</td><td>". $dob ."</td><td>". $age ."</td><td>". $gender ."</td></tr>";
}
} else {
//If there are no names in the participant column(s), display the following
$participants = "No Other Participants";
$participants_table = "";
}
//Output from print_r
Next, the following may be overkill, but here goes: Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => )

Whatever you used in your print_r is what I'm focusing on here for this solution. So if you did print_r($_POST) then this would work:
I use htmlspecialchars to clean the string to prevent nasty xss injections.
<?php
foreach($_POST as $row) {
$part = htmlspecialchars($row[0], ENT_QUOTES, 'UTF-8');
$rel = htmlspecialchars($row[1], ENT_QUOTES, 'UTF-8');
$chart = htmlspecialchars($row[2], ENT_QUOTES, 'UTF-8');
$dob = htmlspecialchars($row[3], ENT_QUOTES, 'UTF-8');
$age = htmlspecialchars($row[4], ENT_QUOTES, 'UTF-8');
$gender = htmlspecialchars($row[5], ENT_QUOTES, 'UTF-8');
$unkown = htmlspecialchars($row[6], ENT_QUOTES, 'UTF-8'); //your output shows a [6]'th element
$participants_table = "<tr><td>". $part . "</td><td>". $rel ."</td><td>". $chart ."</td><td>". $dob ."</td><td>". $age ."</td><td>". $gender ."</td></tr>";
}
What's most interesting to me is that in your datadump, there's no associative keys, hence why numerical keys are used instead

when ever you are receiving an array you have to loop it using for loop or foreach loop, Since you have assigned values to variables like $part = $_POST['part']; which is not present, it will always throw error. And in your code $part is not an array to loop through.
<?php
//$part = $_POST[];
$part = array(
array('part', 'rel', 'chart', 'dob', 'age', 'gender'),
array('part1', 'rel1', 'chart1', 'dob1', 'age1', 'gender1')
);
if ($part != "") {
//The table header (not worried about <td>/<th> semantics right now
$participants = "Participants<table border='1'><tr><td>Name</td><td>Relationship</td><td>Chart #</td><td>DOB</td> <td>Age</td><td>Gender</td></tr>";
//This is where I am lost...looping over and outputting on a row by row basis
foreach ($part as $value) {
$participants .= "<tr><td>" . $value[0] . "</td><td>" . $value[1] . "</td><td>" . $value[2] . "</td><td>" . $value[3] . "</td><td>" . $value[4] . "</td><td>" . $value[5] . "</td></tr>";
}
$participants .= '</table>';
}
else {
//If there are no names in the participant column(s), display the following
$participants = "No Other Participants";
}
echo $participants;
?>

Related

Same Array Index to own Array

Hello I have the following scenario:
The following array is to be changed to a two-dimensional array.
Array
(
[0] => Array
(
[0] => Name1
[1] => Name2
[2] => Name3
[3] => Name4
[4] => Name5
[5] => Name6
[6] => Name7
[7] => Name8
)
[1] => Array
(
[0] => Company1
[1] => Company2
[2] => Company3
[3] => Company4
[4] => Company5
[5] => Company6
[6] => Company7
[7] => Company8
)
[2] => Array
(
[0] => Street1
[1] => Street2
[2] => Street3
[3] => Street4
[4] => Street5
[5] => Street6
[6] => Street7
[7] => Street8
)
[3] => Array
(
[0] => Date1
[1] => Date2
[2] => Date3
[3] => Date4
[4] => Date5
[5] => Date6
[6] => Date7
[7] => Date8
)
[4] => Array
(
[0] => Date_2_1
[1] => Date_2_2
[2] => Date_2_3
[3] => Date_2_4
[4] => Date_2_5
[5] => Date_2_6
[6] => Date_2_7
[7] => Date_2_8
)
[5] => Array
(
[0] => place1
[1] => place2
[2] => place3
[3] => place4
[4] => place5
[5] => place6
[6] => place7
[7] => place8
)
[6] => Array
(
[0] => break1
[1] => break2
[2] => break3
[3] => break4
[4] => break5
[5] => break6
[6] => break7
[7] => break8
)
[7] => Array
(
[0] => postcode1
[1] => postcode2
[2] => postcode3
[3] => postcode4
[4] => postcode5
[5] => postcode6
[6] => postcode7
[7] => postcode8
)
)
How the final array should look like
Array
(
[0] => Array
(
[0] => Name1
[1] => Company1
[2] => Street1
[3] => Date1
[4] => Date_2_1
[5] => place1
[6] => break1
[7] => postcode1
)
[1] => Array
(
[0] => Name2
[1] => Company2
[2] => Street2
[3] => Date2
[4] => Date_2_2
[5] => place2
[6] => break2
[7] => postcode2
)
[2] => Array
(
[0] => Name3
[1] => Company3
[2] => Street3
[3] => Date3
[4] => Date_2_3
[5] => place3
[6] => break3
[7] => postcode3
)
[3] => Array
(
[0] => Name4
[1] => Company4
[2] => Street4
[3] => Date4
[4] => Date_2_4
[5] => place4
[6] => break4
[7] => postcode4
)
[4] => Array
(
[0] => Name5
[1] => Company5
[2] => Street5
[3] => Date5
[4] => Date_2_5
[5] => place5
[6] => break5
[7] => postcode5
)
[5] => Array
(
[0] => Name6
[1] => Company6
[2] => Street6
[3] => Date6
[4] => Date_2_6
[5] => place6
[6] => break6
[7] => postcode6
)
[6] => Array
(
[0] => Name7
[1] => Company7
[2] => Street7
[3] => Date7
[4] => Date_2_7
[5] => place7
[6] => break7
[7] => postcode7
)
[7] => Array
(
[0] => Name8
[1] => Company8
[2] => Street8
[3] => Date8
[4] => Date_2_8
[5] => place8
[6] => break8
[7] => postcode8
)
)
function test($post_employee_nr){
require_once $_SERVER['DOCUMENT_ROOT'].'/module/dienstplan/_config.php';
$employee_query = $dbh->query("SELECT FT.*,M.*,O.*,E.*,K.* FROM
finish_time FT
LEFT JOIN
m_schicht M ON FT.m_schichtid = M.ID
LEFT JOIN
objekte O ON O.ID = M.objid
LEFT JOIN
mitarbeiter E ON E.ID = M.mitarbeiterid
LEFT JOIN
kunde K ON K.ID = M.kdid where FT.mitarbeiterid=$post_employee_nr")->fetchall();
foreach ($employee_query as $row) {
$employee_ID[] = $row['FT.ID'];
$customer[] = $row['kundenname'];
$street[] = $row['straße'];
$postcode[] = $row['plz'];
$place[] = $row['ort'];
$begin[] = $row['b_time'];
$end[] = $row['e_time'];
$break[] = $row['pause'];
$output = array($employee_ID, $customer,$street,$postcode,$place,$begin,$end,$break);
}
$html = $output;
$response = $html;
echo json_encode($response);
}
I hope I could make it obvious enough
EDIT
This is my solution:
$result = array();
foreach($employee_query as $employee_query) {
$result[] = array(
$days[date('l', strtotime($employee_query['b_time']))],
date("d.m.Y", strtotime($employee_query['b_time'])),
$employee_query['kundenname'],
$employee_query['strasse'],
$employee_query['plz'].' '.$employee_query['ort'],
date("H:i", strtotime($employee_query['b_time'])),
date("H:i", strtotime($employee_query['e_time'])),
'<i class="fas fa-plus-circle" style="color:green;"></i>'
);
}
echo json_encode($result);
exit();
}
If you want to change it to a 2 layer array just create a variable that holds the first element of the 3 layer array:
somthing like:
var array2D = array3D[0];
Also this bit of code seems like its not necesarry
$html = array($output);
$response = $html;
echo json_encode($response);
unless you need the array to be 3 layers when encoding it to Json. Otherwise just change it to:
echo json_encode($output);
Hopefully I understood your question and was able to help a little.
Edit
The way your foreach loop is currently running you're only creating a new array for each element then adding the same elements to their respective array and finally storing every array inside a new one (1 array with ALL id's, and one with ALL companynames etc...)
to fix it is very simple.
inside your foreach loop the $row variable looks like this:
$row => [idvalue, companyvalue, streetvalue etc....]
it's already an array containing the current $row's data, now all you need to do is directly add it to your $output array.
You're new foreach loop should look something like this:
foreach($employee_query as $row) {
$output[] = $row; // when using [] after a variable you add to that array
}
echo json_encode($output);
if you don't want to use all the data that your query collected you can specify which attributes you want to use like so:
$output[] = array($row['FT.ID'], $row['kundenname'], $row['straße'], etc...);

Php array saving values only till 62 indices

I am sending a big array of 130+ indices from ajax to php. But while going to php, if i print, it became a 2D array with indices 63, 63, 6 respectively.
Below is snip
Array
(
[0] => Array
(
[0] => 943900200
[1] => 1297017000
[2] => 1299436200
[3] => 1302114600
[4] => 1304879400
[5] => 1307385000
................
[60] => 1452105000
[61] => 1454869800
[62] => 1457375400
)
[1] => Array
(
[0] => 943900200
[1] => 1297017000
[2] => 1299436200
[3] => 1302114600
[4] => 1304879400
[5] => 1307385000
......
[61] => 1454869800
[62] => 1457289000
)
[2] => Array
(
[0] => 1440441000
[1] => 1443033000
[2] => 1445970600
[3] => 1445970600
[4] => 1447007400
[5] => 1448908200
)
)
But i want them in a single D array...[0]--[127] together. I tried to copy them using foreach aswell. It copies the first 63 indices and stops. Any one please help. Thanks
You must use foreach twice:
INTPUT (example):
Array
(
[0] => Array
(
[0] => 5031750
[1] => 3972258
[2] => 1673731
[3] => 721866
[4] => 4031885
[5] => 1454990
)
[1] => Array
(
[0] => 1115002
[1] => 27608
[2] => 3531620
[3] => 4412066
[4] => 4032217
[5] => 2681734
)
[2] => Array
(
[0] => 3360879
[1] => 5190034
[2] => 3452229
[3] => 5112636
[4] => 628357
[5] => 4299124
)
)
PHP Code:
$output = array();
foreach($input as $key=>$sub){
foreach($sub as $k => $v){
$output[] = $v;
}
}
print_r($output);
Output:
Array
(
[0] => 5031750
[1] => 3972258
[2] => 1673731
[3] => 721866
[4] => 4031885
[5] => 1454990
[6] => 1115002
[7] => 27608
[8] => 3531620
[9] => 4412066
[10] => 4032217
[11] => 2681734
[12] => 3360879
[13] => 5190034
[14] => 3452229
[15] => 5112636
[16] => 628357
[17] => 4299124
)

Retrieving elements from a nested array

I am using MultipleIterator() to iterate through two different arrays and get each element.
My code
$d = new MultipleIterator ();
$d->attachIterator ( new ArrayIterator ( $tbl_one_data ) );
$d->attachIterator ( new ArrayIterator ( $tbl_two_data ) );
foreach ( $d as $data ) {
print_r($data);
}
Which generates the following :
My question is how do I loop through the array and return each element? For example I would like to return 2014-11-06 11:31:58.781018. Tried using $data[0][0] but this returns all elements in the first index but I only want one element.
EDIT
print_r($d);
MultipleIterator Object ( [storage:SplObjectStorage:private] => Array ( [00000000583bd67b000000000ac6c449] => Array ( [obj] => ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => Array ( [0] => 1 [1] => 2014-11-06 11:31:58.781018 [2] => NONE [3] => NONE ) [1] => Array ( [0] => 2 [1] => 2014-11-06 11:31:58.799436 [2] => MANAGER [3] => 500 ) [2] => Array ( [0] => 3 [1] => 2014-11-06 11:31:58.841035 [2] => MANAGER [3] => 501 ) [3] => Array ( [0] => 4 [1] => 2014-11-06 11:33:00.741873 [2] => MANAGER [3] => 500 ) [4] => Array ( [0] => 5 [1] => 2014-11-06 11:33:00.802389 [2] => MANAGER [3] => 501 ) [5] => Array ( [0] => 6 [1] => 2014-11-06 13:15:49.457646 [2] => MANAGER [3] => 500 ) [6] => Array ( [0] => 7 [1] => 2014-11-06 13:37:16.259128 [2] => NONE [3] => NONE ) [7] => Array ( [0] => 8 [1] => 2014-11-06 13:37:16.275201 [2] => NONE [3] => 500 ) [8] => Array ( [0] => 9 [1] => 2014-11-06 13:37:27.682873 [2] => NONE [3] => NONE ) [9] => Array ( [0] => 10 [1] => 2014-11-06 13:37:27.690863 [2] => NONE [3] => 500 ) [10] => Array ( [0] => 11 [1] => 2014-11-06 13:52:21.108003 [2] => MANAGER [3] => 500 ) [11] => Array ( [0] => 12 [1] => 2014-11-06 14:17:01.266769 [2] => NONE [3] => NONE ) [12] => Array ( [0] => 13 [1] => 2014-11-06 14:17:01.279507 [2] => node1-1415283420.0 [3] => 500 ) [13] => Array ( [0] => 14 [1] => 2014-11-06 14:17:02.527183 [2] => node1-1415283420.0 [3] => 500 ) [14] => Array ( [0] => 15 [1] => 2014-11-06 14:17:23.775279 [2] => node1-1415283442.1 [3] => 500 ) ) ) [inf] => ) [00000000583bd67a000000000ac6c449] => Array ( [obj] => ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => Array ( [0] => NONE [1] => QUEUESTART [2] => [3] => [4] => [5] => [6] => ) [1] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [2] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [3] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [4] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [5] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [6] => Array ( [0] => Local/120#disc-agents/n [1] => PAUSEALL [2] => [3] => [4] => [5] => [6] => ) [7] => Array ( [0] => Dunc Test [1] => PAUSE [2] => [3] => [4] => [5] => [6] => ) [8] => Array ( [0] => Local/120#disc-agents/n [1] => UNPAUSEALL [2] => [3] => [4] => [5] => [6] => ) [9] => Array ( [0] => Dunc Test [1] => UNPAUSE [2] => [3] => [4] => [5] => [6] => ) [10] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [11] => Array ( [0] => NONE [1] => QUEUESTART [2] => [3] => [4] => [5] => [6] => ) [12] => Array ( [0] => NONE [1] => ENTERQUEUE [2] => [3] => 363 [4] => 1 [5] => [6] => ) [13] => Array ( [0] => NONE [1] => ABANDON [2] => 1 [3] => 1 [4] => 1 [5] => [6] => ) [14] => Array ( [0] => NONE [1] => ENTERQUEUE [2] => [3] => 363 [4] => 1 [5] => [6] => ) ) ) [inf] => ) ) )
if this is really output by print_r then $data[0][1] must be '2014-11-06 11:31:58.781018' . I test it by code:
$data = array(array('1','2014-11-06 11:31:58.781018'));
echo '<pre>';
print_r($data); echo '<br>';
echo 'what we want: '.$data[0][1].'<br>';
otput:
Array
(
[0] => Array
(
[0] => 1
[1] => 2014-11-06 11:31:58.781018
)
)
what we want: 2014-11-06 11:31:58.781018
If you have the $data as an array you can access the value: 2014-11-06 11:31:58.781018 by using,
$date = $data[0][1];

comparing arrays in php and assign values from one to another

been woking on a project of mine for a few days now, and essentialy what i'm triing to do in this project is a comparison of csv file that a user normaly does in excel, to do it in php automaticly every night.
I got the info from the CSV's intro arrays, but i'm having trouble combinig them to get the right info for every book, hence the folowing exemple and question.
I have the folowing array (array1) from a csv file :
Array
(
[0] => Array
(
[0] => book1
[1] => description1
[2] => category1
[3] => code1
[4] => editor1
[5] => 0
[6] => eur
[7] => out of stoc
)
[1] => Array
(
[0] => book2
[1] => description2
[2] => category2
[3] => code2
[4] => editor2
[5] => 0
[6] => curr2
[7] => out of stoc
)
[2] => Array
(
[0] => book3
[1] => description3
[2] => category3
[3] => code3
[4] => editor3
[5] => 0
[6] => curr3
[7] => out of stoc
)
[3] =>
)
and another array (array2) from a second csv file :
Array
(
[0] => Array
(
[0] => book1
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 12
[6] => eur
[7] => in stoc
)
[1] => Array
(
[0] => book2
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 13
[6] => eur
[7] => in stoc
)
[2] => Array
(
[0] => book4
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 14
[6] => usd
[7] => in stoc
)
[3] => Array
(
[0] => book5
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 16
[6] => usd
[7] => in stoc
)
)
I would like to know how to get the values form array2 intro array1 for the books of array1 found in array2.
Ex:
Array
(
[0] => Array
(
[0] => book1
[1] => description2_from_array2
[2] => category2_from_array2
[3] => code2_from_array2
[4] => editor2_from_array2
[5] => 12
[6] => eur
[7] => in stoc
)
[1] => Array
(
[0] => book2
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 13
[6] => curr_from_array2
[7] => in stoc
)
[2] => Array
(
[0] => book3
[1] => description3
[2] => category3
[3] => code3
[4] => editor3
[5] => 0
[6] => curr3
[7] => out of stoc //because book3 is not found in array2
)
[3] =>
)
Any help for this question would be greatly appreciated, belive me!
//Add keys to array 2 to aid lookup
$array2Tmp = array();
foreach($array2 as $item){
$array2Tmp[$item[0]] = $item;
}
$array3 = array();
foreach($array1 as $item){
//Use item from second array if it exists
if(array_key_exists($item[0],$array2Tmp)){
$array3[] = $array2Tmp[$item[0]];
}else{
$array3[] = $item;
}
}

array manipulation

I have a two column array (array1), for each row of that array I need to compare the value in the 2nd column with a column value in each row of another array(array1) , when they equal I want to append another column value (from array2) to the first array.
in english:
if array1[x][1] = array2[y][0]
then array1[x][2] = array2[y][2]
screen dumps of both arrays
array1 (
[1] => Array (
[0] => 1 [1] => 2
)
[2] => Array (
[0] => 2 [1] => 3
)
[3] => Array (
[0] => 3 [1] =>
) [7] => Array (
[0] => 7 [1] => 1
)
[8] => Array (
[0] => 8 [1] => 1
)
[9] => Array (
[0] => 9 [1] => 10
)
[10] => Array (
[0] => 10 [1] => 2
)
)
array2 (
[0] => Array (
[0] => 1
[1] => 2
[2] => 2
[3] => Jane
[4] => Smith
[5] => jsmith#internet.com
[6] => jsmith
[7] => 12345
[8] => 1
[9] => no
)
[1] => Array (
[0] => 2
[1] => 2
[2] => 3
[3] => James
[4] => Beard
[5] => jasb#bellsouth.net
[6] => jbeard03
[7] => keeper
[8] => 1
[9] => no
)
[2] => Array (
[0] => 3
[1] => 2
[2] =>
[3] => Peter
[4] => Allen
[5] => pallen#rfgg.com
[6] => pallen
[7] => pallen
[8] => 1
[9] => no
)
[3] => Array (
[0] => 7
[1] => 2
[2] => 1
[3] => Joe
[4] => Blow
[5] => jasb#bellsouth.net
[6] => jblow
[7] => blow123
[8] => 5
[9] => yes
)
[4] => Array (
[0] => 8
[1] => 2
[2] => 1
[3] => John
[4] => Smith
[5] => logtest#bellsouth.net
[6] => jnsmith
[7] => jsmith123
[8] => 4
[9] => yes
)
[5] => Array (
[0] => 9
[1] => 2
[2] => 10
[3] => Frank
[4] => Smith
[5] => pallen#test.com
[6] => fsmith
[7] => fsmith123
[8] => 4
[9] => yes
)
[6] => Array (
[0] => 10
[1] => 2
[2] => 2
[3] => Loretta
[4] => Beard
[5] => lbeard#me.net
[6] => lbeard
[7] => lbeard123
[8] => 1
[9] => no
)
)
Does this work? I'm not sure I'm entirely clear on what you're looking for.
foreach($array1 as $x => $xarray) {
foreach($array2 as $y => $yarray) {
if($xarray[1] == $yarray[0]) {
$array1[$x][2] = $array[$y][2];
}
}
}
foreach ($array1 as &$a1) {
foreach ($array2 as $a2) {
if ($a1[1] == $a2[0]) {
$a1[] = $a2[2];
continue 2;
}
}
}
BTW, you should try to use explicit keys that actually mean something, e.g. $a1['id'] instead of $a1[1]. You'll thank yourself when you look at the code again two months down the road.
And because I threatened to do so:
btwyoushouldtrytouseexplicitkeysthatactuallymeansomethingeg$a1['id']insteadof$a1[1]youllthankyourselfwhenyoulookatthecodeagaintwomonthsdowntheroad ;-P

Categories