PHP object key value comparison of array indexes - php

I have two arrays where indexes of both arrays are contains objects
$array1=array(1) {
[0]=> object(stdClass) (3) {
["aid"]=> string(1) "1"
["a_number"]=> string(1) "0"
["id_of"]=> string(1) "1"
}
}
$array2=array(3) {
[0]=> object(stdClass) (3) {
["id"]=> string(1) "1",
["number"]=> string(1) "0" ,
["flag"]=> string(1) "1" ,
["zflag"]=> string(1) "0" ,
["xflag"]=> string(1) "1"
} ,
[1]=> object(stdClass) (3) {
["id"]=> string(1) "2",
["number"]=> string(1) "2" ,
["flag"]=> string(1) "2" ,
["zflag"]=> string(1) "0" ,
["xflag"]=> string(1) "1"
},
[1]=> object(stdClass) (3) {
["id"]=> string(1) "3",
["number"]=> string(1) "3",
["flag"]=> string(1) "3" ,
["zflag"]=> string(1) "0" ,
["xflag"]=> string(1) "1"
}
}
I want to compare between the value of $id key in all elements of $array2 with the value of $id_of each element of $array1, if it's not exist then return the element of $array1. Below is my code but it doesn't work
public function unanswered($array1,$array2){
if(!(empty($array2))){
$unanswered_arrays=array();
foreach($array1 as $b){
foreach($array2 as $a){
if($b->id != $a->id_of){
array_push($unanswered_arrays,(object)$b);
}
}
}
return $unanswered_arrays;
}
return $array1;
}

If you're using your function as unanswered($array1,$array2) then replace $array1 with $array2 and vice versa in foreach loops or pass unanswered($array2,$array1) instead of.
Demo

Related

PHP - get main array key search by value into two-level multidimensional array [duplicate]

This question already has answers here:
PHP multidimensional array search by value
(23 answers)
Closed 1 year ago.
I have an $array with build:
array(5) {
[0]=>
array(5) {
["woj"]=>
string(4) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(10) "Test1"
}
[1]=>
array(5) {
["woj"]=>
string(1) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(8) "test2"
}
[2]=>
array(5) {
["woj"]=>
string(1) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(12) "test3"
}
[3]=>
array(5) {
["woj"]=>
string(1) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(8) "Test4"
}
[4]=>
array(5) {
["woj"]=>
string(1) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(11) "Test5"
}
}
and I want to create a php function, which can be search main array key by value (name).
Example - search main array key by name: test3. Result should be 2, because second main key of $array contain subarray where name key = test3.
I tied with array_search, but with multidimensional array, this function not work.
Thanks for your help.
You may try the following for your multi-dimensional array:
function myCustomSearch($data,$searchVal){
foreach($data as $index => $subArray){
foreach($subArray as $key=>$value){
if($value === $searchVal)return $index;
}
}
return null; //return null if not found
}
eg use assuming your current array shared in question is stored in $data
$resultIndex = myCustomSearch($data,'test3');
Let me know if this works for you.

Combine Similar Strings Value into Unique One

I'm fetching a post meta from Wordpress CPT which's returning multiple strings form individually instead of array as shown below:
$data = $calendar->data();
foreach ($data as $event) {
$days = get_post_meta( $event->ID, 'sp_day', true );
var_dump($days);
}
string(1) "1" string(1) "1" string(1) "1" string(1) "1" string(1) "1"
string(1) "1" string(1) "2" string(1) "2" string(1) "2" string(1) "2"
string(1) "2" string(1) "2" string(1) "3" string(1) "3" string(1) "3"
string(1) "3" string(1) "3" string(1) "3" string(1) "4" string(1) "4"
string(1) "4" string(1) "4" string(1) "4" string(1) "4" string(0) ""
string(0) "" string(0) "" string(0) "" string(0) "" string(0) ""
I tried to convert it into array like $days = array(get_post_meta( $event->ID, 'sp_day', true )); the dump values were:
array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" }
array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" }
array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" }
array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" }
array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" }
array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" }
array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" }
array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" }
array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" }
Than I tried the array_merge() and array_unique()
None of the above steps have solved the issue. As you can see we have repeated values multiple times instead I want them unique to get each value only once such "1, 2, 3, 4, 5, etc."
Suggestions are highly appreciated
you can add an empty array before the for loop and inside the for loop you can check if the item exist in the array, if it doesn't exist you add the item to the array.
$data = $calendar->data();
$result = [];
foreach ($data as $event) {
$days = get_post_meta( $event->ID, 'sp_day', true );
if(! in_array($days, $result)) {
$result[] = $days;
}
}
var_dump($result);

I have 2d array, and i want to swap the value of the last index of each array

i want the last index value of each array for example 8 and 10 will be swap
array(2) {
[0]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "5"
[3]=>
string(1) "8"
}
[1]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "6"
[3]=>
string(2) "10"
}
}
so the result will be
array(2) {
[0]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "5"
[3]=>
string(2) "10"
}
[1]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "6"
[3]=>
string(1) "8"
}
}
if there are two value or more (even, not odd) will be swap its okay, as long as not all of the value will be swap. the size of array will be dynamic, thats just for example.
it swaps the last value:
$arr1 = array(1, 3, 5, 8);
$arr2 = array(1, 3, 6, 10);
$val1 = end($arr1);
$val2 = end($arr2);
array_pop($arr1);
array_pop($arr2);
array_push($arr1, $val2);
array_push($arr2, $val1);
print_r($arr1);
print_r($arr2);

