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);
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);
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 have an multidimensional array like this, but i just only need some index to be displayed,
Array
Array
(
[0] => Array
(
[1] => 220
[38] => 200
[232] => 970
)
[1] => Array
(
[0] => 220
[2] => 190
[39] => 200
)
[2] => Array
(
[1] => 190
[3] => 40
[50] => 220
)
[3] => Array
(
[2] => 40
[4] => 200
[57] => 120
)
)
then i just want to display only index [1] and [3], so it would be like this
Array
(
[1] => Array
(
[0] => 220
[2] => 190
[39] => 200
)
[3] => Array
(
[2] => 40
[4] => 200
[57] => 120
)
)
i try using this code
$order = array(1,3);
uksort($graph, function($key1, $key2) use ($order) {
return (array_search($key1, $order) > array_search($key2, $order));
});
but still, its displayed the rest array that i dont need it that's key [0] and [2]
Like this:
foreach(array_keys($graph) as $key)
{
if($key == 0|| $key == 2)
{
unset($graph[$key]);
}
}
print_r
Array
(
[1] => Array
(
[0] => 220
[2] => 190
[39] => 200
)
[3] => Array
(
[2] => 40
[4] => 200
[57] => 120
)
)
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!
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] => a [1] => d [2] => c [3] =>b [4] => )
)
and was wondering whether I can copy all inner array from it to another array where the first element is "b" so it looks like. Will the new array be reindexed when created? Thank you.
Array (
[0] => Array ( [0] => b [1] => d [2] => c [3] =>a [4] => )
[1] => Array ( [0] => b [1] => d [2] => a [3] => [4] => )
)
UPDATE: Had a little error in my code. Now it is fixed and working if you still need it.
$firstArray = array (array ('b', 'd', 'c', 'a'),
array ('c', 'd', 'a', 'b'),
array ('b', 'd', 'a', 'c'),
array ('a', 'd', 'c', 'b'));
$secondArray = array();
foreach($firstArray as $sub) {
if($sub[0] == 'b') {
$secondArray[] = $sub;
}
}
print_r($secondArray);
Output:
Array ( [0] => Array ( [0] => b [1] => d [2] => c [3] => a )
[1] => Array ( [0] => b [1] => d [2] => a [3] => c ) )
function check($val) {
return ($val[0] == 'b');
}
$secondArray = array_filter($firstArray, "check");