i've been busting my head over this array:
Array (
[invoicenr] => Array (
[0] => 1234
[1] => 1234
[2] => 1234
[3] => 4321
[4] => 3214
)
[invoicedate] => Array (
[0] => 17.07.2017.
[1] => 17.07.2017.
[2] => 17.07.2017.
[3] => 11.07.2017.
[4] => 11.07.2017. )
[amount] => Array (
[0] => 10
[1] => 1
[2] => 23
[3] => 10
[4] => 1 )
[cause] => Array (
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1 )
)
I'm trying to split array above based on 1st level key "invoicenr" value, but without luck so far.
I'm expecting result:
Array (
[invoicenr] => Array (
[0] => 1234
[1] => 1234
[2] => 1234)
[invoicedate] => Array (
[0] => 17.07.2017.
[1] => 17.07.2017.
[2] => 17.07.2017.)
[amount] => Array (
[0] => 10
[1] => 1
[2] => 23)
[cause] => Array (
[0] => 1
[1] => 1
[2] => 1)
)
Array (
[invoicenr] => Array (
[0] => 4321 )
[invoicedate] => Array (
[0] => 11.07.2017. )
[amount] => Array (
[0] => 10)
[cause] => Array (
[0] => 1 )
)
Array (
[invoicenr] => Array (
[0] => 3214 )
[invoicedate] => Array (
[0] => 11.07.2017. )
[amount] => Array (
[0] => 1)
[cause] => Array (
[0] => 1 )
)
I want to know if this is possible and how, or i need to rewrite array first?
Thank you all in advance, i'm new into coding and struggling to learn so far. :)
Please post the code you attempted to solve the problem with.
I believe you will need a counter (i) to go through invoicenr and a temporary variable that remembers the last value (Array[i]) and compares it with the next value in the array of invoicenr. If the value is the same you keep going, and if it is different you print out all the other arrays[i].
i've finnaly did it, but i think my way is too primitive. Here is what i did:
//Count number of rows in initial array
$rows = count(array_filter($_POST['invoicenr']));
//Crete unique array that contains only $_POST['invoicenr'] which i can count later
$uniquearray = array();
for ($row = 0; $row < $rows; $row++) {
$uniquearray[] = $_POST['invoicenr'][$row] ;
}
//return unique keys only
$invoicenrunique=array_keys(array_flip($uniquearray));
//count number of rows in unique array
$invoicenruniquecount=count(array_keys(array_flip($uniquearray)));
//do magic, this splits main array into n smaller arrays based on $_POST['invoicenr']
for($uniquerow = 0; $uniquerow < $invoicenruniquecount; $uniquerow++) {
$testarray = array();
for ($row = 0; $row < $rows; $row++) {
if($_POST['invoicenr'][$row]==$invoicenrunique[$uniquerow]){
$testarray[$row]['invoicenr']=$_POST['invoicenr'][$row];
$testarray[$row]['invoicedate']=$_POST['invoicedate'][$row];
$testarray[$row]['amount']=$_POST['amount'][$row];
$testarray[$row]['cause']=$_POST['cause'][$row];
}
}
print_r(array_values($testarray));
echo "<br>";
}
Do you have any suggestions on how to improve my solution?
Best regards!
Related
-I cannot figure out how to regroup this array accourding to ids.
This array comes from a mysql table. the number 17 and 20 are foreign keys in the table.
$orginal_arr =
Array
(
[0] => Array
(
[id] => 17
[content] => string...?
)
[1] => Array
(
[id] => 20
[content] => hello
)
[2] => Array
(
[id] => 20
[content] => string...?
)
[3] => Array
(
[id] => 20
[content] => string...string...??
)
[4] => Array
(
[id] => 17
[content] => string...
)
[5] => Array
(
[id] => 17
[content] => string...
)
);
I want to convert the above $orginal_arr array to the following $desired_arr array.
$desired_arr = Array (
[0] => Array
(
[17] => Array
(
[0] => string...?
)
)
[1] => Array
(
[20] => Array
(
[0] => hello
[1] => string...?
[2] => string... string...??
)
)
[2] => Array
(
[17] => Array
(
[0] => string...
[1] => string...
)
)
);
so far I am trying the following approach:
function group_by_key($key, $data)
{
$result = array();
$j = 0;
foreach ($data as $val) {
if (array_key_exists($key, $val)) {
$result[$val[$key]][$j] = $val;
} else {
$result[""][] = $val;
}
$j++;
}
return $result;
}
$desired_arr = group_by_key("id", $orginal_arr);
$desired_arr2 = array();
foreach ($desired_arr as $index => $item) {
$desired_arr2 += $item;
}
echo "<pre>";
print_r($desired_arr2);
echo "</pre>";
the output:
Array
(
[0] => Array
(
[id] => 17
[content] => string...?
)
[4] => Array
(
[id] => 17
[content] => string...
)
[5] => Array
(
[id] => 17
[content] => string...
)
[1] => Array
(
[id] => 20
[content] => hello
)
[2] => Array
(
[id] => 20
[content] => string...?
)
[3] => Array
(
[id] => 20
[content] => string...string...??
)
)
- Thank you very much.
As I said in my comment, I'm not familiar to PHP syntax anymore so this will be pseudo code.
I would do it in 2 steps,
first I want this result
$temp_arr =
Array
(
[0] => Array
(
[id] => 17
[contents] => Array(
[0] => string...?
)
)
[1] => Array
(
[id] => 20
[contents] => Array(
[0] => hello
[1] => string...?
[2] => string...string...??
)
)
...
...
);
See, I kept the Object structure with id and contents (with a final S).
then it will be easier to check the id as it is directly accessible
$temp_array = Array();
foreach ($original_array as $element) {
$last = end($temp_array);
if ($last && $element->id == $last->id) {
// add to the last $temp_array element
$temp_array[count($temp_array)-1]->contents[] = $element->content;
}
else {
// create a new $temp_array element
$temp_array[] = Array(
"id" => $element->id,
"contents => Array(
$element->content
)
);
}
}
For the second step you just need to iterate the $temp_array and format as you like
here is another solution to this problem.
$desired_arr = array();
$prev_id = '';
$prev_key = null;
foreach ($orginal_arr as $key => $value) {
$this_id = $value['id'];
if ($this_id == $prev_id) {
array_push($desired_arr[$prev_key][$value['id']], $value['content']);
} else {
$desired_arr[$key][$value['id']] = (array)$value['content'];
$prev_key = $key;
}
$prev_id = $this_id;
}
$desired_arr = array_values($desired_arr);
Houston I have a Problem. I have this preg_match_all the response is many array with duplicate numbers (the original data has these duplicates). I need to eliminate duplicate numbers and transfer to one variable. I Try with array_unique() and array_merge(). Thx for your help.
preg_match_all('~x">([^"]*)<\/h4>|[0-9]{5}~',$preg_data,$item, PREG_SET_ORDER);
Output:
Array ( )
Array ([0] => x"> 13:30 - Denver</h4> [1] => 13:30 - Denver)
Array ( )
Array ([0] => 69275)Array([0] => Array([0] => 69275[1] => 69275))
Array ([0] => 69275)Array([0] => Array([0] => 69275[1] => 69275))
Array ([0] => 69275)Array([0] => Array([0] => 69275[1] => 69275))
Array ()
Array ([0] => x"> 16:00 - Miami</h4>[1] => 16:00 - Miami)
Array ()
Array ([0] => 69275)Array([0] => Array([0] => 69275[1] => 69280))
Array ([0] => 69275)Array([0] => Array([0] => 69275[1] => 69280))
Array ([0] => 69275)Array([0] => Array([0] => 69275[1] => 69280))
if (#preg_match_all('/([0-9]{5})/',$item[0],$match, PREG_SET_ORDER)); {
print_r($match);}
Array ( )
Array ( [0] => Array ( [0] => 69268 [1] => 69268 ))
Array ( [0] => Array ( [0] => 69268 [1] => 69268 ))
Array ( )
Array ( [0] => Array ( [0] => 69270 [1] => 69270 ))
Array ( [0] => Array ( [0] => 69270 [1] => 69270 ))
Array ( )
Array ( )
Array ( [0] => Array ( [0] => 69270 [1] => 69270 ))
Array ( [0] => Array ( [0] => 69270 [1] => 69270 ))
Array ( )
Array ( )
Array ( [0] => Array ( [0] => 69270 [1] => 69270 ))
Array ( [0] => Array ( [0] => 69270 [1] => 69270 ))
Array ( )
I use this combinations
preg_match_all('~x">([^"]*)<\/h4>|[0-9]{5}~',$preg_data,$data_mach);
$data_clean= array_values(array_filter(array_unique($data_mach[0])));
preg_mach to extrac data
array_unique to eliminate duplicates
array_filter to eliminate blank
array_values to work only numbers.
Done.
I have two arrays
The first array:
Array ( [0] => stdClass Object ( [idConsultant] => 291 ) [1] => stdClass Object ( [idConsultant] => 292 ) [2] => stdClass Object ( [idConsultant] => 293 ) )
The second array:
Array ( [0] => stdClass Object ( [idConsultant] => 291 ) [1] => stdClass Object ( [idConsultant] => 291 ) [2] => stdClass Object ( [idConsultant] => 292 ) )
I need a function how will return me, foreach value in the first array the number of occurrence in the second.
The result i am looking for is:
Array ( [0] => stdClass Object ( [291] => 2 ) [1] => stdClass Object ( [292] => 1 ) [2] => stdClass Object ( [293] => 0 ) )
Thank you.
there is a function called array_count_values - which is pretty handy in your situation
something like the following should work (assuming your arrays are named arrA and arrB
$arrCntValues = array_count_values(
array_merge
(
array_column($arrA, 'idConsultant'),
array_column($arrB, 'idConsultant')
)
);
print_r($arrCntValues);
and if you really need it like your desired structure just iterate over it
$arrObjects = [];
foreach($arrCntValues AS $key => $val)
{
$obj = new stdClass();
$obj->$key = $val;
$arrObjects[] = $obj;
}
print_r($arrObjects);
I want to save my array with multiple indexing array values.
Demo code
Array
(
[CodeConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] =>
)
)
[ObjectAccountConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] => 2
)
)
[TaxConfiguration] => Array
(
[0] => Array
(
[LineNo] => 2
[IsDirty] => 1
)
[1] => Array
(
[LineNo] => 1
[IsDirty] => 1
)
)
)
I want to save this array values direct into table .Table name is audit_trail_details.
so please suggest mi proper solution for how to save this data into table.
You need to use saveAssociated and parse your array as the first parameter:-
$this->AuditTrailDetail->saveAssociated($data);
You will also need to make sure $data contains the AuditTrailDetail you are saving the associated data against. For example:-
Array
(
[AuditTrailDetail] => Array
(
[id] => 1
)
[CodeConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] =>
)
)
[ObjectAccountConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] => 2
)
)
[TaxConfiguration] => Array
(
[0] => Array
(
[LineNo] => 2
[IsDirty] => 1
)
[1] => Array
(
[LineNo] => 1
[IsDirty] => 1
)
)
)
I have this array:
Array (
[0] => Array ( [0] => b [1] => d [2] => **c** [3] =>a [4] => )
[1] => Array ( [0] => **c** [1] => a [2] => d [3] => [4] => )
[2] => Array ( [0] => b [1] => d [2] => a [3] => [4] => )
[3] => Array ( [0] => **c** [1] => d [2] => a [3] =>b [4] => )
)
and need to delete (unset?) all elements where value is "c" so that one ends up with:
Array (
[0] => Array ( **[0] => b [1] => d [2] => a [3] => [4] =>** )
[1] => Array ( **[0] => a [1] => d [2] => [3] =>** )
[2] => Array ( [0] => b [1] => d [2] => a [3] => [4] => )
[3] => Array ( **[0] => d [1] => a [2] =>b [3] =>** )
)
The element gets removed, and the other elements to shift up. I know that unset does not re-index the array. Cannot get to unset for all multidimensional arrays, but only with one array. Can the arrays be re-indexed afterwards? Appreciate it.
The code BELOW removes elements where the value is equal to "c" but the index of the first element is not re-indexed. Can anyone suggest a solution to re-indexing the inner arrays?
$i=0;
foreach ($array as $val)
{
foreach ($val as $key => $final_val)
{
if ($final_val =="$search_value")
{
unset($array[$i][$key]);
}
}
i = $i + 1;
}
The following code will do what you want:
<?php
$a = 1;
$b = 2;
$c = 3;
$d = 4;
$arr = array(
array ( $b, $d, $c, $a, $b),
array ($c, $a),
array ( $b, $d, $c ),
array( $c, $d, $a, $b, $b)
);
echo "before:\n";
print_r($arr);
foreach($arr as $k1=>$q) {
foreach($q as $k2=>$r) {
if($r == $c) {
unset($arr[$k1][$k2]);
}
}
}
echo "after:\n";
print_r($arr);
?>
Output:
before:
Array
(
[0] => Array
(
[0] => 2
[1] => 4
[2] => 3
[3] => 1
[4] => 2
)
[1] => Array
(
[0] => 3
[1] => 1
)
[2] => Array
(
[0] => 2
[1] => 4
[2] => 3
)
[3] => Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 2
[4] => 2
)
)
after:
Array
(
[0] => Array
(
[0] => 2
[1] => 4
[3] => 1
[4] => 2
)
[1] => Array
(
[1] => 1
)
[2] => Array
(
[0] => 2
[1] => 4
)
[3] => Array
(
[1] => 4
[2] => 1
[3] => 2
[4] => 2
)
)
As you can see, all the 3's have gone...
Search the value in the sub array then unset it.
$search = 'c';
$result = array_map(function ($value) use ($search) {
if(($key = array_search($search, $value)) !== false) {
unset($value[$key]);
}
return $value;
}, $your_array);
Or you could use a loop too:
// this way change your original array
foreach ($your_array as &$sub_array) {
if(($key = array_search($search, $sub_array)) !== false) {
unset($sub_array[$key]);
}
}
var_dump($your_array);