I have an array, when i tried to print_r was just like this :
Array ( [user_id] => Erick ) Array ( [user_id] => Baldi) Array([user_id]=> Bintang ) Array ( [user_id] => Bagas ) Array ( [user_id] => Baim )
My Expected output just like :
Array (
Array (
[user_id] => Erick
)
Array (
[user_id] => Baldi
)
Array (
[user_id] => Bintang
)
Array (
[user_id] => Bagas
)
Array (
[user_id] => Baim
)
)
Anyone here have an idea ? I'm stuck with this.
This my php code :
public function get_userid() {
// $action = $this->input->post('action');
$customerField = $this->input->post('customer');
$projectField = $this->input->post('project');
$user_roleField = $this->input->post('role');
for ($i=0; $i<count($customerField); $i++) {
for ($j=0; $j<count($projectField); $j++) {
for ($k=0; $k<count($user_roleField); $k++) {
array_push($test, $this->get_array_push($customerField[$i], $projectField[$j], $user_roleField[$k]));
}
}
}
}
public function get_array_push($customer, $project, $user_role) {
$query = $this->db->query("SELECT user_id
FROM `ixt_user_project_list`
LEFT JOIN ixt_user_type ON ixt_user_project_list.user_type = ixt_user_type.user_type
WHERE ixt_user_project_list.user_cust_id ='".$customer."'
AND ixt_user_project_list.user_project_id ='".$project."'
AND ixt_user_type.user_owner ='".$user_role."'")->result_array();
//Filter null array
foreach ($query as $key => $value) {
print_r($value);
}
}
And this is my initial query value :
Array ( [0] => Array ( [user_id] => Erick ) ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( [0] => Array ( [user_id] => Baldi ) [1] => Array ( [user_id] => Bintang ) ) Array ( [0] => Array ( [user_id] => Bagas ) [1] => Array ( [user_id] => Baim ) )
As #scaisEdge wrote - its happening because you use the print_r inside the foreach loop.
If you want to filter null result you can use array-filter as:
$arr = array(array("user_id" => "aaa"), null, array(), array("user_id" => null), array("otherField" => "bbb"));
$filterdResult = array_filter($arr, function($elem) {
return ($elem && array_key_exists("user_id", $elem) && !is_null($elem["user_id"]));
});
Now you can just do print_r($filterdResult );
This will output:
Array
(
[0] => Array
(
[user_id] => aaa
)
)
Edited: After setting your new input as:
$query = array(array(array("user_id" => "aaa"), null, array(), array("user_id" => null)), array(array("user_id" => "ccc"), null, array(),array("user_id" =>"ddd")));
You can use the following:
$res = array();
//Filter null array
foreach ($query as $key => $value) {
array_push($res, array_filter($value, function($elem) {
return ($elem && array_key_exists("user_id", $elem) && !is_null($elem["user_id"])); }));
}
print_r($res);
Which output:
Array
(
[0] => Array
(
[0] => Array
(
[user_id] => aaa
)
)
[1] => Array
(
[0] => Array
(
[user_id] => ccc
)
[3] => Array
(
[user_id] => ddd
)
)
)
From here flatten is available if needed.
#David Winder I tried to combine as your suggesstion with the null array filter.
$res = array();
//Filter null array
foreach ($query as $key => $value) {
if (!is_null($value)) {
$res[] = $value;
//print_r($res);
}
}
But when i print_r($res) inside the foreach loop the result became duplicate array inside all of the array value.
Related
I have a multidimensional associative array which has a set of array. I want to change my array index value from some array value.
I already tried some array functions but my array also contains some null array so laravel function keyBy not give me wanted result.
$arr1=array(0 =>array(),1=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
2 =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
My expected result array must be like this
$arr2=array(0 =>array(),'baroque'=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
'adidas' =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
You can use the classic foreach. Check if the handle on element 0 exists using isset, if it does, use that as the key.
$arr1 = //...
$result = array();
foreach($arr1 as $key => $val) {
if (is_array($val) && isset($val[0]["handle"])) $result[ $val[0]["handle"] ] = $val;
else $result[$key] = $val;
}
$result will be:
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
You can use without condition by grouping at the handle as key directly.
$result = [];
foreach ($arr as $key => $value) {
if (!empty($value)) {
foreach ($value as $key1 => $value1) {
$result[$value1['handle']][] = $value1;
}
} else {
$result[] = $value;
}
}
Demo
Output:-
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
Try this..
$res = [];
foreach($x as $key => $value)
{
if(empty($value))
{
$res[] = $value;
}
else
{
foreach($value as $v => $k)
{
if(array_key_exists($k['handle'],$res))
{
$res[$k['handle']][] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
else
{
$res[$k['handle']][0] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
}
}
}
The result is going to be like this.
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
I have two arrays with different structures. Array 1 and Array 2 that I will name from MyList and MyFiles. I would get to return only MyList values that do not have in MyFiles. But the two arrays have different structures and I'm having trouble trying to compare
MyList
Array
(
[info] => Array
(
[0] => Array
(
[player] => Messi
[week] => Array
(
[id] => 252
[videos] => Array
(
[0] => Array
(
[id] => 2929850
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929848
[link] => best.mp4
)
[2] => Array
(
[id] => 2929847
[link] => dribbling.mp4
)
)
)
)
[1] => Array
(
[player] => CR7
[week] => Array
(
[id] => 251
[videos] => Array
(
[0] => Array
(
[id] => 2929796
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929795
[link] => best.mp4
)
)
)
)
[2] => Array
(
[player] => Neymar
[week] => Array
(
[id] => 253
[videos] => Array
(
[0] => Array
(
[id] => 2929794
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929793
[link] => best.mp4
)
)
)
)
)
)
MyFiles Array
Array
(
[252] => Array
(
[0] => Array
(
[id] => 2929850
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929848
[link] => best.mp4
)
)
[251] => Array
(
[0] => Array
(
[id] => 2929796
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929795
[link] => best.mp4
)
)
)
the comparison must be made by id of the week and id of the video
I tried this but it did not work out:
$new = array();
foreach ($list['info'] as $source) {
foreach ($source["week"]['videos'] as $keys => $videos) {
foreach ($file as $key => $upload) {
if ($source["week"]["id"] == $key ) {
for($i=0; $i<count($source["week"]["videos"]); $i++){
if($videos["id"] == $upload[$i]["id"]){
unset($videos);
}else{
$new[] = $videos;
}
}
} else {
$new[] = $videos;
}
}
}
}
The expected return would be something like this.
Array
(
[info] => Array
(
[0] => Array
(
[player] => Messi
[week] => Array
(
[id] => 252
[videos] => Array
(
[2] => Array
(
[id] => 2929847
[link] => dribbling.mp4
)
)
)
)
[2] => Array
(
[player] => Neymar
[week] => Array
(
[id] => 253
[videos] => Array
(
[0] => Array
(
[id] => 2929794
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929793
[link] => best.mp4
)
)
)
)
)
)
I have hidden the array in a usable format for my sake in the future should this answer be incorrect and need changing.
$desired = array();
$desired['info'][0]['player'] = 'Messi';
$desired['info'][0]['week']['id'] = 252;
$desired['info'][0]['week']['videos'][2]['id'] = 2929847;
$desired['info'][0]['week']['videos'][2]['link'] = 'dribbling.mp4';
$desired['info'][2]['player'] = 'Neymar';
$desired['info'][2]['week']['id'] = 253;
$desired['info'][2]['week']['videos'][0]['id'] = 2929794;
$desired['info'][2]['week']['videos'][0]['link'] = 'goals.mp4';
$desired['info'][2]['week']['videos'][1]['id'] = 2929793;
$desired['info'][2]['week']['videos'][1]['link'] = 'best.mp4';
$list = array();
$list["info"][0]["player"] = "Messi";
$list["info"][0]["week"]["id"] = "252";
$list["info"][0]["week"]["videos"][0]["id"] = 2929850;
$list["info"][0]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][0]["week"]["videos"][1]["id"] = 2929848;
$list["info"][0]["week"]["videos"][1]["link"] = "best.mp4";
$list["info"][0]["week"]["videos"][2]["id"] = 2929847;
$list["info"][0]["week"]["videos"][2]["link"] = "dribbling.mp4";
$list["info"][1]["player"] = "CR7";
$list["info"][1]["week"]["id"] = "251";
$list["info"][1]["week"]["videos"][0]["id"] = 2929796;
$list["info"][1]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][1]["week"]["videos"][1]["id"] = 2929795;
$list["info"][1]["week"]["videos"][1]["link"] = "best.mp4";
$list["info"][2]["player"] = "Neymar";
$list["info"][2]["week"]["id"] = "253";
$list["info"][2]["week"]["videos"][0]["id"] = 2929794;
$list["info"][2]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][2]["week"]["videos"][1]["id"] = 2929793;
$list["info"][2]["week"]["videos"][1]["link"] = "best.mp4";
$file = array();
$file[252][0]['id'] = 2929850;
$file[252][0]['link'] = 'goals.mp4';
$file[252][1]['id'] = 2929848;
$file[252][1]['link'] = 'best.mp4';
$file[251][0]['id'] = 2929796;
$file[251][0]['link'] = 'goals.mp4';
$file[251][1]['id'] = 2929795;
$file[251][1]['link'] = 'best.mp4';
Edit
function array_diff_assoc_recursive($array1, $array2) {
$difference=array();
foreach ($array1 as $key => $value) {
if (is_array($value)) {
if( !isset($array2[$key]) || !is_array($array2[$key])) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if (!empty($new_diff))
$difference[$key] = $new_diff;
}
} else if (!array_key_exists($key,$array2) || $array2[$key] !== $value) {
$difference[$key] = $value;
}
}
return $difference;
}
$new = array('info' => array());
foreach ($list['info'] as $key => $item) {
$a = $item['week']['videos'];
//$b = $file[$item['week']['id']] ?? []; // This is PHP7+
$b = isset($file[$item['week']['id']]) ? $file[$item['week']['id']] : [];
$c = array_diff_assoc_recursive($a, $b);
if (!empty($c)) {
$new['info'][$key] = $item;
$new['info'][$key]['week']['videos'] = $c;
}
}
You will need a function that will check the difference between the videos arrays.
What I do is simply iterate over the list array and then check the difference between that item and the file array. The difference is then stored in $c.
If there is a difference then if statement is fired which will store that player in the $new array and then replace the videos array with the difference array.
This is similar to what you were doing when you were unsetting variables.
i have an array $mainArray of arrays and i would like to remove / undset the arrays that vave keys with no value.
here is my array:
Array
(
[0] => Array
(
[msg_id] => 203
[comment] => Array
(
[0] => Array
(
[com_id] =>
)
)
)
[1] => Array
(
[msg_id] => 202
[comment] => Array
(
[0] => Array
(
[com_id] => 196
)
[1] => Array
(
[com_id] => 197
)
[2] => Array
(
[com_id] =>
)
)
)
[2] => Array
(
[msg_id] => 201
[comment] => Array
(
[0] => Array
(
[com_id] => 198
)
[1] => Array
(
[com_id] =>
)
)
)
)
In this case i would like to look inside comment array arrays and see if there are any of them that have empty values. The best case scenario would be to remove the comment array entirely if all sub arrays are empty.
nut im of with leaving the comment hey there just null
this array should become:
Array
(
[0] => Array
(
[msg_id] => 203
)
[1] => Array
(
[msg_id] => 202
[comment] => Array
(
[0] => Array
(
[com_id] => 196
)
[1] => Array
(
[com_id] => 197
)
)
)
[2] => Array
(
[msg_id] => 201
)
)
any ideas on how to proceed?
thanks.
array_filter() is what you're after. Particularly a recursive version. The following was taken from a comment on the PHP Doc:.
function array_filter_recursive($array, $callback = null) {
foreach ($array as $key => & $value) {
if (is_array($value)) {
$value = array_filter_recursive($value, $callback);
}
else {
if ( ! is_null($callback)) {
if ( ! $callback($value)) {
unset($array[$key]);
}
}
else {
if ( ! (bool) $value) {
unset($array[$key]);
}
}
}
}
unset($value);
return $array;
}
Use php's unset() to unset any array key/value.
More info on this link http://in3.php.net/unset
in your case the code can be, (i have not tested it. but let me know if you have any problem and i can fix it)
function unsetCommentFromArray($mainArray) {
foreach($mainArray as $key => $value) {
foreach($value['comment'] as $k => $v) {
if(empty($v['com_id'])) {
unset($mainArray[$key]['comment'][$k]);
}
}
}
return $mainArray;
}
$array = array_map(function ($i) {
$i['comment'] = array_filter($i['comment'], function ($c) { return $c['com_id']; });
return array_filter($i);
}, $array);
Requires PHP 5.3 or higher.
I have this array:
Array ( [0] =>
Array (
[0] => Array (
[07] => Array (
[2] => 352.9
[3] => 375737
[4] => 1000000002
) )
[1] => Array (
[07] => Array (
[2] => 362.1
[3] => 375797
[4] => 1000000002
) )
)
Array ( [1] =>
[0] => Array (
[08] => Array (
[2] => 305.7
[3] => 375857
[4] => 1000000002
) )
)
)
i need a final array sum for the key 07 ( is the month ) like this:
Array ( [0] =>
Array (
[0] => Array (
[07] => Array (
[2] => 3254.9 ( the sum of each 07 )
[3] => 6521545 ( the sum )
[4] => 98474916521621 ( the sum )
) )
)
Array ( [1] =>
[0] => Array (
[08] => Array (
[2] => 305.7 ( not summed cause month 08 is only one )
[3] => 375857 ""
[4] => 1000000002 ""
) )
)
)
Any help?
Here, try this - I'm sure it's neither a perfect nor optimal solution ( 3 foreach-es ), but it works on a reasonably large data set...
$inputArray is the multidimensional array with the data you provided, btw...
EDIT: Fixed version:
$result = array();
foreach( $inputArray as $subArray ) {
foreach ( $subArray as $subKey => $member ) {
if ( empty( $result[$subKey]) ) {
$result[$subKey] = $member;
} else {
foreach ( $member as $id => $subMember ) {
if ( empty( $result[$subKey][$id]) ) {
$result[$subKey][$id] = $subMember;
} else {
$result[$subKey][$id] += $subMember;
}
}
}
}
}
EDIT2: Since you changed the format of arrays, the solution is different:
Note: $array1 and $array2 are your "global" - predefined arrays.
$arrayWrapper = array_merge( ( array ) $array1, ( array ) $array2 );
$result = array();
foreach ( $arrayWrapper as $inputArray ) {
foreach( $inputArray as $subArray ) {
foreach ( $subArray as $subKey => $member ) {
if ( empty( $result[$subKey]) ) {
$result[$subKey] = $member;
} else {
foreach ( $member as $id => $subMember ) {
if ( empty( $result[$subKey][$id]) ) {
$result[$subKey][$id] = $subMember;
} else {
$result[$subKey][$id] += $subMember;
}
}
}
}
}
}
Tested it with your data, should work.
Cheers.
I have a multidimensional array that could be any size or depth. I'm basically trying replace empty values with a value but only in certain cases. Here is an example of the array, it's quite large but I want to illustrate my point well:
[field_ter] =>
[field_title] => Array
(
[0] => Array
(
[value] =>
)
)
[field_firstnames] => Array
(
[0] => Array
(
[value] => test9
)
)
[field_birth] => Array
(
[0] => Array
(
[value] =>
)
)
[field_postal] => Array
(
[0] => Array
(
[value] =>
)
)
[group_certificates] => Array
(
[0] => Array
(
[_delta] => 0
[field_cert_details] => Array
(
[value] =>
)
[field_cert_issuedate] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_expiry] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_issue_country] => Array
(
[value] =>
)
[field_cert_limits] => Array
(
[value] =>
)
)
[1] => Array
(
[_delta] => 1
[field_cert_details] => Array
(
[value] =>
)
[field_cert_issuedate] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_expiry] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_issue_country] => Array
(
[value] =>
)
[field_cert_limits] => Array
(
[value] =>
)
)
)
What I'm trying to do is find any element in the array that is empty, then replace the empty value with a value. I have an array of exceptions where the empty element is not replaced. This is the function I'm working on at the moment:
function check_empty(&$array) {
$exceptions = array('changed', 'form_build_id','date', 'status', 'op');
// This is the array we will return at the end of the function
$new_array = array();
foreach($array as $key => $value) {
if(is_array($value)) {
check_empty($value);
}
elseif ($value == '' && !in_array($key, $exceptions)) {
$new_array[$key] = '$$$';
}
else {
$new_array[$key] = $value;
}
}
return $new_array;
}
Could someone help me tweak my function or point me in the direction of a better way? I tried array_walk_recursive but it doesn't work with my arrays.
You need to assign the return value of the recursive check_empty:
function check_empty($array) {
$exceptions = array('changed', 'form_build_id','date', 'status', 'op');
// This is the array we will return at the end of the function
$new_array = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$new_array[$key] = check_empty($value);
}
elseif ($value == '' && !in_array($key, $exceptions)) {
$new_array[$key] = '$$$';
}
else {
$new_array[$key] = $value;
}
}
return $new_array;
}
if(is_array($value)) {
check_empty($value);
}
Here you don't return resulting value to anywhere. Probably that's the problem