@80689248440

Как доабвить элемент в сложный массив?

Есть вот такой массив который лежит в $cart_object->cart_contents
Как в него добавить в каждую ветку после [line_total] дополнительный элемент равный [line_total]
то есть например [oldprise] = [line_total]
Array
(
    [7f7fc17485016b50781712c529de1df4] = Array
        (
            [product_id] = 20511
            [variation_id] = 
            [variation] = Array
                (
                )

            [quantity] = 1
            [line_total] = 90360
            [line_tax] = 0
            [line_subtotal] = 90360
            [line_subtotal_tax] = 0
            [line_tax_data] = Array
                (
                    [total] = Array
                        (
                        )

                    [subtotal] = Array
                        (
                        )

                )

            [data] = WC_Product_Simple Object
                (
                    [id] = 20511
                    [post] = WP_Post Object
                        (
                            [ID] = 20511
                            [post_author] = 1
                            [post_date] = 2015-07-07 21:54:06
                            [post_date_gmt] = 2015-07-07 21:54:06
                            [post_content] = 
                            [post_title] = 
                            [post_excerpt] = 
                            [post_status] = publish
                            [comment_status] = open
                            [ping_status] = closed
                            [post_password] = 
                            [post_name] = 
                            [to_ping] = 
                            [pinged] = 
                            [post_modified] = 2015-08-07 11:13:56
                            [post_modified_gmt] = 2015-08-07 07:13:56
                            [post_content_filtered] = 
                            [post_parent] = 0
                            [guid] =
                            [menu_order] = 0
                            [post_type] = product
                            [post_mime_type] = 
                            [comment_count] = 0
                            [filter] = raw
                        )

                    [product_type] = simple
                    [dimensions:protected] = 
                    [shipping_class:protected] = 
                    [shipping_class_id:protected] = 0
                    [price] = 90360
                    [stock_status] = instock
                    [tax_status] = taxable
                )

        )

    [15256a6b7f55e835d0d50a20833b11bb] = Array
        (
            [product_id] = 33189
            [variation_id] = 
            [variation] = Array
                (
                )

            [quantity] = 1
            [line_total] = 124120
            [line_tax] = 0
            [line_subtotal] = 124120
            [line_subtotal_tax] = 0
            [line_tax_data] = Array
                (
                    [total] = Array
                        (
                        )

                    [subtotal] = Array
                        (
                        )

                )

            [data] = WC_Product_Simple Object
                (
                    [id] = 33189
                    [post] = WP_Post Object
                        (
                            [ID] = 33189
                            [post_author] = 1
                            [post_date] = 2015-07-07 21:54:06
                            [post_date_gmt] = 2015-07-07 17:54:06
                            [post_content] = 
                            [post_title] = 
                            [post_excerpt] = 
                            [post_status] = publish
                            [comment_status] = open
                            [ping_status] = closed
                            [post_password] = 
                            [post_name] = 
                            [to_ping] = 
                            [pinged] = 
                            [post_modified] = 2015-07-16 01:34:06
                            [post_modified_gmt] = 2015-07-15 21:34:06
                            [post_content_filtered] = 
                            [post_parent] = 0
                            [guid] = 
                            [menu_order] = 0
                            [post_type] = product
                            [post_mime_type] = 
                            [comment_count] = 0
                            [filter] = raw
                        )

                    [product_type] = simple
                    [dimensions:protected] = 
                    [shipping_class:protected] = 
                    [shipping_class_id:protected] = 0
                    [price] = 124120
                    [manage_stock] = no
                    [stock_status] = instock
                    [tax_status] = taxable
                )

        )

    [c3a83e015935188821aa9ee65e6b322f] = Array
        (
            [product_id] = 32081
            [variation_id] = 
            [variation] = Array
                (
                )

            [quantity] = 1
            [line_total] = 87300
            [line_tax] = 0
            [line_subtotal] = 87300
            [line_subtotal_tax] = 0
            [line_tax_data] = Array
                (
                    [total] = Array
                        (
                        )

                    [subtotal] = Array
                        (
                        )

                )

            [data] = WC_Product_Simple Object
                (
                    [id] = 32081
                    [post] = WP_Post Object
                        (
                            [ID] = 32081
                            [post_author] = 1
                            [post_date] = 2015-07-26 13:30:01
                            [post_date_gmt] = 2015-07-26 13:30:01
                            [post_content] = 
                            [post_title] = 
                            [post_excerpt] = 
                            [post_status] = publish
                            [comment_status] = open
                            [ping_status] = closed
                            [post_password] = 
                            [post_name] = 
                            [to_ping] = 
                            [pinged] = 
                            [post_modified] = 2015-08-09 01:45:18
                            [post_modified_gmt] = 2015-08-08 21:45:18
                            [post_content_filtered] = 
                            [post_parent] = 0
                            [guid] = 
                            [menu_order] = 0
                            [post_type] = product
                            [post_mime_type] = 
                            [comment_count] = 0
                            [filter] = raw
                        )

                    [product_type] = simple
                    [dimensions:protected] = 
                    [shipping_class:protected] = 
                    [shipping_class_id:protected] = 0
                    [price] = 87300
                    [stock_status] = instock
                    [tax_status] = taxable
                )

        )

)
  • Вопрос задан
  • 215 просмотров
Решения вопроса 1
littleguga
@littleguga
Не стыдно не знать, а стыдно не интересоваться.
foreach ($cart_object->cart_contents as $item) {
    $item["oldprise"] = $item["line_total"];
}
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
@heartdevil
плыву как воздушный шарик
Привет.

Поиск в гугле дал вот это

/*
 * Inserts a new key/value after the key in the array.
 *
 * @param $key
 *   The key to insert after.
 * @param $array
 *   An array to insert in to.
 * @param $new_key
 *   The key to insert.
 * @param $new_value
 *   An value to insert.
 *
 * @return
 *   The new array if the key exists, FALSE otherwise.
 *
 * @see array_insert_before()
 */
function array_insert_after($key, array (link is external) &$array, $new_key, $new_value) {
  if (array_key_exists (link is external)($key, $array)) {
    $new = array (link is external)();
    foreach ($array as $k => $value) {
      $new[$k] = $value;
      if ($k === $key) {
        $new[$new_key] = $new_value;
      }
    }
    return $new;
  }
  return FALSE;
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы