Use PHP if() to match item in array - php

I am working on an admin panel in which the code will display different if the PDO query returns all the admins from the admin table and the userid matches 1 of the admin id results. Only there can be an unlimited amount of admins and I don't want to be editing the code for each of them.
Would it be possible to do this (pseudo code):
if($userid isfoundin $result['admin_user_id']{
-- admin code here
}
I haven't yet written the admin table as I want more info on it first but if I cycle through the user table and look for id $query->fetchAll() then I get this:
array(30) { [0]=> array(2) { ["id"]=> string(1) "7" [0]=> string(1) "7" } [1]=> array(2) { ["id"]=> string(1) "6" [0]=> string(1) "6" } [2]=> array(2) { ["id"]=> string(1) "8" [0]=> string(1) "8" } [3]=> array(2) { ["id"]=> string(2) "31" [0]=> string(2) "31" } [4]=> array(2) { ["id"]=> string(2) "26" [0]=> string(2) "26" } [5]=> array(2) { ["id"]=> string(1) "4" [0]=> string(1) "4" } [6]=> array(2) { ["id"]=> string(2) "35" [0]=> string(2) "35" } [7]=> array(2) { ["id"]=> string(2) "21" [0]=> string(2) "21" } [8]=> array(2) { ["id"]=> string(2) "38" [0]=> string(2) "38" } [9]=> array(2) { ["id"]=> string(2) "24" [0]=> string(2) "24" } [10]=> array(2) { ["id"]=> string(2) "34" [0]=> string(2) "34" } [11]=> array(2) { ["id"]=> string(2) "20" [0]=> string(2) "20" } [12]=> array(2) { ["id"]=> string(2) "19" [0]=> string(2) "19" } [13]=> array(2) { ["id"]=> string(2) "23" [0]=> string(2) "23" } [14]=> array(2) { ["id"]=> string(2) "33" [0]=> string(2) "33" } [15]=> array(2) { ["id"]=> string(2) "28" [0]=> string(2) "28" } [16]=> array(2) { ["id"]=> string(1) "3" [0]=> string(1) "3" } [17]=> array(2) { ["id"]=> string(2) "15" [0]=> string(2) "15" } [18]=> array(2) { ["id"]=> string(1) "9" [0]=> string(1) "9" } [19]=> array(2) { ["id"]=> string(2) "25" [0]=> string(2) "25" } [20]=> array(2) { ["id"]=> string(1) "1" [0]=> string(1) "1" } [21]=> array(2) { ["id"]=> string(2) "32" [0]=> string(2) "32" } [22]=> array(2) { ["id"]=> string(1) "5" [0]=> string(1) "5" } [23]=> array(2) { ["id"]=> string(2) "18" [0]=> string(2) "18" } [24]=> array(2) { ["id"]=> string(2) "29" [0]=> string(2) "29" } [25]=> array(2) { ["id"]=> string(2) "27" [0]=> string(2) "27" } [26]=> array(2) { ["id"]=> string(2) "30" [0]=> string(2) "30" } [27]=> array(2) { ["id"]=> string(2) "22" [0]=> string(2) "22" } [28]=> array(2) { ["id"]=> string(2) "10" [0]=> string(2) "10" } [29]=> array(2) { ["id"]=> string(2) "36" [0]=> string(2) "36" } }
this si what I am trying to use right now
require_once $_SERVER['DOCUMENT_ROOT']."/resources/settings.php";
$query = $pdo->prepare("SELECT id FROM users");
$query->execute();
var_dump($query->fetchAll());
if (in_array($user['id'], $query->fetchAll())){
echo $user['id'];
}

Yes, you can use in_array() for this.
I assume that you have a set of users for admin. Say, it is:
$adminUsers = array("admin", "administrator");
# Or by your code
$adminUsers = $result['admin_user_id'];
Now the code part is like:
if (in_array($userid, $adminUsers))
// Admin code here

I think you're looking for in_array() to see if a given item is in an array.

You should use in_array() if all you need to know is that the user ID exists in your array.
But if you actually need a reference to the item, take a look at array_search() http://au1.php.net/manual/en/function.array-search.php

Instead of going through all the users in your admins table and then having another loop in order to find if a specific user exists , why not doing this check in the query itself?
For instance:
$stmt = $mysqli->prepare("SELECT name,email FROM admins WHERE id=?");
$stmt->bind_param("d", $user_id);
$stmt->execute();
$stmt->bind_result($name, $email);
if ($stmt->fetch()) {
//The user with user_id exists in the admins table , show him the admin panel.
//You can use $name and $email.
}
$stmt->close();

Related

using array_unique, I appear to have stripped out the records that were duplicated?

Within available_options I have somehow stripped out Express when I just wanted to keep one of them?
The array looks like this
["options"]=>
array(9) {
[0]=>
array(8) {
["id"]=>
string(2) "79"
["product_id"]=>
string(2) "15"
["sku"]=>
string(9) "CSR-FTC4S"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "35.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "219"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(19) "Five Ten C4 Stealth"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(5) "FTC4S"
["user_value"]=>
string(25) "Five Ten C4 Stealth 5.5mm"
["sequence"]=>
string(1) "0"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "220"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
[2]=>
array(6) {
["id"]=>
string(3) "221"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "23"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(16) "Rand Toe Patches"
["name"]=>
string(3) "RTP"
["sku"]=>
string(3) "RTP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "10.000"
}
}
}
}
}
[1]=>
array(8) {
["id"]=>
string(2) "80"
["product_id"]=>
string(2) "15"
["sku"]=>
string(10) "CSR-FTONYX"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "37.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "222"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "2"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "2"
["admin_name"]=>
string(13) "Five Ten Onyx"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(6) "FTONYX"
["user_value"]=>
string(19) "Five Ten Onyx 4.5mm"
["sequence"]=>
string(1) "1"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "223"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
and my code goes like this
foreach($this->_data as &$data) {
foreach($data['options'] as &$option) {
$option['available_options'] = array_unique($option['available_options']);
}
}
It's working apart from it's stripped out the duplicates rather than showing them once?
array_unique does not work recursively, you need to go inside your array to apply it on option_data directly.
foreach($this->_data as &$data) {
foreach ($data['options'] as &$option) {
foreach ($option['available_options'] as &$available_option) {
foreach ($available_option['option_data'] as &$option_data) {
$option_data = array_unique($option_data);
}
}
}
}
This way, the last option_data looks like
'option_data' => [
[
'id' => '1',
'admin_name' => 'Express',
'sku' => 'EXP',
'sequence' => '9999',
'option_price' => '25.000'
]
]
But as you can see, the value Express only appear once, but user_value and status are removed too, because there value is 1, like id.

Creating multidimensional array PHP

I'm attempting to create a multidimensional array which should have the ID and quantity from the $_POST array. At the moment it seems to put every quantity into each an element with each ID.However I want it to take the first elements from each array and then add them together to a new array and so on.
Whereas it should be
ID 1 - Quantity 100
ID 2 - Quantity 50
etc
But at the moment I get this
array(16) {
[0]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(2) "50"
}
[1]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(3) "100"
}
[2]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(3) "100"
}
[3]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(3) "100"
}
[4]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(2) "50"
}
[5]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(3) "100"
}
[6]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(3) "100"
}
[7]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(3) "100"
}
[8]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(2) "50"
}
[9]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(3) "100"
}
[10]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(3) "100"
}
[11]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(3) "100"
}
[12]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(2) "50"
}
[13]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(3) "100"
}
[14]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(3) "100"
}
[15]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(3) "100"
}
}
Here is my PHP code.
foreach($_POST['sweetids'] as $id) {
foreach($_POST['quantites'] as $quantity) {
$stock_array[] = array(
"id"=> $id,
"quantity" => $quantity
);
}
}
I think this is what you're trying to achieve:
foreach($_POST['sweetids'] as $key=>$id) {
$stock_array[] = array(
"id"=> $id,
"quantity" => $_POST['quantities'][$key]
);
}
You're iterating $_POST['quantities'] for every $_POST['sweetids'] which is probably not what you intend. When you iterate both, your result will be every combination of sweetids and quantities, not each pair of them.
I'm guessing you meant something more like:
// Assuming you already verified that $_POST['quantities'] and $_POST['sweetids'] exist
// and that both of them have the same number of elements
for ( $i = 0, $len = count($_POST['sweetids']); $i < $len; $i++ ) {
$stock_array[] = array(
'id' => $_POST['sweetids'][$i],
'quantity' => $_POST['quantities'][$i]
);
}