making new arrays in 1 multidimensional array on looping statement codeigniter

guys lets play in multidimensional array little bit :p
lets said i have $filter which it contain multidimensional array :
array(2) {
["time"]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
}
["people"]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(1) "4"
[4]=>
string(1) "5"
}
}
from the array it should be return :
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "1"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "2"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "3"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "4"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "5"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "1"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "2"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "3"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "4"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "5"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "1"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "2"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "3"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "4"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "5"
["promo_id"]=>
string(1) "1"
}
actually i could get that result with this following code :
foreach ($filter['time'] as $row){
foreach ($filter['people'] as $item){
$new_array['time'] = $row;
$new_array['people'] = $item;
$new_array['promo_id'] = $promo_id;
vd($new_array,"new");
}
}
but i cant use that following code, because it using $filter['time'] and $filter['people'], how about if the time or people is a random string?
so can you show me guys, to looping it from $filter?
as like this :
foreach ($filter as $key => $row){
}
thank you (:
function combination($arr,$res,$completed_key){
$array_key = array_keys($arr);
$last_array_key = array_pop($array_key);
$start = false;
$temp = $res;
$res=array();
$i=0;
foreach($arr as $key=>$row){
if(!in_array($key,$completed_key)&& $start==false){
array_push($completed_key,$key);
$start=true;
}
if($start){
if(count($temp)==0){
foreach($row as $key2 => $row2){
$res[][$key]=$row2;
}
}else{
foreach($temp as $temp_key=>$temp_row){
foreach($row as $key2 => $row2){
$res[$i]=$temp_row;
$res[$i][$key]=$row2;
$i++;
}
}
}
}
if($start){
break;
}
}
if(!in_array($last_array_key,$completed_key)){
return $this->combination($arr,$res,$completed_key,$debugger);
}else{
return $res;
}
}
This function will return you want.
$filter["time"]=array(1,2,3);
$filter["people"]=array(1,2);
$filter["size"]=array("a","b");
$res=$this->combination($filter,array(),array());
I'll explain later.

combine some data in 1 arrray if in same id on multidimesensional array

I have a multidimensional array $voucher_data with this following array :
array(6) {
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
string(1) "2"
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
string(1) "6"
["voucher_menu_id"]=>
string(1) "3"
}
[2]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "1"
["variant_menu_id"]=>
string(1) "1"
["voucher_menu_id"]=>
string(1) "4"
}
[3]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "1"
["variant_menu_id"]=>
string(1) "6"
["voucher_menu_id"]=>
string(1) "4"
}
[4]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "519"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
NULL
["voucher_menu_id"]=>
string(1) "1"
}
[5]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "525"
["qty"]=>
string(1) "3"
["variant_menu_id"]=>
NULL
["voucher_menu_id"]=>
string(1) "2"
}
}
In my array [0] and [1] the voucher_menu_id is 3. They have same voucher_menu_id and also [2] and [3] is 4
So i want to combine the variant_menu_id become 1 array become like this :
array(4) {
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
array(2){
string(1) "2"
string(1) "6"
}
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "1"
["variant_menu_id"]=>
array(2){
string(1) "1"
string(1) "4"
}
["voucher_menu_id"]=>
string(1) "4"
}
}
[2]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "519"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
NULL
["voucher_menu_id"]=>
string(1) "1"
}
[3]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "525"
["qty"]=>
string(1) "3"
["variant_menu_id"]=>
NULL
["voucher_menu_id"]=>
string(1) "2"
}
}
Guys can you help me how to combine it?
Thank you (:
Just like the comment above, just use your voucher_menu_id as your key into assigning the new container. Use that so that it'll be group when you loop:
$new_array = array();
foreach ($voucher_data as $v) {
if(empty($new_array[$v['voucher_menu_id']])) { // if empty initialize
$new_array[$v['voucher_menu_id']] = $v; // base array group
$new_array[$v['voucher_menu_id']]['variant_menu_id'] = array(); // intialize container
}
// push id
$new_array[$v['voucher_menu_id']]['variant_menu_id'][] = $v['variant_menu_id'];
}
$new_array = array_values($new_array); // array reindex
Untested, but something like this might work:
$output = array();
foreach ($array as $entry) {
// make the variant_menu_id an array
$entry['variant_menu_id'] = (array) $entry['variant_menu_id'];
$key = $entry['voucher_menu_id'];
if (!array_key_exists($key, $output)) {
// create a new entry in the output array using the entire $entry
$output[$key] = $entry;
continue;
}
// otherwise, add your variant menu ID to the existing values
$output[$key]['variant_menu_id'] = array_merge($output[$key]['variant_menu_id'], $entry['variant_menu_id']);
}
This will essentially create a new array by grouping entries by their voucher_menu_id, and adding variant_menu_id values into an array in the $output.

Categories