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"));
?>
Related
First time posting here and really need some help. Relatively new to php I have an array that I'd like to restructure. I have subjects at top level with students in a sub array. I'd like to restructure so that Students are at the top level with subjects in a sub array.
$startingArray = [
[ "subject" => "Physics",
"student" => [
["id" => "00003", "Name" => "Peter", "email" => "peter#schooool.com",],
["id" => "00004", "Name" => "Mary", "email" => "mary#schooool.com",],
["id" => "00005", "Name" => "Jane", "email" => "jane#schooool.com",],
]
],
[ "subject" => "Chemistry",
"student" => [
["id" => "00003", "Name" => "Peter", "email" => "peter#schooool.com",],
["id" => "00004", "Name" => "Mary", "email" => "mary#schooool.com",],
["id" => "00005", "Name" => "Jane", "email" => "jane#schooool.com",],
]
],
[ "subject" => "Mathematics",
"student" => [
["id" => "00003", "Name" => "Peter", "email" => "peter#schooool.com",],
["id" => "00006", "Name" => "Fred", "email" => "fred#schooool.com",],
["id" => "00007", "Name" => "Wilma", "email" => "wilma#schooool.com",],
]
],
[ "subject" => "Biology",
"student" => [
["id" => "00004", "Name" => "Mary", "email" => "mary#schooool.com",],
["id" => "00006", "Name" => "Fred", "email" => "fred#schooool.com",],
["id" => "00008", "Name" => "Alison", "email" => "alison#schooool.com",],
]
]
];
I want my new array to be like this;
$endingArray = [
"students" => [
[ "id" => "00003",
"Name" => "Peter",
"email" => "peter#schooool.com",
"subjects" => [ "Physics", "Chemistry", "Mathematics", ],
],
[ "id" => "00004",
"Name" => "Mary",
"email" => "mary#schooool.com",
"subjects" => [ "Physics", "Chemistry", "Biology", ],
],
[ "id" => "00005",
"Name" => "Jane",
"email" => "jane#schooool.com",
"subjects" => [ "Physics", "Chemistry", ],
],
[ "id" => "00006",
"Name" => "Fred",
"email" => "fred#schooool.com",
"subjects" => [ "Mathematics", "Biology", ],
],
[ "id" => "00007",
"Name" => "Wilma",
"email" => "wilma#schooool.com",
"subjects" => [ "Mathematics", "Biology", ],
],
[ "id" => "00008",
"Name" => "Alison",
"email" => "alison#schooool.com",
"subjects" => [ "Physics", "Biology", ],
],
],
];
I'm trying a 2 stage process. First is to create the new students array without the subjects. This works fine.
Then I'm trying to append the subjects but I know my logic is wrong and the code I have just creates a new row for each student and each subject.
foreach ( $startingArray as $row ) {
$students = $row['student'];
foreach ($students as $student) {
if (!in_array($student, $studentsArray)) {
$studentsArray[] = $student;
}
}
}
the second step just produces garbage
$s1 = $student['id'];
foreach ($startingArray as $row) {
$subject = $row['subject'];
$students = $row['student'];
foreach ($students as $s2) {
if ($s2['id]'] = $s1) {
$student['subjects'] = $subject;
array_push($studentsArray, $student);
}
}
}
}
Any help appreciated!
Here what you can do:
$newArray = [];
foreach ($startingArray as $item) {
foreach ($item['student'] as $student) {
if (empty($newArray[$student['id']])) {
$student['subjects'] = [];
$newArray[$student['id']] = $student;
}
$newArray[$student['id']]['subjects'][] = $item['subject'];
}
}
Sample fiddle here.
Case you need to maintain a sequential id like your example do this
$newArray = [];
foreach ($startingArray as $item) {
foreach ($item["student"] as $student) {
$filter = array_filter($newArray, function ($d) use ($student) {
return ($d["id"] == $student["id"]);
});
if (!$filter) {
$student["subjects"][] = $item['subject'];
$newArray[] = $student;
} else {
$newArray[key($filter)]["subjects"][] = $item["subject"];
}
}
}
$finalArray = [
'students' => $newArray
];
The final result will be
Array
(
[students] => Array
(
[0] => Array
(
[id] => 00003
[Name] => Peter
[email] => peter#schooool.com
[subjects] => Array
(
[0] => Physics
[1] => Chemistry
[2] => Mathematics
)
)
[1] => Array
(
[id] => 00004
[Name] => Mary
[email] => mary#schooool.com
[subjects] => Array
(
[0] => Physics
[1] => Chemistry
[2] => Biology
)
)
[2] => Array
(
[id] => 00005
[Name] => Jane
[email] => jane#schooool.com
[subjects] => Array
(
[0] => Physics
[1] => Chemistry
)
)
[3] => Array
(
[id] => 00006
[Name] => Fred
[email] => fred#schooool.com
[subjects] => Array
(
[0] => Mathematics
[1] => Biology
)
)
[4] => Array
(
[id] => 00007
[Name] => Wilma
[email] => wilma#schooool.com
[subjects] => Array
(
[0] => Mathematics
)
)
[5] => Array
(
[id] => 00008
[Name] => Alison
[email] => alison#schooool.com
[subjects] => Array
(
[0] => Biology
)
)
)
)
I have associative array like -
[0] => Array
(
[date] => 2018-06-22
[id] => 2282991
[type] => VIDEO
[domain] =>
[code] => Austin
[address] => Phone
)
[1] => Array
(
[date] => 2018-06-22
[id] => 2282991
[type] => VIDEO
[domain] =>
[code] =>
[address] => Phone
)
[3] => Array
(
[date] => 2018-06-22
[id] => 2282991
[type] => VIDEO
[domain] =>
[code] => Austin
[address] => Phone
)
I need to check is there any column having all the values are blank.
That means it should return only domain from above array because it is blank everywhere.
Is there any way to do this with minimum use of forloop? I need to check this for all these columns.
This will work if your subarray having same number of keys.Like "Date, id, Type etc".
$array = [
[ "date" => "2018-06-22", "id" => 2282991, "type" => "VIDEO", "domain" =>'', "code" => "Austin", "address" => "Phone"],
[ "date" => "2018-06-22", "id" => 2282991, "type" => "VIDEO", "domain" =>'', "code" => "", "address" => "Phone"],
[ "date" => "2018-06-22", "id" => 2282991, "type" => "VIDEO", "domain" =>'', "code" => "Austin", "address" => "Phone"]
];
$empty = [];
foreach($array[0] as $key=>$val){
$error = array_column($array, $key);
if(empty (array_filter($error)) ) {
$empty[] = $key;
}
}
print_r($empty);
Output:
Array
(
[0] => domain
)
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" */
I'm facing a little problem and I don't know is it's possible to achieve it or not. Let me explain :
I have an associative array like this :
Array
(
[res_id] => 104
[subject] => Test
[type_id] => 503
[format] => pdf
[typist] => fefe
[creation_date] => 2017-02-10 14:27:37.236711
[modification_date] => 2017-02-10 14:27:37.236711
[fulltext_result] => 1
[doc_date] => 2017-02-01 00:00:00
[docserver_id] => FASTHD_MAN
[path] => 2017#02#0001##
[filename] => 0008.pdf
[fingerprint] =>
[filesize] => 84979
[status] => VAL
[destination] => DSG
[priority] => 2
[is_multi_docservers] => N
[is_frozen] => N
[tablename] => res_letterbox
[initiator] => COU
[dest_user] => ddaull
[locker_user_id] => fefefef
[locker_time] => 2017-02-13 15:52:25.624521
[confidentiality] => N
[tnl_path] => 2017#02#0001##
[tnl_filename] => 0008.png
)
I want to know if I can use this associative array, in order to make an INSERT TO request ? I want the first part of the array (like res_id, subject) goes to column part for the insertion. The second part of the array (like 104,Test) will go to the values
Thanks in advance for your help, hope I am clear enough..
#Nathan30 try this :
<?php
$arr = array(
"res_id" => 104,
"subject" => "Test",
"type_id" => 503,
"format" => "pdf",
"typist" => "fefe",
"creation_date" => "2017-02-10 14:27:37.236711",
"modification_date" => "2017-02-10 14:27:37.236711",
"fulltext_result" => 1,
"doc_date" => "2017-02-01 00:00:00",
"docserver_id" => "FASTHD_MAN",
"path" => "2017#02#0001##",
"filename" =>" 0008.pdf",
"fingerprint" => "",
"filesize" => 84979,
"status" => "VAL",
"destination" => "DSG",
"priority" => 2,
"is_multi_docservers" => "N",
"is_frozen" => "N",
"tablename" => "res_letterbox",
"initiator" => "COU",
"dest_user" => "ddaull",
"locker_user_id" => "fefefef",
"locker_time" => "2017-02-13 15:52:25.624521",
"confidentiality" => "N",
"tnl_path" => "2017#02#0001##",
"tnl_filename" => "0008.png",
);
echo "<pre>";
print_r($arr);
$column = array();
$values = array();
foreach($arr as $key => $value){
$column[] = $key;
$values[] = $value;
}
now query will be like:
"insert into table values(".implode(',', $column).") values (".implode(',', $values).")";
i am facing problem in storing work and education information of Facebook user.
this is how i am getting info of user:
$profile_request=$fb->get('/me?fields=name,first_name,last_name,email,work,education');
and then convert the response in to multidimensional array:
$profile = $profile_request->getGraphNode()->asArray();
this is how i get the response:
$profile=Array
(
"name" => "abc pqr",
"first_name" => "abc",
"last_name" => "pqr",
"email" => "abc#gmail.com",
"work" => Array
(
"0" => Array
(
"employer" => Array
(
"id" => "348468651959125",
"name" => "Karachi university"
),
"location" => Array
(
"id" => "110713778953693",
"name" => "Karachi, Pakistan"
),
"position" => Array
(
"id" => "1556082398000870",
"name" => "Lecturer"
),
"start_date" => "2008-04-25"
),
"1" => Array
(
"description" => "i am teaching......",
"end_date" => "2011-06-20",
"employer" => Array
(
"id" => "486743441453589",
"name" => "Happy Palace Grammar School NORTH 3 - J2"
),
"location" => Array
(
"id" => "110713778953693",
"name" => "Karachi, Pakistan"
),
"position" => Array
(
"id" => "1556082398000870",
"name" => "Lecturer"
),
"start_date" => "2005-06-17"
)
),
"education" => Array
(
"0" => Array
(
"school" => Array
(
"id" => "193563427344550",
"name" => "Mehran Model Sec School"
),
"type" => "High School"
),
"1" => Array
(
"concentration" => Array
(
"0" => Array
(
"id" => "104076956295773",
"name" => "Computer Science"
)
),
"school" => Array
(
"id" => "113340788773685",
"name" => "Sir Adamjee Institute"
),
"type" => "College",
"year" => Array
(
"id" => "118118634930920",
"name" => "2012"
)
),
"2" => Array
(
"concentration" => Array
(
"0" => Array
(
"id" => "104076956295773",
"name" => "Computer Science"
)
),
"degree" => Array
(
"id" => "519451718218292",
"name" => "Bachelor of science in Computer Science in Computer Science"
),
"school" => Array
(
"id" => "107345492681211",
"name" => "SZABIST"
),
"type" => "Graduate School",
"year" => Array
(
"id" => "127342053975510",
"name" => "2016"
)
)
),
"id" => "145864899647476"
);
and this is my code:
$keys = array_keys($profile);
print_r($keys);
echo count($profile);
echo "<br>";
for ($i=4; $i <count($profile)-1 ; $i++) {
# code...
/*[4] => work [5] => education*/
echo $keys[$i]."<br>";
foreach ($profile[$keys[$i]] as $k => $v) {
# code...
echo "<br>$k<br>";
$keeys=array_keys($v);
print_r($keeys);
}
}