PHP - Sum values of a matching key if 3 other key values match

I have an array that has multiple years of total sales for several countries. Below is small sample of the array. Some countries had sales in several different currencies but are now converted into the same currency. Now I need to add those total sales together where the Year, Month and Country values are the same but still keep the similar structure.
If values of ["Year"] & ["Month"] & ["Country"] match, then I need sum the values of ["Total_Sales"] and keep the same array structure.
I'm not sure where to begin. I'm sure there is a foreach loop or 2 involved.
Here is the array
array(6) {
[0]=>
array(4) {
["Year"]=>
string(4) "2014"
["Month"]=>
string(2) "12"
["Country"]=>
string(2) "NZ"
["Total_Sales"]=>
float(8.25)
}
[1]=>
array(4) {
["Year"]=>
string(4) "2014"
["Month"]=>
string(2) "12"
["Country"]=>
string(2) "NZ"
["Total_Sales"]=>
string(6) "5.50"
}
[2]=>
array(4) {
["Year"]=>
string(4) "2014"
["Month"]=>
string(2) "12"
["Country"]=>
string(2) "US"
["Total_Sales"]=>
string(9) "10.05"
}
[3]=>
array(4) {
["Year"]=>
string(4) "2015"
["Month"]=>
string(1) "1"
["Country"]=>
string(2) "NZ"
["Total_Sales"]=>
float(9.50)
}
[4]=>
array(4) {
["Year"]=>
string(4) "2015"
["Month"]=>
string(1) "1"
["Country"]=>
string(2) "NZ"
["Total_Sales"]=>
string(6) "15.00"
[5]=>
array(4) {
["Year"]=>
string(4) "2015"
["Month"]=>
string(2) "1"
["Country"]=>
string(2) "US"
["Total_Sales"]=>
string(9) "6.00"
}
Here's the results that I would like:
array(4) {
[0]=>
array(4) {
["Year"]=>
string(4) "2014"
["Month"]=>
string(2) "12"
["Country"]=>
string(2) "NZ"
["Total_Sales"]=>
float(13.75)
}
[1]=>
array(4) {
["Year"]=>
string(4) "2014"
["Month"]=>
string(2) "12"
["Country"]=>
string(2) "US"
["Total_Sales"]=>
string(9) "10.05"
}
[2]=>
array(4) {
["Year"]=>
string(4) "2015"
["Month"]=>
string(1) "1"
["Country"]=>
string(2) "NZ"
["Total_Sales"]=>
float(24.50)
}
[3]=>
array(4) {
["Year"]=>
string(4) "2015"
["Month"]=>
string(2) "1"
["Country"]=>
string(2) "US"
["Total_Sales"]=>
string(9) "6.00"
}
do something like
foreach($currentArray as $value){
$newArraykey = $value["country"]."-".$value["Year"]."-".$value["month"]
if(isset($newArray[$newArraykey])){
$newArray[$newArraykey]["Total_Sales"] = $newArray[$newArraykey]["Total_Sales"] + $value["Total_Sales"];
}else{
$newArray[$newArraykey] = $value;
}
}
$newArray would be something like
array(4) {
[NZ-2014-12]=>
array(4) {
["Year"]=>
string(4) "2014"
["Month"]=>
string(2) "12"
["Country"]=>
string(2) "NZ"
["Total_Sales"]=>
float(13.75)
}
[US-2014-12]=>
array(4) {
["Year"]=>
string(4) "2014"
["Month"]=>
string(2) "12"
["Country"]=>
string(2) "US"
["Total_Sales"]=>
string(9) "10.05"
}
[NZ-2015-1]=>
array(4) {
["Year"]=>
string(4) "2015"
["Month"]=>
string(1) "1"
["Country"]=>
string(2) "NZ"
["Total_Sales"]=>
float(24.50)
}
[US-2015-1]=>
array(4) {
["Year"]=>
string(4) "2015"
["Month"]=>
string(2) "1"
["Country"]=>
string(2) "US"
["Total_Sales"]=>
string(9) "6.00"
}
If you do not want the keys like country-year-month they use array_values($newArray) this would return exactly the same array you wanted.

Codeigniter 2.x, ActiveRecord, var_dump(), what is the expected result?

I am trying to debug some problems I am having with viewing data from a database. I am using codeigniter 2.x and active records. Here is my code:
$data = $this->db->select('country_id', 'country')->from('mhcountry')->get()->result();
var_dump($data);
die;
Var_dump returns the following:
array(20) { [0]=> object(stdClass)#21 (1) { ["country_id"]=> string(1) "1" } [1]=> object(stdClass)#22 (1) { ["country_id"]=> string(1) "2" } [2]=> object(stdClass)#23 (1) { ["country_id"]=> string(1) "3" } [3]=> object(stdClass)#24 (1) { ["country_id"]=> string(1) "4" } [4]=> object(stdClass)#25 (1) { ["country_id"]=> string(1) "5" } [5]=> object(stdClass)#26 (1) { ["country_id"]=> string(1) "6" } [6]=> object(stdClass)#27 (1) { ["country_id"]=> string(1) "7" } [7]=> object(stdClass)#28 (1) { ["country_id"]=> string(1) "8" } [8]=> object(stdClass)#29 (1) { ["country_id"]=> string(1) "9" } [9]=> object(stdClass)#30 (1) { ["country_id"]=> string(2) "10" } [10]=> object(stdClass)#31 (1) { ["country_id"]=> string(2) "11" } [11]=> object(stdClass)#32 (1) { ["country_id"]=> string(2) "12" } [12]=> object(stdClass)#33 (1) { ["country_id"]=> string(2) "13" } [13]=> object(stdClass)#34 (1) { ["country_id"]=> string(2) "14" } [14]=> object(stdClass)#35 (1) { ["country_id"]=> string(2) "15" } [15]=> object(stdClass)#36 (1) { ["country_id"]=> string(2) "16" } [16]=> object(stdClass)#37 (1) { ["country_id"]=> string(2) "17" } [17]=> object(stdClass)#38 (1) { ["country_id"]=> string(2) "18" } [18]=> object(stdClass)#39 (1) { ["country_id"]=> string(2) "19" } [19]=> object(stdClass)#40 (1) { ["country_id"]=> string(2) "20" } }
I have 20 countries entered from 1 to 20 BUT I do not see my country names in the var_dump. It is just dumping the first field. Is this normal or expected?
I tried the same thing on another table and it also just returns the first field.
You should pay attention to the documentation here, meanwhile you can try this :
$this->db->select(array('country_id', 'country'))
// OR
$this->db->select('country_id, country')
$data = $this->db->select('country_id', 'country')->from('mhcountry')->get()->result();
This is wrong. it will get only country_id.
Use this
$data = $this->db->select('country_id , country')->from('mhcountry')->get()->result();

Get specific value from PHP array using foreach key value

I have the following array:
array(15) {
[0]=> object(stdClass)#317 (2) { ["id"]=> string(1) "2" ["value"]=> string(1) "1" }
[1]=> object(stdClass)#316 (2) { ["id"]=> string(1) "3" ["value"]=> string(531) "awfaww" }
[2]=> object(stdClass)#315 (2) { ["id"]=> string(1) "4" ["value"]=> string(1) "1" }
[3]=> object(stdClass)#318 (2) { ["id"]=> string(1) "5" ["value"]=> string(1) "1" }
[4]=> object(stdClass)#319 (2) { ["id"]=> string(1) "6" ["value"]=> string(1) "1" }
[5]=> object(stdClass)#320 (2) { ["id"]=> string(1) "7" ["value"]=> string(1) "1" }
[6]=> object(stdClass)#321 (2) { ["id"]=> string(1) "8" ["value"]=> string(1) "1" }
[7]=> object(stdClass)#322 (2) { ["id"]=> string(2) "30" ["value"]=> string(8) "12:30:02" }
[8]=> object(stdClass)#323 (2) { ["id"]=> string(2) "31" ["value"]=> string(8) "18:12:00" }
[9]=> object(stdClass)#324 (2) { ["id"]=> string(2) "11" ["value"]=> string(10) "2014-06-17" }
[10]=> object(stdClass)#325 (2) { ["id"]=> string(2) "12" ["value"]=> string(10) "2014-06-26" }
[11]=> object(stdClass)#326 (2) { ["id"]=> string(2) "14" ["value"]=> string(1) "2" }
[12]=> object(stdClass)#327 (2) { ["id"]=> string(2) "15" ["value"]=> string(1) "2" }
[13]=> object(stdClass)#328 (2) { ["id"]=> string(2) "16" ["value"]=> string(1) "4" }
[14]=> object(stdClass)#329 (2) { ["id"]=> string(2) "17" ["value"]=> string(1) "5" }
}
I would like to get a specific value from this array using the ID. For example, if the ID: 11 is found in the array I want to retrieve its value. How can I do this?
Try something like this:
<?php
function findById($array, $id) {
foreach ($array as $value) {
if ($value->id == $id) {
return $value->value;
}
}
return null;
}
$result = findById($yourArray, 11);
?>
if the array is static for each run - i'd suggest changing the array into "key"=>"value" array, using this:
$new_arr = array();
foreach($original_array as $object) {
$new_arr[$object->id] = $object->value;
}
and then you can just use $new_arr[id], instead of searching the whole original array each time.
You can use array_filter:
array_filter($arr, function($i) { return $i->id == '11'; });
See Documentation

Categories