Home > OS >  How to search with multidimensional array search in PHP
How to search with multidimensional array search in PHP

Time:09-27

I'm getting the arrays I want get the price accroding to the ID and the quantity. this is my array can you please help me how to do this in using array search function. Please help me slove this issue.

This is my array view

Here i'm paasing the dm_id , decoration_code, q1 accroding to this value i want to get the one of the p value (p mean price)

$dm_id = LL0007;
$decoration_code = LL0007C9;
$quantity =  50;

original result = p1 = 0.25

How can get this result when I use the arrary search?

array:3 [▼
  0 => array:24 [▼
    "dm_id" => "LL0007"
    "decoration_code" => "LL0007B9"
    "decoration_name" => "Undecorated"
    "main_decoration" => "LL0007_LLNZ"
    "setup_change" => "0"
    "show_to_clients" => "1"
    "q1" => "50"
    "p1" => "0"
    "q2" => "100"
    "p2" => "0"
    "q3" => "250"
    "p3" => "0"
    "q4" => "500"
    "p4" => "0"
    "q5" => "0"
    "p5" => "0"
    "q6" => "0"
    "p6" => "0"
  ]
  1 => array:24 [▼
    "dm_id" => "LL0007"
    "decoration_code" => "LL0007C9"
    "decoration_name" => "1 Colour 1 Position Print"
    "main_decoration" => "LL0007_LLNZ"
    "setup_change" => "35"
    "show_to_clients" => "1"
    "q1" => "50"
    "p1" => "0.25"
    "q2" => "100"
    "p2" => "0.25"
    "q3" => "250"
    "p3" => "0.25"
    "q4" => "500"
    "p4" => "0.25"
    "q5" => "0"
    "p5" => "0"
    "q6" => "0"
    "p6" => "0"
  ]
  2 => array:24 [▼
    "dm_id" => "LL0007"
    "decoration_code" => "LL0007D9"
    "decoration_name" => "1 Position Digital Print"
    "main_decoration" => "LL0007_LLNZ"
    "setup_change" => "35"
    "show_to_clients" => "1"
    "q1" => "50"
    "p1" => "0.4"
    "q2" => "100"
    "p2" => "0.4"
    "q3" => "250"
    "p3" => "0.4"
    "q4" => "500"
    "p4" => "0.4"
    "q5" => "0"
    "p5" => "0"
    "q6" => "0"
    "p6" => "0"
  ]
]

CodePudding user response:

I know maybe I shouldn't answer (because very little information was given and no code was shown) but here is the code I found. It works but I don't know if there is better to do.

// Initialization
$dm_id = "LL0007";
$decoration_code = "LL0007C9";
$quantity = 50;
$price = 0;

// For Each Product
foreach($productList as $productKey => $productInfo)
{
    // If Product Identified
    if( ($productInfo["dm_id"]==$dm_id) && ($productInfo["decoration_code"]==$decoration_code) )
    {
        // For Each Info
        foreach($productInfo as $keyInfo => $valueInfo)
        {
            // If Good Info "Quantity"
            if( (substr($keyInfo,0,1)=="q") && (bcmul($valueInfo,1,0)>0) )
            {
                // If Quantity is Correcte
                if(bcmul($valueInfo,1,0)<=bcmul($quantity,1,0)) $price = $productInfo["p".substr($keyInfo,1,1).""];
            }
            // End - If Good Info "Quantity"
        }
        // End - For Each Info
    }
    // End - If Product Identified      
}
// End - For Each Product

// Display Result
echo "<b>".$price."</b>";

Product List is the array shown in the code in question.

  • Related