Php 如何使用foreach获取我的ids产品?

Php 如何使用foreach获取我的ids产品?,php,codeigniter-3,Php,Codeigniter 3,我想在销售过程中使一个有效,但我的最后库存等于x库存,然后它将被添加到通知表中,但我如何才能获得我的产品ID only ID foreach,然后将它们散列到我的get_产品中 获取购物车的函数 $this->session->carrito = $this->sale->checar_existe_carrito(); $array = $this->sale->get_all_cart($this->session->carrito);

我想在销售过程中使一个有效,但我的最后库存等于x库存,然后它将被添加到通知表中,但我如何才能获得我的产品ID only ID foreach,然后将它们散列到我的get_产品中

获取购物车的函数

 $this->session->carrito = $this->sale->checar_existe_carrito();
 $array  = $this->sale->get_all_cart($this->session->carrito);
json

函数为有效,则库存产品等于x库存

public function concretar_venta(){
        if($this->sale->checa_carrito_vacio($this->session->carrito)){
            $total = $this->input->post("total", TRUE);
            $cantidad_pagada = $this->input->post("cantidad_pagada", TRUE);
            $cambio = $cantidad_pagada - $total;
            $producto = $this->products->get_product('861578d797aeb0634f77aff3f488cca2');
            if($this->sale->concretar_venta($this->session->carrito, $total, $cantidad_pagada, $cambio)){
                if($producto->stock <= 2){
                   $this->notification->addNotification('low stock', $product_id[$output], $user_id[$output_u], 'low stock',now());
                }else{
                    echo 'bien';
                }
            }
            else{
                echo "Ocurrio un error al concretar la venta, por favor intentelo de nuevo";
            }
        }
        else{
            "El carrito está vacío";
        }
    }

此解决方案基于您提供的JSON

$json = '[{"id":"4165","qty":"1.00","price":"77.00","name":"hammer"},{"id":"4168","qty":"3.00","price":"7.00","name":"whey protein"},{"id":"4169","qty":"3.00","price":"500.00","name":"papas"}]';

$jsonArray = json_decode($json, true);
// echo '<pre>'; print_r($jsonArray); die; // check what is coming in array

$ids = array();
// extract ids using loop
foreach ($jsonArray as $value) {
  $ids[] = md5($value['id']);
}
// print_r($ids); die;

foreach ($ids as $id) {
  $producto = $this->products->get_product($id);
}
$json='[{id:“4165”,“数量:”“1.00”,“价格:”“77.00”,“名称:”“hammer”},{id:“4168”,“数量:”“3.00”,“价格:”“7.00”,“名称:”“乳清蛋白”},{id:“4169”,“数量:”“3.00”,“价格:”“500.00”,“名称:”“爸爸”};
$jsonArray=json_decode($json,true);
//回声';打印(jsonArray);死亡;//检查阵列中出现的内容
$ids=array();
//使用循环提取ID
foreach($jsonArray作为$value){
$ids[]=md5($value['id']);
}
//打印(ID);死亡
foreach($id作为$id){
$producto=$this->products->get\u product($id);
}

您可以体验live here

您需要从JSON中提取唯一的id。是吗?请不要发布错误消息的图片。它破坏了该网站索引页面进行搜索的能力。是,但我想做的是,为什么数组在我尝试发送$ids时,它将数组作为列名而不是值发送?我不明白你在说什么?@aronimerial answer upadted立即检查当我尝试通过ids发送我的md5字符串时,然后在“where子句”中显示未知列“array”,它说它应该是字符串,但是我怎样才能发送dinamic阵列呢/
  public function FunctionName() {
    $json = '[{"id":"4165","qty":"1.00","price":"77.00","name":"hammer"},{"id":"4168","qty":"3.00","price":"7.00","name":"whey protein"},{"id":"4169","qty":"3.00","price":"500.00","name":"papas"}]';

    $jsonArray = json_decode($json, true);

    $ids = array();

    foreach ($jsonArray as $value) {
      $ids[] = md5($value['id']);
    }
      print_r($ids);
      $producto = $this->products->get_product($ids);
    }
$json = '[{"id":"4165","qty":"1.00","price":"77.00","name":"hammer"},{"id":"4168","qty":"3.00","price":"7.00","name":"whey protein"},{"id":"4169","qty":"3.00","price":"500.00","name":"papas"}]';

$jsonArray = json_decode($json, true);
// echo '<pre>'; print_r($jsonArray); die; // check what is coming in array

$ids = array();
// extract ids using loop
foreach ($jsonArray as $value) {
  $ids[] = md5($value['id']);
}
// print_r($ids); die;

foreach ($ids as $id) {
  $producto = $this->products->get_product($id);
}