this may seem a rather trivial question, please excuse my ignorance. Still getting the hang of array manipulation...
I have a CakePHP app that is posting an array to my controller to be saved. I need to somehow reformat the sent array so that it may be processed properly by Cake's Save behaviour.
The array posted is:
Array (
[788] => Array ( [id] => 788 )
[787] => Array ( [id] => 787 )
[786] => Array ( [id] => 0 )
[785] => Array ( [id] => 0 )
[value_1] => 0
[analysed_date] => Array (
[month] => 08
[day] => 16
[year] => 2011
)
[job_id] => 34
)
Desired Array:
Array (
[0] => Array (
[id] => 788
[value_1] => 0
[analysed_date] => Array (
[month] => 08
[day] => 16
[year] => 2011
)
)
[1] => Array (
[id] => 787
[value_1] => 0
[analysed_date] => Array (
[month] => 08
[day] => 16
[year] => 2011
)
)
)
Thanks for taking the time to look.
EDIT:
I've just realised I omitted the fact that if the array has an [id] => 0 that it needs to be ignored. This was my primary stumbling block. Apologies. I hope the edit clarifies my problem better.
SOLVED
Thank you for your help guys. I was able to come up with the solution by myself. Here is what I came up with.
foreach($org_array as $key => $value){
if(is_array($value)){
if(isset($value['id'])){
if($value['id'] != 0) {
$data[$i] = array(
'id' => $value['id'],
'value_1'=> $value_1,
'analysed_date' => $date
);
$i++;
}
}
}
}
Something like this should work, but just for your example:
$array_keys = array_keys($org_array);
$new_array = array();
foreach ($array_keys as $key)
{
if (is_int($key))
{
$new_array[] = array(
"id" => $key,
"value1" => $org_array["value1"],
"analysed_date" => $org_array["analysed_date"]
);
// you might want to loop throught the original array to get all non-integer key values instead of hard-coding it
}
}
$main = Array (
[788] => Array ( [id] => 788 )
[787] => Array ( [id] => 787 )
[786] => Array ( [id] => 786 )
[785] => Array ( [id] => 785 )
[value_1] => 0
[analysed_date] => Array (
[month] => 08
[day] => 16
[year] => 2011
)
[job_id] => 34
)
$analysed_date = $main['analysed_date'];
$value1 = $main['value_1'];
$result = array();
$i=0;
foreach($main as $key=>$value)
{
if( is_numeric($key)
{
$result[$i]=array();
$result[$i]['id']=$key;
$result[$i]['value_1']=$value1;
$result[$i]['analysed_date']=$analysed_date;
$i++;
}
}
Related
I have a multidimensional array as shown below. in the day's array, it has various days which has both working and non-working days. now I want to consider first type=" working" as the start_date.
could you help me. thanks
Array
(
[error] => 0
[data] => Array
(
[start_date] => 2018-03-11
[end_date] => 2018-03-21
[days] => Array
(
[0] => Array
(
[type] => non_working
[sub_type] => weekend
[sub_sub_type] =>
[date] => 2018-03-11
)
[1] => Array
(
[type] => working
[sub_type] =>
[sub_sub_type] =>
[full_date] => 2018-03-12
)
[2] => Array
(
[type] => working
[sub_type] =>
[sub_sub_type] =>
[full_date] => 2018-03-13
)
)
)
)
I've tries this as of now:
$i=0;
$var = array();
foreach($arr['data']['days'][$i] as $var) {
if($var['type'] == 'working') {
break;
}
}
Rework $arr['data']['days'][$i] to $arr['data']['days']:
$start = null;
foreach($arr['data']['days'] as $var) {
if($var['type'] == 'working') {
$start = $var['date'];
break;
}
}
I stuck on this and really don't know how to solve it.
I have two multi-dimensional arrays and need to match every "entry_id" from second array with first one. Then need to check if every "file_no" from second array is in database (first array) and "status" are matched with 1st array . If "status" is different, update second array with string (e.g. updated value) like this:
...
[status] => Array
(
[0] => abc
[1] => defghijk - "updated value"
)
So I have first array from database:
Array
(
[0] => Array
(
[entry_id] => 1
[file_no] => KSBR 40 INS 3674 / 2014
[status] => abc
)
[1] => Array
(
[entry_id] => 9
[file_no] => KSUL 77 INS 18898 / 2013
[status] => abc
)
[2] => Array
(
[entry_id] => 9
[file_no] => KSUL 77 INS 21218 / 2013
[status] => defg
)
)
And second array generated from script:
Array
(
[0] => Array
(
[entry_id] => 1
[id] => 500910/098
[fullname] => Milan Vrtal
[type] => person
[file_no] => Array
(
[0] => KSBR 26 INS 37146 / 2013
[1] => KSBR 40 INS 3674 / 2014
)
[status] => Array
(
[0] => status1
[1] => status2
)
)
[1] => Array
(
[entry_id] => 2
[id] => 46900217
[fullname] => ENTEC a.s.
[type] => company
[file_no] => Array
(
[0] => KSBR 28 INS 1232 / 2013
)
[status] => Array
(
[0] => qwer
)
)
[2] => Array
(
[entry_id] => 9
[fullname] => Blanka Kořínková
[type] => person
[file_no] => Array
(
[0] => KSUL 77 INS 18898 / 2013
[1] => KSUL 77 INS 21218 / 2013
)
[status] => Array
(
[0] => abc
[1] => defghijk
)
)
)
Thanks for every comment and sorry for english :)
This is by creating a temporary array to search in. This will use quite some memory when the arrays are big, but will result in faster execution time...
$tmparr = array();
foreach($arr1 as $arr1_val)
{
//put an new element in $temparr with key 'entry_id' and an array as value
if (!isset($tmparr[$arr1_val['entry_id']]))
$tmparr[$arr1_val['entry_id']] = array();
//add the status to the array
$tmparr[$arr1_val['entry_id']][] = $arr1_val['status'];
}
/*
$tmparr = Array
(
[1] => Array
(
[0] => abc
)
[9] => Array
(
[0] => abc
[1] => defg
)
)
*/
//arr2_val by reference so that we can change it
foreach($arr2 as &$arr2_val)
{
//get the current entry_id
$entry_id = $arr2_val['entry_id'];
//see if this entry_id was in the first array, and if so...
if (isset($tmparr[$entry_id]))
{
//change the status to both the original status and the status of the first array
$arr2_val['status'] = array_merge($arr2_val['status'],$tmparr[$entry_id]);
}
}
print_r($arr2);
Output:
Array
(
[0] => Array
(
[entry_id] => 1
[id] => 500910/098
[fullname] => Milan Vrtal
[type] => person
[file_no] => Array
(
[0] => KSBR 26 INS 37146 / 2013
[1] => KSBR 40 INS 3674 / 2014
)
[status] => Array
(
[0] => status1
[1] => status2
[2] => abc
)
)
[1] => Array
(
[entry_id] => 2
[id] => 46900217
[fullname] => ENTEC a.s.
[type] => company
[file_no] => Array
(
[0] => KSBR 28 INS 1232 / 2013
)
[status] => Array
(
[0] => qwer
)
)
[2] => Array
(
[entry_id] => 9
[fullname] => Blanka Kořínková
[type] => person
[file_no] => Array
(
[0] => KSUL 77 INS 18898 / 2013
[1] => KSUL 77 INS 21218 / 2013
)
[status] => Array
(
[0] => abc
[1] => defghijk
[2] => abc
[3] => defg
)
)
)
edit: This is possible too, whitout the temp array, but with a loop in a loop. This will be slower than the first one, but will consume less memory:
//arr2_val by reference so that we can change it
foreach($arr2 as &$arr2_val)
{
//get the current entry_id
$entry_id = $arr2_val['entry_id'];
//search for the correct row in the first array
foreach($arr1 as $arr1_val)
{
if ($arr1_val['entry_id'] == $arr2_val['entry_id'])
{
$arr2_val['status'][] = $arr1_val['status'];
//a continue should be added here to make it faster...
}
}
}
print_r($arr2);
This should work
foreach($array1 as $i)
{
foreach($array2 as $key=>$j)
{
if($j['entry_id'] == $i['entry_id'])
{
if($array2[$key]['status'] != $i['status'])
{
$j['status'] = array(
$i['status'],
$j['status'] // the new status
);
}
continue;
}
}
}
I found a solution for you :
$a1 = [['entry_id' => 1, 'file_no' => 'KSBR', 'status' => 'abc'], ['entry_id' => 2, 'file_no' => 'KSUL', 'status' => 'defg']];
$a2 = [['entry_id' => 1, 'file_no' => 'KSBR', 'status' => 'abc', 'type' => 'person'], ['entry_id' => 2, 'file_no' => 'KSUL', 'status' => 'defg']];
print_r(new_array_merge_recursive($a1, $a2));
function new_array_merge_recursive(array $array1, array $array2=array())
{
$arrays = func_get_args();
$merge = array_shift($arrays);
foreach ($arrays as $array)
{
foreach ($array as $key => $val)
{
if (is_array($val) && array_key_exists($key, $merge))
{
$val = new_array_merge_recursive((array) $merge[$key], $val);
}
$merge[$key] = $val;
}
}
return $merge;
}
I have array like this
Array ([0] => Array ( [user_id] => 21 [email] => momod#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W ) ) [1] => Array ( [user_id] => 22 [email] => hemisphere#modara.com [brand] => Array ( [0] => GOFUEL_W ) ) [2] => Array ( [user_id] => 23 [email] => madoka#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W [2] => GOFUEL_BGD_W ) ) )
i want to locate user_id 22 and put this value "GO_FUEL_SGD_W" on brand, what should i do, so the view of array will look like this
Array ([0] => Array ( [user_id] => 21 [email] => momod#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W ) ) [1] => Array ( [user_id] => 22 [email] => hemisphere#modara.com [brand] => Array ( [0] => GOFUEL_W => [1] =>GO_FUEL_SGD_W ) ) [2] => Array ( [user_id] => 23 [email] => madoka#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W [2] => GOFUEL_BGD_W ) ) )
Just use loop:
foreach($array as &$item)
{
if(array_key_exists('user_id', $item) &&
$item['user_id']==22 &&
array_key_exists('brand', $item) &&
!in_array('GO_FUEL_SGD_W', $item['brand']))
{
$item['brand'][] = 'GO_FUEL_SGD_W';
}
}
A simple foreach loop will do the job:
foreach($myarray AS &$subarray) {
if($subarray['user_id'] == 22) {
$subarray['brand'][] = "GO_FUEL_SGD_W";
break;
}
}
Working example: http://3v4l.org/8aQMj
You will need to iterate over the array and look for the element you're searching for.
foreach ($array as &$element) {
if ($element['user_id'] != 22)
continue;
$element['brand'][] = "GO_FUEL_SGD_W";
break;
}
With continue; all elements will be skipped, who have $element['user_id'] != 22 (and so none of the code after the continue; will be applied to them!).
Also it will end the loop once the requested element is reached and modified, thanks to break;.
$array= //your array;
foreach($array as $x){
if($x['user_id']=='22'){
$x['brand'][]='GO_FUEL_SGD_W';
break;
}
}
I'm sure this is easy for someone well-versed in php, but I've made the mistake of overloading my brain, so now I'm really confused as to whether I should use array_combine, array_merge, or something else... I've been googling and reading php.net for 4 hours and I think I'm just confusing myself even more...
Essentially, I just want to combine an array while keeping the keys?
//Here are the original arrays
[field_sreference] => Array
(
[0] => Array
(
[nid] => 28
)
[1] => Array
(
[nid] => 28
)
[2] => Array
(
[nid] => 29
)
)
[field_idelta] => Array
(
[0] => Array
(
[value] => 0
)
[1] => Array
(
[value] => 1
)
[2] => Array
(
[value] => 0
)
)
[field_iswitch] => Array
(
[0] => Array
(
[value] => 0
)
[1] => Array
(
[value] => 0
)
[2] => Array
(
[value] => 0
)
)
//Here is what I'm trying to achieve:
[combinedarray] => Array
(
[0] => Array
(
[nid] => 28
[idelta] => 0
[iswitch] => 0
)
[1] => Array
(
[nid] => 28
[idelta] => 1
[iswitch] => 0
)
[2] => Array
(
[nid] => 29
[idelta] => 0
[iswitch] => 0
)
)
You can solve this is O(n) by simply iterating the arrays...
$combinedarray = array();
$len = count($field_sreference);
for ($i = 0; $i < $len; $i++) {
$combinedarray[] = array("nid" => $field_sreference[$i]['nid'],
"idelta" => $filed_idelta[$i]['value'],
"iswitch" => $field_iswitch[$i]['value']);
}
This assumes, the 3 arrays are all of equal length.
A bit quickly typed, but this should work:
$result = array();
foreach ($arrays as $array)
{
foreach ($array as $index => $data)
{
$result[$index] += $data;
}
}
As you have not provided some input array in some easy form, you need to test it on your own. Let's say it's pseudo-code and I leave it here as an exercise. The + operator is the array union operator.
and i got a problem (its big for me) :(
ok, i have some array like ...
Array(
[0] => Array
(
[id] => 1
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
[3] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[4] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
how to merge them if the array have same key value ... ?
the result that i need got is like this ...
Array(
[0] => Array
(
[id] => 1
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
)
)
[2] => Array
(
[id] => 2
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
)
)
im very need some help here, and im working on PHP ... what method should i do for this case?
i try too searching on google and in here ... but i dont know the keyword >.<
Many thanks before :)
regard, Stecy
Try like this:
<?php
$result = array();
foreach ($my_array as $v) {
$id = $v['id'];
$result[$id]['id'] = $id;
$result[$id]['detail'][] = array(
'order_sn' => $v['order_sn'],
'total' => $v['total'],
);
}
You can just loop over the array and build a resulting one:
// $a is your array
$r=array();
foreach($a as $v)
$r[$v['id']][]=array('order_sn'=>$v['order_sn'], 'total'=>$v['total']);
echo'<pre>';
var_dump($r);
Since you do the paring by ID, it is wise to have it as the key and all the data associated with it as the value. There's no need to also have id and detail.
foreach($origianlArray as $key => $value){
$newArray[$value['id']]['id'] = $value['id'];
$newArray[$value['id']]['detail'][] = array('order_sn' => $value['order_sn'], 'total' => $value['total']);
}
Check out the PHP array_merge function.