Array
(
[RX Housing] => Array
(
[0] => Array
(
[reg_id] => 63
[vRegionName] => Boston
[Forwarded] => 1
[Accepted] => 0
[Rejected] => 0
[Dupe] => 0
[PotentialLease] => 0
[Lcpc] => 0
[CloseLead] => 0
[TotalInvamt] => 0.00
)
[1] => Array
(
[reg_id] => 41
[vRegionName] => Chicago
[Forwarded] => 1
[Accepted] => 0
[Rejected] => 0
[Dupe] => 0
[PotentialLease] => 0
[Lcpc] => 0
[CloseLead] => 1
[TotalInvamt] => 954.00
)
)
)
Array
(
[RX Housing] => Array
(
[0] => Array
(
[reg_id] => 63
[TotalInv] => 2
)
[1] => Array
(
[reg_id] => 41
[TotalInv] => 8
)
)
)
i want to add [TotalInv] on First array based on [reg_id] , how can i do this?? pls help me.
i want this output::
Array
(
[RX Housing] => Array
(
[0] => Array
(
[reg_id] => 63
[vRegionName] => Boston
[Forwarded] => 1
[Accepted] => 0
[Rejected] => 0
[Dupe] => 0
[PotentialLease] => 0
[Lcpc] => 0
[CloseLead] => 0
[TotalInvamt] => 0.00
[TotalInv] => 2
)
[1] => Array
(
[reg_id] => 41
[vRegionName] => Chicago
[Forwarded] => 1
[Accepted] => 0
[Rejected] => 0
[Dupe] => 0
[PotentialLease] => 0
[Lcpc] => 0
[CloseLead] => 1
[TotalInvamt] => 954.00
[TotalInv] => 8
)
)
)
Thank in advance.
Vims Mak
I would recommend when defining your first array to set the keys to the reg_id.
For example if you were creating this array from query results:
$results = array();
while($row = mysql_fetch_assoc($blah)){
$id = $row['reg_id'];
$results[$id] = $row;
}
Then when you loop through your second array you can easily update the first.
foreach($secondArray as $key => $row){
$id = $row['reg_id'];
$val = $row['TotalInv'];
$first[$id]['TotalInv'] = $val;
}
Arrays are a beautiful thing! Hope that helps!
-fie
Do you mean:
foreach($yourArr["RX Housing"] as $key => $val) {
if($val["reg_id"] == 10) {
$yourArr["RX Housing"][$key]["TotalInv"] = 2;
}
else {
$yourArr["RX Housing"][$key]["TotalInv"] = 20;
}
}
Hope it helps
try this
$a = Array
(
'RX Housing' => Array
(
Array
(
'reg_id' => 63,
'vRegionName' => 'Boston',
'Forwarded' => 1,
'Accepted' => 0,
'Rejected' => 0,
'Dupe' => 0,
'PotentialLease' => 0,
'Lcpc' => 0,
'CloseLead' => 0,
'TotalInvamt' => 0.00
),
Array
(
'reg_id' => 41,
'vRegionName' => 'Chicago',
'Forwarded' => 1,
'Accepted' => 0,
'Rejected' => 0,
'Dupe' => 0,
'PotentialLease' => 0,
'Lcpc' => 0,
'CloseLead' => 1,
'TotalInvamt' => 954.00
)
)
);
$b = Array
(
'RX Housing' => Array
(
Array
(
'reg_id' => 63,
'TotalInv' => 2
),
Array
(
'reg_id' => 41,
'TotalInv' => 8
)
)
);
foreach ($a['RX Housing'] as $value) {
foreach ($b['RX Housing'] as $value1) {
foreach ($value as $key => $result) {
if (array_key_exists($key, $value1)) {
if ($result == $value1[$key]) {
$value = array_merge($value, $value1);
$c['RX Housing'][] = $value;
}
}
}
}
}
var_dump($c);
out put is
array
'RX Housing' =>
array
0 =>
array
'reg_id' => int 63
'vRegionName' => string 'Boston' (length=6)
'Forwarded' => int 1
'Accepted' => int 0
'Rejected' => int 0
'Dupe' => int 0
'PotentialLease' => int 0
'Lcpc' => int 0
'CloseLead' => int 0
'TotalInvamt' => float 0
'TotalInv' => int 2
1 =>
array
'reg_id' => int 41
'vRegionName' => string 'Chicago' (length=7)
'Forwarded' => int 1
'Accepted' => int 0
'Rejected' => int 0
'Dupe' => int 0
'PotentialLease' => int 0
'Lcpc' => int 0
'CloseLead' => int 1
'TotalInvamt' => float 954
'TotalInv' => int 8
Related
I have an array of arrays like this :
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '103',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Balance',
1 => 'ID',
),
1 =>
array (
0 => '4533',
1 => '101',
)
),
'data3' => array (
0 =>
array (
0 => 'Active',
1 => 'ID',
),
1 =>
array (
0 => 'Yes',
1 => '101',
),
2 =>
array (
0 => 'No',
1 => '103',
)
),
);
Here is the current working answer by user #Nigel Ren I have to this question here.
$store = [];
$headers = [];
foreach ( $data as $set ) {
$headerRow = array_shift($set);
// Collect all header columns
$headers = array_merge($headers, $headerRow);
foreach ( $set as $index => $list ){
// Create associative list of data so they can be combined (i.e. ID fields)
$list = array_combine($headerRow, $list);
// Use ID value as key and create if needed
if ( !isset($store[$list["ID"]]) ) {
$store[$list["ID"]] = $list;
}
else {
$store[$list["ID"]] = array_merge($store[$list["ID"]], $list);
}
}
}
$headers = array_unique($headers);
$output = [ 'output' => [$headers]];
// Create template array, so that missing fields will be set to null
$blank = array_fill_keys($headers, null);
foreach ( $store as $dataRow ) {
// Fill in the fields for this ID and then change to numeric keys
$output['output'][] = array_values(array_merge($blank, $dataRow));
}
print_r($output);
Above code works perfectly for the above given array.
Here are the new cases I need to handle:
CASE 1 :
When the data contains only one array where the ID are same :
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '101',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Balance',
1 => 'ID',
),
),
'data3' => array (
0 =>
array (
0 => 'Active',
1 => 'ID',
),
),
);
In this case Nigel's answer doesn't output both the both records for the same ID. It outputs just a single record.
Expected output when there's a single array with duplicate ID's present :
Array
(
[output] => Array
(
[0] => Array
(
[0] => ID
[1] => PinCode
[2] => Date
)
[1] => Array
(
[0] => 101
[1] => 454075
[2] => 2012-03-03
)
[2] => Array
(
[0] => 101
[1] => 786075
[2] => 2012-09-05
)
)
)
CASE 2 :
When any of the array's other than the first array contain's entries for multiple same ID.
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '103',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Order',
1 => 'ID',
),
1 =>
array (
0 => 'Colgate',
1 => '101',
),
2 =>
array (
0 => 'Waffles',
1 => '101',
)
),
);
Expected output in this case :
Array
(
[output] => Array
(
[0] => Array
(
[0] => ID
[1] => PinCode
[2] => Date
[3] => Order
)
[1] => Array
(
[0] => 101
[1] => 454075
[2] => 2012-03-03
[3] => Colgate
)
[2] => Array
(
[0] => 101
[1] => 454075
[2] => 2012-03-03
[3] => Waffles
)
[3] => Array
(
[0] => 103
[1] => 786075
[2] => 2012-09-05
[3] =>
)
)
)
How do I do it?
you can get the desired output by using a loop:
$desired_array = array();
$n_data1 = count($data['data1']);
$n_data2 = count($data['data2']);
$n_data3 = count($data['data3']);
for ($i=0; $i < $n_data1; $i++) {
if ($n_data2 > $i) {
foreach ($data['data2'][$i] as $key => $value) {
if(!in_array($value,$data['data1'][$i])){
$data['data1'][$i][]=$value;
}
}
} else {
$data['data1'][$i][]= null;
}
}
for ($i=0; $i < $n_data1; $i++) {
if ($n_data3 > $i) {
foreach ($data['data3'][$i] as $key => $value) {
if(!in_array($value,$data['data1'][$i])){
$data['data1'][$i][]=$value;
}
}
} else {
$data['data1'][$i][]= null;
}
}
$desired_array = $data['data1'];
I have one large multidimensional array(I dnt the depth of array) and I want to display it in tabular form . this is one array from my array
[Kai Roger Tester] => Array
(
[Ikke navngitt] => Array
(
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 3
[waiting_approval_documents] => 1
[waiting_verfication_documents] => 0
[under_construction_documents] => 3
)
)
[Finnfjord] => Array
(
[NVD test] => Array
(
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 1
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 5
)
)
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 0
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 2
)
)
[Endringslogg] => Array
(
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 0
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 1
)
)
[Laste opp doc] => Array
(
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 1
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 1
)
)
[status] => Array
(
[documents_green] => 1
[documents_yellow] => 0
[documents_red] => 6
[waiting_approval_documents] => 3
[waiting_verfication_documents] => 4
[under_construction_documents] => 13
)
)
[Prosess 1] => Array
(
[AF Decom] => Array
(
[status] => Array
(
[documents_green] => 1
[documents_yellow] => 0
[documents_red] => 0
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 3
)
)
[status] => Array
(
[documents_green] => 7
[documents_yellow] => 0
[documents_red] => 2
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 11
)
)
Folder Name green yellow red
Kai Roger Tester 1 0 6
Ikke navngitt' 0 0 3
Finnfjord 0 0 0
NVD test 0 0 1
Process1 1 1 0
I tired with below method
public static function getfoldertable($array, $prefix = '') {
$body_start = "<tbody>";
if (count($array) > 0 && is_array($array)) {
$i = 0;
foreach ($array as $key => $row) {
$body_start.='<tr>
<td>'.$i.'</td>
<td>'.$key.'</td>
<td>'.$row['status']['documents_green'].'</td>
<td>'.$row['status']['documents_yellow'].'</td>
<td>'.$row['status']['documents_red'].'</td>
</tr>';
$body_start.= self::getfoldertable($row, $prefix . '-');
$i++;
}
$body_start.="</tbody>";
}
//echo $body_start; die;
return $body_start;
}
Can anyone help me how can i display this?
Thanks in advance
Here is a function you could use for generating that output:
function getTreeHTML($tree, $level = 0) {
$html = "";
$indent = str_repeat(" ", $level * 2);
foreach ($tree as $key => $value) {
if ($key == "status") continue;
$html .= "
<tr><td>$indent$key</td>
<td>{$value['status']['documents_green']}</td>
<td>{$value['status']['documents_yellow']}</td>
<td>{$value['status']['documents_red']}</td>
</tr>" . getTreeHTML($value, $level+1);
}
return $html;
}
If the input data is defined like this:
$input = array (
'Kai Roger Tester' =>
array (
'Ikke navngitt' =>
array (
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 3,
'waiting_approval_documents' => 1,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 3,
),
),
'Finnfjord' =>
array (
'NVD test' =>
array (
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 1,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 5,
),
),
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 0,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 2,
),
),
'Endringslogg' =>
array (
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 0,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 1,
),
),
'Laste opp doc' =>
array (
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 1,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 1,
),
),
'status' =>
array (
'documents_green' => 1,
'documents_yellow' => 0,
'documents_red' => 6,
'waiting_approval_documents' => 3,
'waiting_verfication_documents' => 4,
'under_construction_documents' => 13,
),
),
'Prosess 1' =>
array (
'AF Decom' =>
array (
'status' =>
array (
'documents_green' => 1,
'documents_yellow' => 0,
'documents_red' => 0,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 3,
),
),
'status' =>
array (
'documents_green' => 7,
'documents_yellow' => 0,
'documents_red' => 2,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 11,
),
),
);
...and you would call that function like this:
$html = getTreeHTML($input);
...embedding that HTML result as follows in a table:
<table border=1>
<tr><th>Folder Name</th><th>green</th><th>yellow</th><th>red</th></tr>
<?=$html?>
</table>
...then the output would look in a browser like this:
Use RecursiveIterator.
Working code:
$array = array();
$array[0] = array('test' => array('asdf', 'asdf', array('asf', array('asdfads', 'asdf' => array(234,234,234)))));
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $key => $item) {
if (is_array($item)) {
echo '<pre>';print_r($item);echo '</pre>';
}
}
I have an array that looks like this...
Array
(
[0] => Array
(
[code] => AS34
[2014-12-10] => 32
)
[1] => Array
(
[code] => AS34
[2014-12-11] => 42
)
[2] => Array
(
[code] => AS34
[2014-12-12] => 40
)
[3] => Array
(
[code] => AS34
[2014-12-15] => 44
)
[4] => Array
(
[code] => AH98
[2014-12-10] => 1
)
[5] => Array
(
[code] => AT78
[2014-12-12] => 1
)
[6] => Array
(
[code] => AL44
[2014-12-10] => 23
)
[7] => Array
(
[code] => AL44
[2014-12-11] => 27
)
[8] => Array
(
[code] => AL44
[2014-12-13] => 25
)
[9] => Array
(
[code] => AL44
[2014-12-15] => 26
)
)
I am trying to turn it into an array that looks like this...
var $example_data = array(
array(
'ID' => 1,
'code' => 'AS34',
'09/12/14' => '0',
'10/12/14' => '32',
'11/12/14' => '42',
'12/12/14' => '40',
'13/12/14' => '0',
'14/12/14' => '0',
'15/12/14' => '44',
),
array(
'ID' => 2,
'code' => 'AH98',
'09/12/14' => '0',
'10/12/14' => '1',
'11/12/14' => '0',
'12/12/14' => '0',
'13/12/14' => '0',
'14/12/14' => '0',
'15/12/14' => '0',
),
array(
'ID' => 3,
'code' => 'AT78',
'09/12/14' => '0',
'10/12/14' => '0',
'11/12/14' => '0',
'12/12/14' => '1',
'13/12/14' => '0',
'14/12/14' => '0',
'15/12/14' => '0',
),
array(
'ID' => 4,
'code' => 'AL44',
'09/12/14' => '0',
'10/12/14' => '23',
'11/12/14' => '27',
'12/12/14' => '0',
'13/12/14' => '25',
'14/12/14' => '0',
'15/12/14' => '26',
),
);
So basically it sets up an array for each 'code' and then the previous 7 days. Can anybody point me in the direction of a similar example or some reading on the right method I should be looking at using?
I figured this:
$data = Array
(
0 => Array
(
"id" => 1,
"code" => "AS34",
"2014-12-10" => 32
),
1 => Array
(
"id" => 1,
"code" => "AS34",
"2014-12-11" => 42
),
2 => Array
(
"id" => 1,
"code" => "AS34",
"2014-12-12" => 40
),
3 => Array
(
"id" => 1,
"code" => "AS34",
"2014-12-15" => 44
),
4 => Array
(
"id" => 1,
"code" => "AH98",
"2014-12-10" => 1
),
5 => Array
(
"id" => 1,
"code" => "AT78",
"2014-12-12" => 1
),
6 => Array
(
"id" => 1,
"code" => "AL44",
"2014-12-10" => 23
),
7 => Array
(
"id" => 1,
"code" => "AL44",
"2014-12-11" => 27
),
8 => Array
(
"id" => 1,
"code" => "AL44",
"2014-12-13" => 25
),
9 => Array
(
"id" => 1,
"code" => "AL44",
"2014-12-15" => 26
)
);
function in_array_r($needle, $haystack, $strict = false) { //taken from http://stackoverflow.com/a/4128377/4263082
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
$codes = Array();
$counter = -1;
foreach($data as $key => $value) {
if(!in_array_r($data[$key]["code"], $codes, true)) {
$codes[++$counter] = Array();
foreach($data[$key] as $subkey => $subvalue) {$codes[$counter][$subkey] = $subvalue;}
}
else{
foreach($data[$key] as $subkey => $subvalue) {
if($subkey != "code" && $subkey != "id") {$codes[$counter][$subkey] = $subvalue;}
}
}
}
print_r($codes);
OUTPUT
Array
(
[0] => Array
(
[id] => 1
[code] => AS34
[2014-12-10] => 32
[2014-12-11] => 42
[2014-12-12] => 40
[2014-12-15] => 44
)
[1] => Array
(
[id] => 1
[code] => AH98
[2014-12-10] => 1
)
[2] => Array
(
[id] => 1
[code] => AT78
[2014-12-12] => 1
)
[3] => Array
(
[id] => 1
[code] => AL44
[2014-12-10] => 23
[2014-12-11] => 27
[2014-12-13] => 25
[2014-12-15] => 26
)
)
Something like (fixed):
$example_data = array();
foreach($data as $id => $row) {
$code = $row['code'];
unset($row['code']);
$karr = array_keys($row);
$date = current($karr);
$example_data[$code]['ID'] = $id;
$example_data[$code]['code'] = $code;
$example_data[$code][$date] = $row[$date];
}
print_r($example_data);
Test online
I got this working:
<?php
header ('Content-type: text/plain; charset=utf-8');
//source data from the question
$sourceArr = array(
array('id' => 1
,'code' => 'AS34'
,'2014-12-10' => '32'
)
,array('id' => 1
,'code' => 'AS34'
,'2014-12-11' => '42'
)
,array('id' => 1
,'code' => 'AS34'
,'2014-12-12' => '40'
)
,array('id' => 1
,'code' => 'AS34'
,'2014-12-15' => '44'
)
,array('id' => 1
,'code' => 'AH98'
,'2014-12-10' => '1'
)
,array('id' => 1
,'code' => 'AT78'
,'2014-12-12' => '1'
)
,array('id' => 1
,'code' => 'AL44'
,'2014-12-10' => '23'
)
,array('id' => 1
,'code' => 'AL44'
,'2014-12-11' => '27'
)
,array('id' => 1
,'code' => 'AL44'
,'2014-12-13' => '25'
)
,array('id' => 1
,'code' => 'AL44'
,'2014-12-15' => '26'
)
);
foreach($sourceArr as $k => $v)
{
//get dates for last seven days
$d0 = date('Y-m-d',time() - 60 * 60 * 24 * 0);
$d1 = date('Y-m-d',time() - 60 * 60 * 24 * 1);
$d2 = date('Y-m-d',time() - 60 * 60 * 24 * 2);
$d3 = date('Y-m-d',time() - 60 * 60 * 24 * 3);
$d4 = date('Y-m-d',time() - 60 * 60 * 24 * 4);
$d5 = date('Y-m-d',time() - 60 * 60 * 24 * 5);
$d6 = date('Y-m-d',time() - 60 * 60 * 24 * 6);
//if we have a valid element - then save it in temp array
if(array_key_exists($d0,$v)){ $tmpArr[$v['code']][$d0] = "'$v[$d0]'"; }
if(array_key_exists($d1,$v)){ $tmpArr[$v['code']][$d1] = "'$v[$d1]'"; }
if(array_key_exists($d2,$v)){ $tmpArr[$v['code']][$d2] = "'$v[$d2]'"; }
if(array_key_exists($d3,$v)){ $tmpArr[$v['code']][$d3] = "'$v[$d3]'"; }
if(array_key_exists($d4,$v)){ $tmpArr[$v['code']][$d4] = "'$v[$d4]'"; }
if(array_key_exists($d5,$v)){ $tmpArr[$v['code']][$d5] = "'$v[$d5]'"; }
if(array_key_exists($d6,$v)){ $tmpArr[$v['code']][$d6] = "'$v[$d6]'"; }
}
//create the result array
$cnt = 0;
foreach($tmpArr as $k => $v)
{
$resultArr[$cnt]['ID'] = $cnt;
$resultArr[$cnt]['code'] = "'$k'";
asort($v);
foreach($v as $k2 => $v2)
{
$resultArr[$cnt][$k2] = $v2;
}
$cnt++;
}
//output
echo "Old array: ".print_r($sourceArr,1);
echo "New array: ".print_r($resultArr,1);
?>
$series = array();
while($row = mysql_fetch_assoc($result)) {
$series[$row["data_id"]][] = $row;
}
The output from a print_r on $series yields for two example series:
Array (
[1] => Array ( [0] => Array ( [id] => 1 [data_id] => 1 [time_id] => 1
[data] => 1 ) [1] => Array ( [id] => 2 [data_id] => 1 [time_id] => 2
[data] => 3 ) )
[2] => Array ( [0] => Array ( [id] => 6 [data_id] => 2 [time_id] => 1
[data] => 7 ) [1] => Array ( [id] => 7 [data_id] => 2 [time_id] => 2
[data] => 4 ) )
My question: how do I unset the multidimensional array so it contains only [data] and none of the other keys? I still want $series to contain [1] and [2] but I do not want the respective sub-arrays to contain any other keys other than [data].
In fact, since I am reducing the subarrays to contain a single key, I would really like to get rid of the subarrays altogether so that I have two arrays:
$series[1] = array(1,3) and
$series[2] = array(7,4)
Try this :
$series = array();
while($row = mysql_fetch_assoc($result)) {
$series[$row["data_id"]][] = $row['data'];
}
I think you can loop in your array and build a new one keeping only data details
$array = array ('1' => array ( '0' => array ( 'id' => 1, 'data_id' => 1, 'time_id' => 1, 'data' => 1 ), '1' => array ( 'id' => 2, 'data_id' => 1, 'time_id' => 2, 'data' => 3 ), ),
'2' => array ( '0' => array ( 'id' => 6, 'data_id' => 2, 'time_id' => 1, 'data' => 7 ), '1' => array ( 'id' => 7, 'data_id' => 2, 'time_id' => 2, 'data' => 4 ) ));
$i= 0;
$n= 0;
$series = array();
foreach($array as $dato)
{
$series[$i] = array();
foreach($dato as $data)
{
foreach($data as $key => $value)
{
if($key == 'data')
{
$series[$i][$n] = $value;
$n++;
}
}
}
$n = 0;
$i++;
}
var_dump($series);
This will output
array (size=2)
0 =>
array (size=2)
0 => int 1
1 => int 3
1 =>
array (size=2)
0 => int 7
1 => int 4
Live demo
I am trying to use an array to search for a value that is inside of an array and then take the full array that the value is in and add it to an array. Below is the array to get the value from:
Array (
[0] => Array ( [ID] => 138 [dimmer] => 5 [order] => 1 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
[1] => Array ( [ID] => 139 [dimmer] => 6 [order] => 1 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
[2] => Array ( [ID] => 140 [dimmer] => 7 [order] => 2 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
[3] => Array ( [ID] => 141 [dimmer] => 8 [order] => 2 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
)
I am trying to get the value of dimmer with the search function below:
function search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, search($subarray, $key, $value));
}
return $results;
}
Below it uses the $chan value which is an integer to use the function above to search an array. The foreach is then supposed to go through the array that is $patch and select the arrays out of the array above (only returns an empty array), although it doesn't do that unless you change $patch_single['dimmer'] with a string such as "7".
$patch = search($patch, 'Channel', $chan);
foreach ($patch as $patch_single) {
print_r($patch_single);
$dim_single = intval($patch_single['dimmer']);
echo $dim_single;
$dimmers = search($dimmers, 'dimmer', $dim_single);
}
The array that is being used to get $patch_single['dimmer'] is, when inside the foreach:
Array ( [ID] => 241 [Channel] => 100 [dimmer] => 7 )
Array ( [ID] => 242 [Channel] => 100 [dimmer] => 25 )
Thank you for your advice.
Hm, i see that you have 2 dimensional array. Why you just don't use this?
foreach($array as $row) {
if ($row['dimmer'] == $myValue) { $newArray[] = $row; }
}
Try this :
$arr = Array (Array ( "ID" => 138, "dimmer" => 5, "order" => 1, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
Array ( "ID" => 139, "dimmer" => 6, "order" => 1, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
Array ( "ID" => 140, "dimmer" => 7, "order" => 2, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
Array ( "ID" => 141, "dimmer" => 8, "order" => 2, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ));
$arr = array_filter($arr, function($ar) {
return ($ar['dimmer'] == '7' );
});
echo "<pre>";
print_r($arr);
Output :
Array
(
[2] => Array
(
[ID] => 140
[dimmer] => 7
[order] => 2
[double] => 0
[location1] => DSR
[location2] => Stage Pockets
)
)
ref: http://php.net/manual/en/function.array-filter.php