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...);
Related
This pertains to PHP.
I have an array of arrays that is a recordset from a db query:
Array
(
[0] => Array
(
[0] => Amazon
[1] => AmazonSendTracking
[2] =>
[3] => 1
[4] => IN
[5] => 2020-01-07 11:32:18
[6] => 7
[7] => 5
)
[1] => Array
(
[0] => Amazon
[1] => AmazonGetOrdersAndMove
[2] =>
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 6
[7] => 4
)
[2] => Array
(
[0] => Test
[1] => RedirectTest1
[2] => data=data1&data2=data2&testvar=testvariable
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 17
)
[3] => Array
(
[0] => Test
[1] => RedirectTest2
[2] => data=value&data2=value2&testvar=value3
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 25
)
[4] => Array
(
[0] => Amazon
[1] => AmazonPushInventory
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 26
)
[5] => Array
(
[0] => Amazon
[1] => CalculateFloorCeiling
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 27
)
[6] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXML
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 28
)
[7] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXMLResetBusinessAndFlipEm
[2] =>
[3] => 1440
[4] => OUT
[5] =>
[6] => 8
[7] => 6
)
)
the Value in the 7th position (6) of the sub-array represents a "group" that I want to break out into individual arrays.
So I want the above to become one array of items not in any group and then a new array for each group like this:
Array1 - All Items not in a group
Array1
(
[0] => Array
(
[0] => Amazon
[1] => AmazonSendTracking
[2] =>
[3] => 1
[4] => IN
[5] => 2020-01-07 11:32:18
[6] => 7
[7] => 5
)
[1] => Array
(
[0] => Amazon
[1] => AmazonGetOrdersAndMove
[2] =>
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 6
[7] => 4
)
[2] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXMLResetBusinessAndFlipEm
[2] =>
[3] => 1440
[4] => OUT
[5] =>
[6] => 8
[7] => 6
)
)
Array2 - 1st group
Array2(
[0] => Array
(
[0] => Test
[1] => RedirectTest1
[2] => data=data1&data2=data2&testvar=testvariable
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 17
)
[1] => Array
(
[0] => Test
[1] => RedirectTest2
[2] => data=value&data2=value2&testvar=value3
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 25
)
)
Array3 - 2nd group
Array3(
[0] => Array
(
[0] => Amazon
[1] => AmazonPushInventory
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 26
)
[1] => Array
(
[0] => Amazon
[1] => CalculateFloorCeiling
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 27
)
[2] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXML
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 28
)
)
Im just not sure what array functions would be best used to accomplish this. I have tried getting the count of each group using the following.
I have a loop that steps through the master array (array of arrays call $rs) and while in the loop I pull out the 7th position into a variable called $HeaderRecNbr and then do this:
$GroupCount=array_count_values(array_column($rs, 6))[$HeaderRecNbr];
but Im not sure where to go from here.
Just create an array indexed by the group from index 6. This will create a sub-array for each group indexed by the group number:
foreach($array as $v) {
$groups[$v[6]][] = $v;
}
Then to get the items that are alone in a group, check if there is exactly one sub-array and add it to another array. Then remove it from the group array:
foreach($groups as $k => $v) {
if(count($v) == 1) {
$other[] = $v; // or $other[$v[0][6]][] = $v;
unset($groups[$k]);
}
}
$mainArray = ... all rows variable here..
$groupedArray = [];
$countArray = [];
foreach ($mainArray as $k => $v) {
$groupedArray[$v[6]][] = $v;
$countArray[$v[6]]++;
}
$singleItems = [];
$multipleItems = [];
foreach ($groupedArray as $k => $v) {
if ($countArray[$k] > 1) $multipleItems[] = $v;
else $singleItems = $v;
}
... do something with single and multi - groups...
1: start by grouping all items in groups by the 6th item of sub array... keep count of them at same time
2. Separate single and multiple items groups in 2 arrays...
3. Do whatever you need with multi-items groups.
This function will group them by desired index, then combine the ones that dont have any other with same index, and return an array of arrays, ungrouped, then all grouped.
function sortByIndex( $data, $index ) {
$sortedData = array();
$ungroupedData = array();
// Make sure you can loop through
if ( ! is_array( $data ) ) {
return FALSE;
}
foreach ( $data as $key => $arrayToInspect ) {
if ( ! isset( $sortedData[$arrayToInspect[$index]] ) ) {
$sortedData[$arrayToInspect[$index]] = array();
}
$sortedData[$arrayToInspect[$index]][] = $arrayToInspect;
}
// Combine as desired
foreach ( $sortedData as $groupId => $data ) {
if ( count( $data ) < 2 ) {
$ungroupedData[] = $data;
unset( $sortedData[$groupId] );
}
}
return ( array_merge( [ $ungroupedData ], array_values( $sortedData ) ) );
}
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;
?>
Array
(
[csv_data] => Array
(
[0] => Array
(
[0] => CAM
[1] => Partner
[2] => Division
[3] => Domain
[4] => Year
[5] => Quarter
[6] => Tactic
[7] => Impressions
[8] => Responders
)
[1] => Array
(
[0] => CAM
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Single Email Campaign
[7] => 8000
[8] => 6000
)
[2] => Array
(
[0] =>
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Multi-Touch Email Campaign
[7] => 350
[8] => 200
)
[3] => Array
(
[0] => TestR
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q1
[6] => Single Email Campaign
[7] => 9000
[8] => 4000
)
[4] => Array
(
[0] =>
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Linkedin(Groups)
[7] => 350
[8] => 200
)
)
)
Hello, i am new in PHP.
i just want that in this array i want to add the particular array key values and identify that it sholud be has the same partner and divison after identify the values of all particular should be add in new array.
ANSWER should be like this:
Array
(
[csv_data] => Array
(
[0] => Array
(
[0] => CAM
[1] => Partner
[2] => Division
[3] => Domain
[4] => Year
[5] => Quarter
[6] => Tactic
[7] => Impressions
[8] => Responders
)
[1] => Array
(
[0] => CAM
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Single Email Campaign
[7] => 8350
[8] => 6200
)
[2] => Array
(
[0] => TestR
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q1
[6] => Single Email Campaign
[7] => 9350
[8] => 4200
)
)
)
Try this code, this will definitely work for you..
$removeKeys = array();
foreach($data['csv_data'] as $key => $val)///loop through array..
{
foreach($data['csv_data'] as $k => $v)
{
if($val[1] == $v[1] && $key != $k)////check if key 1 matches
{
if(!in_array($key,$removeKeys)) ////check if item is already added or not
{
$removeKeys[] = $k; ///push into removed keys because this is added into matched item
$data['csv_data'][$key][7]+=$data['csv_data'][$k][7];
$data['csv_data'][$key][8]+=$data['csv_data'][$k][8];
}
}
}
}
foreach($removeKeys as $rk)
{
unset($data['csv_data'][$rk]); ////remove all the keys in removeKeys
}
print_r($data['csv_data']);///your desired output...
This will give you :
Array
(
[0] => Array
(
[0] => CAM
[1] => Partner
[2] => Division
[3] => Domain
[4] => Year
[5] => Quarter
[6] => Tactic
[7] => Impressions
[8] => Responders
)
[1] => Array
(
[0] => CAM
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Single Email Campaign
[7] => 8350
[8] => 6200
)
[3] => Array
(
[0] => TestR
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q1
[6] => Single Email Campaign
[7] => 9350
[8] => 4200
)
)
LIVE EXAMPLE : CLICK HERE
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;
}
}
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