Array 1 output
Array (
[0] => Array ( [ID] => 335 [userid] => 4 [username] => demo [media_id] => 17 )
[1] => Array ( [ID] => 436 [userid] => 4 [username] => demo [media_id] => 18 )
[2] => Array ( [ID] => 637 [userid] => 4 [username] => demo [media_id] => 19 )
[3] => Array ( [ID] => 838 [userid] => 4 [username] => demo [media_id] => 20 )
);
Array 2 output
Array (
[1] => Array ( [ID] => 35 [userid] => 4 [media_id] => 17 )
[2] => Array ( [ID] => 36 [userid] => 4 [media_id] => 18 )
);
How to get other array value if match? I need if media_id and userid of array 2 match in array 1 then how to get perticuler ID and username from array 1 in foreach loop of array 2 ?
Update
$array1 = array (
0 => array ( "ID" => "335", "userid" => "4", "username" => "demo", "media_id" => "17" ),
1 => array ( "ID" => "436", "userid" => "4", "username" => "demo", "media_id" => "18" ),
2 => array ( "ID" => "637", "userid" => "4", "username" => "demo", "media_id" => "19" ),
3 => array ( "ID" => "838", "userid" => "4", "username" => "demo", "media_id" => "20" )
);
$array2 = array (
1 => array ( "ID" => "35", "userid" => "4", "media_id" => "17" ),
2 => array ( "ID" => "36", "userid" => "4", "media_id" => "18" )
);
foreach($array2 as $array) {
foreach($array1 as $get_data) {
if($array1['media_id'] == $get_data['media_id'] && $array1['userid'] == $get_data['userid']){
$get_result[] = //get_data true;
} else {
$get_result[] = //get_data false;
}
}
//get ID and username or show false value
}
Final answer
foreach($array2 as $array) {
$return = "false";
foreach($array1 as $get_data) {
if($array1['media_id'] == $get_data['media_id'] && $array1['userid'] == $get_data['userid']) {
$get_result[] = ['ID' => $get_data['ID'], 'username' => $get_data['username']];
$return = "true";
}
}
if($return = "false"){
echo false;
}
}
If I understood well your question (I'm not sure), this should work:
foreach($array2 as $array) {
foreach($array1 as $get_data) {
if($array1['media_id'] == $get_data['media_id'] && $array1['userid'] == $get_data['userid']) {
$get_result[] = ['ID' => $get_data['ID'], 'username' => $get_data['username']];
}
}
}
if (count($get_result) == 0) {
$get_result[] = false;
}
Not sure about your real needs, but #gmc gave you (part of ? all ?) what you need IMHO
<?php
$array1 = array (
0 => array ( "ID" => "335", "userid" => "4", "username" => "demo", "media_id" => "117" ),
1 => array ( "ID" => "436", "userid" => "4", "username" => "demo", "media_id" => "118" ),
2 => array ( "ID" => "637", "userid" => "4", "username" => "demo", "media_id" => "19" ),
3 => array ( "ID" => "838", "userid" => "4", "username" => "demo", "media_id" => "20" )
);
$array2 = array (
1 => array ( "ID" => "35", "userid" => "4", "media_id" => "17" ),
2 => array ( "ID" => "36", "userid" => "4", "media_id" => "18" )
);
$get_result = array();
foreach($array2 as $array) {
foreach($array1 as $get_data) {
if( ($array['media_id'] == $get_data['media_id']) && ($array['userid'] == $get_data['userid'])) {
$get_result[] = ['ID' => $get_data['ID'], 'username' => $get_data['username']];
}
}
}
if (count($get_result) == 0) {
echo"false";
}
else {
echo"true"; print_r($get_result); }
?>
/* I modified values to 117 & 118 -> returns "false" */
/* If you set values to 17 & 18 -> returns "true" */
Related
I'm retrieving some JSON that I am converting to an associative array. The issue that I am having is I am trying to get the email value from the user's who id matches the value that I have already set as a variable.
Here is what the array looks like
Array
(
[object] => list
[data] => Array
(
[0] => Array
(
[object] => pro
[id] => pro_77c9c6a85d814e059a6a2690989bae29
[first_name] => Jane
[last_name] => Doe
[full_name] => Jane Doe
[initials] => JD
[email] => admin#testorg.com
[mobile_number] => 9998761234
[messaging_uuid] => 4547c231c3e7d0ff1796f47b88f166d5
[color_hex] => EF9159
[avatar_url] => /assets/add_image.png
[avatar_thumb_url] =>
[has_avatar] =>
[organization_name] => testorg
[is_admin] => 1
[permissions] => Array
(
[show_company_setup] => 1
[can_see_home_data] => 1
[show_reporting] => 1
)
[is_super_pro] =>
[is_archived] =>
[impersonated] =>
)
[1] => Array
(
[object] => pro
[id] => pro_0fcb8e8610e54c518078db77ced7530e
[first_name] => Robert
[last_name] => Jordan
[full_name] => Robert Jordan
[initials] => RJ
[email] => rj#testorg.com
[mobile_number] => 4547457742
[messaging_uuid] => 0fcb8e8610e54c518078db77ced7530e
[color_hex] => EF9159
[avatar_url] => /assets/add_image.png
[avatar_thumb_url] =>
[has_avatar] =>
[organization_name] => testorg
[is_admin] => 1
[permissions] => Array
(
[show_company_setup] => 1
[can_see_home_data] => 1
[show_reporting] => 1
)
[is_super_pro] =>
[is_archived] =>
[impersonated] =>
)
)
[url] => /pros
)
Im basically trying to match the value that I have set in my pro_id variable
pro_0fcb8e8610e54c518078db77ced7530e
To the 'ID' Key in the array above and get the 'email' value associated to that same array and store for use later in my code.
Here is what I have so far, but no joy
foreach ($proObject as $key => $value) {
if ($key['id'] == $pro_id)
$techmail = $key['email'];
}
From my understanding of your question, and if you want to get only one element, you should be able to use array_search with array_column to get the index of the searched element. Using that you can then access the element and its corresponding email value. If you might expect more than one element I would use array_filter.
One Element:
In short:
$i = array_search($pro_id, array_column($proObject, 'id'));
$element = ($i !== false ? $proObject[$i] : null);
print_r($element["email"]);
Full code:
<?php
$proObject = array(
array(
"object" => "pro",
"id" => "pro_77c9c6a85d814e059a6a2690989bae29",
"first_name" => "Jane",
"last_name" => "Doe",
"full_name" => "Jane Doe",
"initials" => "JD",
"email" => "admin#testorg.com",
"mobile_number" => "9998761234",
"messaging_uuid" => "4547c231c3e7d0ff1796f47b88f166d5",
"color_hex" => "EF9159",
"avatar_url" => "/assets/add_image.png",
"avatar_thumb_url" => "",
"has_avatar" => "",
"organization_name" => "testorg",
"is_admin" => "1",
"permissions" => array(
"show_company_setup" => "1",
"can_see_home_data" => "1",
"show_reporting" => "1",
) ,
"is_super_pro" => "",
"is_archived" => "",
"impersonated" => "",
) ,
array(
"object" => "pro",
"id" => "pro_0fcb8e8610e54c518078db77ced7530e",
"first_name" => "Robert",
"last_name" => "Jordan",
"full_name" => "Robert Jordan",
"initials" => "RJ",
"email" => "rj#testorg.com",
"mobile_number" => "4547457742",
"messaging_uuid" => "0fcb8e8610e54c518078db77ced7530e",
"color_hex" => "EF9159",
"avatar_url" => "/assets/add_image.png",
"avatar_thumb_url" => "",
"has_avatar" => "",
"organization_name" => "testorg",
"is_admin" => "1",
"permissions" => array(
"show_company_setup" => "1",
"can_see_home_data" => "1",
"show_reporting" => "1",
) ,
"is_super_pro" => "",
"is_archived" => "",
"impersonated" => "",
)
);
$pro_id = "pro_0fcb8e8610e54c518078db77ced7530e";
$i = array_search($pro_id, array_column($proObject, 'id'));
$element = ($i !== false ? $proObject[$i] : null);
print_r($element["email"]);
?>
Multiple Elements:
In short:
class idEqualsFilter {
private $id;
public function __construct($id) {
$this->id = $id;
}
function __invoke($i) {
return $this->id === $i["id"];
}
};
$elements = array_filter($proObject, new idEqualsFilter($pro_id));
print_r(array_column($elements, "email"));
Full code:
<?php
$proObject = array(
array(
"object" => "pro",
"id" => "pro_77c9c6a85d814e059a6a2690989bae29",
"first_name" => "Jane",
"last_name" => "Doe",
"full_name" => "Jane Doe",
"initials" => "JD",
"email" => "admin#testorg.com",
"mobile_number" => "9998761234",
"messaging_uuid" => "4547c231c3e7d0ff1796f47b88f166d5",
"color_hex" => "EF9159",
"avatar_url" => "/assets/add_image.png",
"avatar_thumb_url" => "",
"has_avatar" => "",
"organization_name" => "testorg",
"is_admin" => "1",
"permissions" => array
(
"show_company_setup" => "1",
"can_see_home_data" => "1",
"show_reporting" => "1",
),
"is_super_pro" => "",
"is_archived" => "",
"impersonated" => "",
),
array(
"object" => "pro",
"id" => "pro_0fcb8e8610e54c518078db77ced7530e",
"first_name" => "Robert",
"last_name" => "Jordan",
"full_name" => "Robert Jordan",
"initials" => "RJ",
"email" => "rj#testorg.com",
"mobile_number" => "4547457742",
"messaging_uuid" => "0fcb8e8610e54c518078db77ced7530e",
"color_hex" => "EF9159",
"avatar_url" => "/assets/add_image.png",
"avatar_thumb_url" => "",
"has_avatar" => "",
"organization_name" => "testorg",
"is_admin" => "1",
"permissions" => array
(
"show_company_setup" => "1",
"can_see_home_data" => "1",
"show_reporting" => "1",
),
"is_super_pro" => "",
"is_archived" => "",
"impersonated" => "",
)
);
$pro_id = "pro_0fcb8e8610e54c518078db77ced7530e";
class idEqualsFilter {
private $id;
public function __construct($id) {
$this->id = $id;
}
function __invoke($i) {
return $this->id === $i["id"];
}
};
$elements = array_filter($proObject, new idEqualsFilter($pro_id));
print_r(array_column($elements, "email"));
?>
I have the data :
Array
(
[0] => Array
(
[id] => 12
[code] => 12345
[name] => Aaron
)
[1] => Array
(
[id] => 5
[code] => 16784
[name] => Bryan
)
[2] => Array
(
[id] => 35
[code] => 32467
[name] => Charlie
)
[3] => Array
(
[id] => 25
[code] => 44513
[name] => Denise
)
[4] => Array
(
[id] => 44
[code] => 15774
[name] => Michael
)
)
In my model i create function :
private function getPosition($field_search, $field_value){ // ID or code
$this->db->select('*');
$this->db->from($this->table);
$this->db->order_by('name','asc');
$result = $this->db->get()->result();
$pos = array_search($field_value, array_column($result, $field_search));
return ($pos !==false ? $pos : -1);
}
if i want to get row index of "32467", i just call
$pos = $this->getPosition('code', '32467');
then i got the row index is "2",
but i wonder, how to get row index if the query is :
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('code', '32467');
$this->db->order_by('name','asc');
$result = $this->db->get()->result();
litte correction in my question,
i mean, if we index in database where code = "32467", we've got index is "2", how to get return if index is "2"
thanks for your help...
<?php
$userdb= Array
(
"0" => Array(
'id' => "12",
'code' => "12345",
'name' => 'Aaron'
),
"1" => Array
(
"id" => "5",
"code" => "16784",
"name" => "Bryan"
),
"2" => Array
(
"id" => "35",
"code" => "32467",
"name" => "Charlie"
),
"3" => Array
(
"id" => "25",
"code" => "44513",
"name" => "Denise"
),
"4" => Array
(
"id" => "44",
"code" => "15774",
"name" => "Michael"
),
);
$key = array_search(32467, array_column($userdb, 'code'));
echo ("The key is: ".$key);
private function getPosition($field_search, $field_value){ // ID or code
$this->db->select('*');
$this->db->from($this->table);
$this->db->order_by('name','asc');
$result = $this->db->get()->result();
$key = array_search($field_value, array_column($result, $field_search));
return $key;
}
?>
UPDATE var_export version of the array HERE
I have the following array which I would like to group the elements/children by their UNILEVEL value:
array(
0 => array(
"member_id" => 3,
"unilevel" => 1,
"children" => array(
0 => array(
"member_id" => 4,
"unilevel" => 2,
"children" => array(
0 => array(
"member_id" => 6,
"unilevel" => 3,
"children" => array(
0 => array(
"member_id" => 7,
"unilevel" => 4 ) ) ) ) )
1 => array(
"member_id" => 9
"unilevel" => 2 ) ) )
1 => array(
"member_id" => 5,
"unilevel" => 1,
"children" => array(
0 => array(
"member_id" => 8,
"unilevel" => 2,
"children" => array(
0 => array(
"member_id" => 10,
"unilevel" => 3 ) ) ) ) ) )
The Controller below has a function named, groupPerlevel which does the grouping, but it only groups the first parent right now, it's already a recursive function, I am not sure why it's not putting the second parent's elements on the unilevel groupings.
class TestController extends Controller {
private $group = array();
private function setGroup($value) {
$this->group = $this->group + $value;
}
private function getGroup() {
return $this->group;
}
public function create()
{
$this->groupPerlevel($tree);
dd($this->getGroup());
}
private function groupPerlevel(array $items)
{
var_dump($items);
$grouparr = $this->getGroup();
$newkey = 0;
$templevel = 1;
foreach($items as $key => $val) {
if($templevel == $val->unilevel) {
$grouparr[$templevel][$newkey] = $val;
$this->setGroup($grouparr);
} else {
if(isset($grouparr[$val->unilevel][$newkey])) {
$count = count($grouparr[$val->unilevel]);
$grouparr[$val->unilevel][$count] = $val;
$this->setGroup($grouparr);
} else {
$grouparr[$val->unilevel][$newkey] = $val;
}
}
if(isset($val->children)) {
$children = $val->children;
unset($val->children);
$this->groupPerlevel($children);
}
$newkey++;
}
$this->setGroup($grouparr);
}
}
The following array would be my desired output. array(4) { [1] is the UNILEVEL (group) so all elements/children should be in their proper unilevel group based on their unilevel field value. But it only does that for the first parent, the second parent doesn't group.
array(
1 => array(
0 => array(
"member_id" => 3,
"unilevel" => 1 ) )
4 => array(
0 => array(
"member_id" => 7,
"unilevel" => 4 ) )
3 => array(
0 => array (
"member_id" => 6,
"unilevel" => 3 ) )
2 => array(
0 => array(
"member_id" => 4,
"unilevel" => 2
1 => array(
"member_id" => 9
"unilevel" => 2 )))
Sorry for late response try this
$arr=array("shiva" => array("member_id" => "3","unilevel" => "1","children" => array("0" => array("member_id" => "4","unilevel" => "2","children" => array("0" => array("member_id" => "6", "unilevel" => "3", "children" => array( "0" => array( "member_id" => "7", "unilevel" => "4" ) ) ) ) ), "1" => array( "member_id" => "9", "unilevel" => "2" ))),"1" => array("member_id" => "5","unilevel" => "1","children" => array("0" => array("member_id" => "8","unilevel" => "2", "children" => array( "0" => array( "member_id" => "10", "unilevel" => "3" ) ) ) ) ) );
function check($arr){
if(is_array($arr)){
foreach($arr as $arr1){
foreach($arr1 as $arr2){
if(is_array($arr2)){
return true;
}
}
}
}
else{
return false;
}
return false;
}
$i=0;
while(check($arr)){
foreach($arr as $arr1key=>$arr1val){
foreach($arr1val as $arr2key=>$arr2val){
if(is_array($arr2val)){
$arr[]=$arr[$arr1key][$arr2key];
unset($arr[$arr1key][$arr2key]);
}
}
}
}
foreach($arr as $arr1key=>$arr1val)
{
if(count($arr1val)==0){
unset($arr[$arr1key]);
}
}
//print_r($arr);
$result=array();
$values=array();
foreach($arr as $arr1key=>$arr1val)
{
if(((array_key_exists("unilevel", $arr1val)) || (array_key_exists("member_id", $arr1val)))==true)
{
$key=$arr1val['unilevel'];
$result[0][$key][]=array("unilevel"=>$arr1val["unilevel"],"member_id"=>$arr1val["member_id"]);
}
}
print_r($result);
Result:
Array ( [0] => Array ( [1] => Array ( [0] => Array ( [unilevel] => 1 [member_id] => 3 ) [1] => Array ( [unilevel] => 1 [member_id] => 5 ) ) [2] => Array ( [0] => Array ( [unilevel] => 2 [member_id] => 4 ) [1] => Array ( [unilevel] => 2 [member_id] => 9 ) [2] => Array ( [unilevel] => 2 [member_id] => 8 ) ) [3] => Array ( [0] => Array ( [unilevel] => 3 [member_id] => 6 ) [1] => Array ( [unilevel] => 3 [member_id] => 10 ) ) [4] => Array ( [0] => Array ( [unilevel] => 4 [member_id] => 7 ) ) ) )
How can I loop through a multi-layer array and replace some associate values in it?
For instance, this is my array,
$items = array(
0 => array(
"id" => "1",
"title" => "parent 1",
"children" => array()
),
1 => array(
"id" => "2",
"title" => "parent 2",
"children" => array (
0 => array(
"id" => "4",
"title" => "children 1",
"granchildren" => array(
0 => array(
"id" => "7",
"title" => "granchildren 1"
),
1 => array(
"id" => "8",
"title" => "granchildren 2"
)
)
),
1 => array(
"id" => "5",
"title" => "children 2",
"granchildren" => array()
)
),
),
3 => array(
"id" => "3",
"title" => "parent 3",
"children" => array()
)
);
These are two working functions I have,
function translate ($id){
$items = array(
0 => array(
"id" => 1,
"title" => "parent 1 en"
),
1 => array(
"id" => 4,
"title" => "children 1 en"
),
2 => array(
"id" => 8,
"title" => "granchildren 2 en"
)
);
foreach($items as $item) {
if($id === $item['id'])
{
return $item['title'];
}
}
}
function looper ($items){
$new_items = array();
foreach($items as $key => $item) {
if(isset($key) && is_array($key)){
$new_items[$key] = translate($item['id']);
}else {
//looper($item);
}
}
return $new_items;
}
print_r(looper ($items));
This is the result I am after,
Array
(
[0] => Array
(
[id] => 1
[title] => parent 1 en // translated
[children] => Array
(
)
)
[1] => Array
(
[id] => 2
[title] => parent 2
[children] => Array
(
[0] => Array
(
[id] => 4
[title] => children 1 en // translated
[granchildren] => Array
(
[0] => Array
(
[id] => 7
[title] => granchildren 1
)
[1] => Array
(
[id] => 8
[title] => granchildren 2 en // translated
)
)
)
[1] => Array
(
[id] => 5
[title] => children 2
[granchildren] => Array
(
)
)
)
)
[3] => Array
(
[id] => 3
[title] => parent 3
[children] => Array
(
)
)
)
Is it possible?
Sounds like a job for array_walk or array_walk_recursive.
It will call a user-supplied function for every item in an array. You can have it modify the array by reference to achieve what you're after.
I have this array
Array (
[0] => Array
(
[0] => stdClass Object
(
[id] => 226
[user_id] => 1
[name] => Eden Corner Tub by Glass - $2099
)
[1] => stdClass Object
(
[id] => 225
[user_id] => 1
[name] => Blue Quilted Leather Jacket by Minusey - $499
)
[2] => stdClass Object
(
[id] => 222
[user_id] => 1
[name] => Darling New Bathtub by Duravit - $6300
)
)
[1] => Array
(
[0] => stdClass Object
(
[id] => 226
[user_id] => 1
[name] => Eden Corner Tub by Glass - $2099
)
[1] => stdClass Object
(
[id] => 229
[user_id] => 1
[name] => Batman Tumbler Golf Cart - $50000
)
[2] => stdClass Object
(
[id] => 228
[user_id] => 1
[name] => Swirlio Frozen Fruit Dessert Maker - $60
)
) )
I have an array of products that I need to make sure are unique.
Need to make this array unique by id. These array are generated by pushing value.
I'm trying to solve this for more than a week now, but I dont get it to work. I know it should be easy...but anyway - I don't get it :D
Try this:
$array = array(
0 => array(
"name" => "test",
"id" => 4
),
1 => array(
"name" => "test2",
"id" => 152
),
2 => array(
"name" => "test2",
"id" => 152
)
);
$newArray = array();
foreach($array as $value) {
$newArray[$value['id']]['name'] = $value['name'];
$newArray[$value['id']]['id'] = $value['id'];
}
foreach($array as $key=>$inner_array)
{
foreach($array as $key_again=>$array_again)
{
if($key != $key_again)
{
if($inner_array['name'] != $array_again['name'] AND $inner_array['id'] != $array_again['id'])
{
//its okay
}
else
{
unset($array[$key]);
}
}
}
}
Not sure how the arrays are generated, but, if you can change that, you could set the array keys to the IDs directly and check if the id is already set.
Otherwise, you can do the following:
$unique = array();
foreach( $array as $values ) {
if( ! isset( $unique[$values['id']] ) ) {
$unique[$values['id']] = $values;
}
}
This will make your array unique:
$array = array(
0 => array(
"name" => "test",
"id" => 4
),
1 => array(
"name" => "test2",
"id" => 152
),
2 => array(
"name" => "test2",
"id" => 152
) );
$result = array();
$index = array();
foreach($array as $i => $elem) {
if(!isset($index[$elem['id']])) {
$result[$i] = $elem;
$index[$elem['id']] = 1;
}
}
echo var_export($result);
Output:
array (
0 =>
array (
'name' => 'test',
'id' => 4,
),
1 =>
array (
'name' => 'test2',
'id' => 152,
),
)
This will work. It could be considered more clean than a for loop, but I'm not sure about performance.
$array = [
[ "name" => "test", "id" => 4 ],
[ "name" => "test2", "id" => 152 ],
[ "name" => "test2", "id" => 152 ]
];
array_walk($array, function(&$item, $idx) use($array){
$matches = array_slice(array_keys($array, $item), 1);
if (in_array($idx, $matches)) {
$item = false;
}
});
$array = array_filter($array);
Edit Since you updated the data set to work with, you would need to flatten it 1 level:
$array = call_user_func_array('array_merge', $array);