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]
);
}
Related
$_SESSION['items_' . $restaurant_id]["menu_0"] = array(
order_id" => $order_id,
'user_order_id' => $last_order["user_order_id"],
"menu_id" => $menu_id,
"qty" => $qty,
);
that code is my session and will be return become this :
["items_250"]=>
array(2) {
["menu_0"]=>
array(3) {
["user_order_id"]=>
string(2) "85"
["menu_id"]=>
array(3) {
[0]=>
string(2) "236"
[1]=>
string(2) "357"
[2]=>
string(2) "232"
}
["qty"]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
[2]=>
string(1) "1"
}
}
in my `$menu_id is array :
array(4) {
[0]=>
string(3) "236"
[1]=>
string(3) "357"
[3]=>
string(3) "232"
}
this is my $qty :
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
[2]=>
string(1) "1"
}
i want trying to make my session will be return like this :
["items_250"]=>
array(3) {
["menu_0"]=>
array(3) {
["user_order_id"]=>
string(2) "85"
["menu_id"]=>
string(3) "236"
["qty"]=>
int(1)
}
["menu_1"]=>
array(3) {
["user_order_id"]=>
string(2) "85"
["menu_id"]=>
string(3) "357"
["qty"]=>
int(1)
}
["menu_2"]=>
array(3) {
["user_order_id"]=>
string(2) "85"
["menu_id"]=>
string(3) "232"
["qty"]=>
int(1)
}
guys can you help me how to make my session become like that?
thank you (:
Just use good ol' for loop. First get the count, then create the necessary rows:
$count = count($menu_id); // get count
for($i = 0; $i < $count; $i++) {
$_SESSION['items_' . $restaurant_id]['menu_' . $i] = array(
'user_order_id' => $last_order['user_order_id'],
'menu_id' => $menu_id[$i],
'qty' => $qty[$i],
);
}
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.
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
I currently have an array that looks like the one below but I only want to display show results in the array from which are unique (description), I presume with array_unique? but I keep getting the same results?
Here is my array:
$taglist = array(5) {
[0]=> array(5) {
["id"]=> string(2) "27"
["page_id"]=> string(2) "18"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
[1]=> array(5) {
["id"]=> string(2) "29"
["page_id"]=> string(2) "18"
["description"]=> string(3) "Tutorials"
["slug"]=> string(3) "tutorials"
["visibility"]=> string(7) "visible"
}
[2]=> array(5) {
["id"]=> string(2) "31"
["page_id"]=> string(2) "21"
["description"]=> string(3) "tag"
["slug"]=> string(3) "tag"
["visibility"]=> string(7) "visible"
}
[3]=> array(5) {
["id"]=> string(2) "32"
["page_id"]=> string(2) "21"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
}
Here is my while:
$items = array();
$results = $taglist;
foreach ($results as $result)
{
$items[]= $result['description'];
$items = array_unique($items);
}
echo '<ul>';
while ($tag_item = current($items))
{
echo '<li>'.$tag_item['description'].'</li>';
next($items);
}
echo '</ul>';
$taglist = array(
0 => array('description'=>'one'),
1 => array('description'=>'two'),
2 => array('description'=>'one'),
3 => array('description'=>'three'),
4 => array('description'=>'one'),
);
// echo var_export($taglist, 1); // uncomment to see the diff vs var_dump()
foreach($taglist as $tag){
$new[] = $tag['description'];
}
var_dump(array_unique($new));
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();