简体   繁体   English

PHP/CodeIgnitier 从这个多维 PHP 数组中获取单个值

[英]PHP/CodeIgnitier Get a single value from this multimdimensionnal PHP array

I want to retrieve a single value from this php array.我想从这个 php 数组中检索一个值。 My array in PHP is:我在 PHP 中的数组是:

Array
(
    [0] => stdClass Object
        (
            [id] => 27
            [id_customer] => 19
            [my_cart] => Array
(
    [c81e728d9d4c2f636f067f89cc14862c] => Array
        (
            [id] => 2
            [qty] => 1
            [price] => 39000
            [name] => HEBERGEMENT WEB MUTUALISE PREMIUM
            [rowid] => c81e728d9d4c2f636f067f89cc14862c
            [subtotal] => 39000
        )

    [a87ff679a2f3e71d9181a67b7542122c] => Array
        (
            [id] => 4
            [qty] => 1
            [price] => 150000
            [name] => HEBERGEMENT WEB MUTUALISE ULTIMATE
            [rowid] => a87ff679a2f3e71d9181a67b7542122c
            [subtotal] => 150000
        )

)
1
            [created_at] => 2020-03-30
        )

)

The problem is that I can not get qty, name, price问题是我无法获得数量、名称、价格

I tried by doing this in my foreach我尝试在我的 foreach 中这样做

foreach($cart_data as $cd){
  echo 'ID Table: '.$cd->id.'<br>';
  echo 'ID customer: '.$cd->id_customer.'<br>';

  $data = $cd->my_cart;
  if(is_array($data)){
    foreach($data as $s){
      echo 'My cart: '.$s;
    }
  }
}

But nothing is happened!但是什么都没有发生! I want to get price, name and qty.我想获得价格、名称和数量。

To get several keys from my_cart array, try to write the loop like this :要从my_cart数组中获取多个键,请尝试编写如下循环:

  foreach($cart_data as $cd){
    echo 'ID Table: '.$cd->id.'<br>';
    echo 'ID customer: '.$cd->id_customer.'<br>';

    $data = $cd->my_cart;
    if(is_array($data)){
        $filtered    = [];
        $keys        = ['qty', 'name', 'price']; // the keys
        $column_keys = array_flip($keys); // getting keys as values
        foreach ($data as $key => $val) {
            // getting only those key value pairs, which matches $column_keys
            $item = array_intersect_key($val, $column_keys);
            if (!empty($item))
                $filtered[$key] = $item;
        }
        echo 'My cart: <br/>';
        print_r($filtered);
    }
  }

In this line:在这一行:

    echo 'My cart: '.$s;

The variable $s holds an array, not a string.变量$s保存一个数组,而不是一个字符串。 If you follow your data structure, the contents of $s would be similar to this:如果您遵循您的数据结构, $s的内容将类似于:

    Array (
        [id] => 4
        [qty] => 1
        [price] => 150000
        [name] => HEBERGEMENT WEB MUTUALISE ULTIMATE
        [rowid] => a87ff679a2f3e71d9181a67b7542122c
        [subtotal] => 150000
    )

So, in order to get name, qty, price, you'd need to change your inner loop to something like this:因此,为了获得名称、数量、价格,您需要将内部循环更改为如下所示:

foreach ($data as $s) {
    echo 'Product name: '  . $s['name'] . '<br>';
    echo 'Quantity: '  . $s['qty'] . '<br>';
    echo 'Price: '  . $s['price'] . '<br>';
}

What happens when you try to use an array as a string is that it shows up as the string "Array" and will trigger a PHP Notice: Array to string conversion in [location] .当您尝试将数组用作字符串时会发生什么,它显示为字符串 "Array" 并触发PHP Notice: Array to string conversion in [location] I'm guessing this error might be the reason you get nothing, instead of My cart: Array .我猜这个错误可能是你什么也没得到的原因,而不是My cart: Array

I found the solution.我找到了解决方案。 When inserting into database I encode it like this htmlspecialchars(json_encode($data)) and decode in with json_decode while getting the $data.插入数据库时​​,我像这样编码htmlspecialchars(json_encode($data))并在获取 $data 的同时使用json_decode解码。 So now I have this:所以现在我有这个:

foreach($cart_data as $cd){
  echo 'ID Table: '.$cd->id.'<br>';
  echo 'ID customer: '.$cd->id_customer.'<br>';
  $data = json_decode($cd->my_cart);

  foreach($data as $row){
    echo $row->name;
    echo $row->qty;
    echo $row->price;
  } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM