I have this array which I want to extract the values using the extract php function but for some reason it's not working for me.
This is the array:
Array
(
[5] => Array
(
[quantity] => 1
[price] => 45
)
[7] => Array
(
[quantity] => 1
[price] => 18
)
)
and this is the output I would like to see
$pid = 5;
$quantity = 1;
$price = 45;
Why not a simple foreach loop?
$my_array = array(
5 => array(
'quantity' => 1,
'price' => 45.00,
),
7 => array(
'quantity' => 1,
'price' => 18.00,
),
);
foreach($my_array as $pid => $data) {
$pid; // 5, 7
$data['quantity']; // 1, 1
$data['price']; // 45.00, 18.00
}
Related
I have array from foreach loop like this :
Array (
[id] => 2
[qty] => 1
[price] => 130000
[name] => 0002/PNBP/411971/1/02/18
)
Array (
[id] => 3
[qty] => 1
[price] => 120000
[name] => 0003/PNBP/411971/1/02/18
)
Array (
[id] => 4
[qty] => 1
[price] => 150000
[name] => 0004/PNBP/411971/1/02/18
)
I want insert data array in array like this :
Array(
Array (
[id] => 2
[qty] => 1
[price] => 130000
[name] => 0002/PNBP/411971/1/02/18
),
Array (
[id] => 3
[qty] => 1
[price] => 120000
[name] => 0003/PNBP/411971/1/02/18
),
Array (
[id] => 4
[qty] => 1
[price] => 150000
[name] => 0004/PNBP/411971/1/02/18
)
);
I try and code like this :
$data = array();
foreach ($_POST['id_kuitansi'] as $id_kuitansi){
$detail_kuitansi = $this->kuitansi_model->detail($id_kuitansi);
$i = $this->input;
$data = array(
'id' => $id_kuitansi,
'qty' => '1',
'price' => $detail_kuitansi['nilai'],
'name' => $detail_kuitansi['no_kuitansi']
);
print_r($data);
//$this->cart->insert($data);
}
Try this,
$data = array();
foreach ($_POST['id_kuitansi'] as $id_kuitansi){
$detail_kuitansi = $this->kuitansi_model->detail($id_kuitansi);
$i = $this->input;
$data[] .= array(
'id' => $id_kuitansi,
'qty' => '1',
'price' => $detail_kuitansi['nilai'],
'name' => $detail_kuitansi['no_kuitansi']
);
print_r($data);
//$this->cart->insert($data);
}
You were simply reassigned the values to $data each while you need to append the array value to the $data array
Problem solved with this code :
foreach ($_POST['id_kuitansi'] as $id_kuitansi)
{
$detail_kuitansi = $this->kuitansi_model->detail($id_kuitansi);
$i = $this->input;
$data[] = array(
'id' => $id_kuitansi,
'qty' => '1',
'price' => $detail_kuitansi['nilai'],
'name' => $detail_kuitansi['no_kuitansi']
);
}
//print_r($data);
$this->cart->insert($data);
Thanks Mohammad...
whenever we need to insert multiple data in single query, then we can user batch for inserting data. such as,
$data = $arrInsert = array();
foreach ($_POST['id_kuitansi'] as $id_kuitansi){
$detail_kuitansi = $this->kuitansi_model->detail($id_kuitansi);
$i = $this->input;
$data = array(
'id' => $id_kuitansi,
'qty' => '1',
'price' => $detail_kuitansi['nilai'],
'name' => $detail_kuitansi['no_kuitansi']
);
//print_r($data);
$arrInsert[] = $data;
//$this->cart->insert($data);
}
$this->db->insert_batch('tableName', $arrInsert);
In insert batch you can replace tableName with your table name.
Php multidimensional array same key’s same value’s related total in
new array. I have an array of following mentioned. i need new array
as total qty of same item_id. anyone can help would be appreciate.
My Original Array is as following
Array
(
[a] => Array
(
[item] => Array
(
[item_id] => 1
)
[qty] => 0
),
[b] => Array
(
[item] => Array
(
[item_id] => 2
)
[qty] => 35
),
[c] => Array
(
[item] => Array
(
[item_id] => 2
)
[qty] => 15
),
[e] => Array
(
[item] => Array
(
[item_id] => 3
)
[qty] => 20
),
);
I want array Output like following :
Array(
[0] => Array (
[item_id] => 1,
[item_total_qty] => 0,
)
[1] => Array (
[item_id] => 2,
[item_total_qty] => 50,
)
[2] => Array (
[item_id] => 3,
[item_total_qty] => 20,
)
);
Hope it help
$arrays = array(
'a' => array(
'item' => array(
'item_id' => 1
),
'qty' => 0
),
'b' => array(
'item' => array(
'item_id' => 2
),
'qty' => 35
),
'c' => array(
'item' => array(
'item_id' => 2
),
'qty' => 15
),
'd' => array(
'item' => array(
'item_id' => 3
),
'qty' => 20
)
);
$result = array();
foreach ($arrays as $key => $array) {
if (is_array($result) && !empty($result)) {
foreach ($result as $key => $r) {
if ($r['item_id'] == $array['item']['item_id']) {
$result[$key]['item_total_qty'] += $array['qty'];
continue 2;
}
}
$result[] = array(
'item_id' => $array['item']['item_id'],
'item_total_qty' => $array['qty']);
} else {
$result[] = array(
'item_id' => $array['item']['item_id'],
'item_total_qty' => $array['qty']);
}
}
Simple foreach on your original table:
$sorted = array();
foreach ($original as $item) {
$id = $item['item']['item_id'];
$sorted[$id]['item_total_qty'] = $sorted[$id] ? $sorted[$id] + $item['qty'] : item['qty'];
$sorted[$id]['item_id'] = $id;
}
$sorted = array_values($sorted);
I have below array,
Array ( [0] => Array ( [report_id] => 1 [amount] => 100.00 [category_name] => Trial1 ) [1] => Array ( [report_id] => 1 [amount] => 150.00 [category_name] => Trial2 ) [2] => Array ( [report_id] => 1 [amount] => 200.00 [category_name] => Trial2 )
What i want to send to have JSON with below format
It will get some of Equal category name and then send it as json.
[{'category_name': 'Trial1', 'Sum':100]}, {'category_name':'Trial2', 'Sum':350]
How can i achieve this?
Was thinking to get foreach loop and then make compare of category_name and use .=+ to get sum? but i lost there,
Thanks,
Try below solution:
<?php
$array = array (
'0' => Array ( 'report_id' => 1, 'amount' => '100.00', 'category_name' => 'Trial1' ) ,
'1' => Array ( 'report_id' => 1, 'amount' => '150.00' ,'category_name' => 'Trial2' ),
'2' => Array ( 'report_id' => 1, 'amount' => '200.00' ,'category_name' => 'Trial2' ) ,
);
$new_array = array();
foreach($array as $a){
if(!isset($new_array[$a['category_name']]['amount'])){
$new_array[$a['category_name']]['amount'] = 0;
}
$new_array[$a['category_name']] = array(
'category_name' => $a['category_name'],
'amount' => $new_array[$a['category_name']]['amount'] + $a['amount'],
);
}
//print_r(array_values($new_array));
echo json_encode(array_values($new_array));
Output
[{"category_name":"Trial1","amount":100},{"category_name":"Trial2","amount":350}]
Possible solution:
$categoriesArray = array();
foreach ($yourArray as $arrayItem) {
if (!isset($categoriesArray[$arrayItem['category_name']])) {
$categoriesArray[$arrayItem['category_name']] = array(
'category_name' => $arrayItem['category_name'],
'sum' => 0
);
}
$categoriesArray[$arrayItem['category_name']]['sum'] += $arrayItem['amount'];
}
$categoriesArray = json_encode(array_values($categoriesArray));
Assuming $input is your array and $output is the JSON string:
$categorysum = [];
array_walk($input, function($el) use (&$categorysum) {
$categorysum += [$el['category_name'] => ['category_name' => $el['category_name'], 'Sum' => 0]];
$categorysum[$el['category_name']]['Sum'] += $el['amount'];
});
$output = json_encode(array_values($categorysum));
Im having troubles counting this.
I want to count all rates than belongs to id_image.
Maybe like key = id_image and value = tot count, id tried with array_count_values, but i cant use it normally when its multi :-S
Array
(
[0] => Array
(
[id_image] => 12
[rate] => 4
)
[1] => Array
(
[id_image] => 13
[rate] => 4
)
[2] => Array
(
[id_image] => 14
[rate] => 3
)
[3] => Array
(
[id_image] => 13
[rate] => 4
)
[4] => Array
(
[id_image] => 12
[rate] => 5
)
[5] => Array
(
[id_image] => 12
[rate] => 4
)
)
// test array
$arr = array(
0 => array(
'id_image' => 1,
'rate' => 3
),
1 => array(
'id_image' => 2,
'rate' => 8
),
2 => array(
'id_image' => 3,
'rate' => 4
),
3 => array(
'id_image' => 1,
'rate' => 2
),
4 => array(
'id_image' => 3,
'rate' => 2
)
);
// put the length in a var so we don't keep calling count();
$length = count($arr);
// the new array that'll hold the sum of the rates
$totals = array();
// iterate through the test array
for ($i = 0; $i < $length; ++$i) {
// check if $totals already contains data for the specified id_image
if (isset($totals[$arr[$i]['id_image']])) {
// if so, add data
$totals[$arr[$i]['id_image']] += $arr[$i]['rate'];
} else {
// if not so, set data
$totals[$arr[$i]['id_image']] = $arr[$i]['rate'];
}
}
var_dump($totals);
Example
I have the following array:
Array
(
[0] => Array
(
[hotelID] => 10
[hotelcategoryID] => 12
[hotelName] => Grand Forest Metsovo
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 2
)
[1] => Array
(
[hotelID] => 10
[hotelcategoryID] => 12
[hotelName] => Grand Forest Metsovo
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 3
)
[2] => Array
(
[hotelID] => 10
[hotelcategoryID] => 12
[hotelName] => Grand Forest Metsovo
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 4
)
[3] => Array
(
[hotelID] => 14
[hotelcategoryID] => 7
[hotelName] => Hotel Metropolis
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 23
)
[4] => Array
(
[hotelID] => 14
[hotelcategoryID] => 7
[hotelName] => Hotel Metropolis
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 24
)
)
I have two different hotelID keys. I would like to extract only one element (the first one) where the hotelID is unique in whole array. I am trying with following code:
$data['uniqueHotels'] = array_map('unserialize', array_unique(array_map('serialize', $hotels)));
but without any luck so far.
Anyone can give me a hint?
If looking for the first element:
<?php
$hotels = array(
array(
'id' => 1,
'hotelID' => 10
),
array(
'id' => 2,
'hotelID' => 10,
),
array(
'id' => 3,
'hotelID' => 20,
),
array(
'id' => 4,
'hotelID' => 20,
),
);
function getUniqueHotels($hotels) {
$uniqueHotels = array();
foreach($hotels as $hotel) {
$niddle = $hotel['hotelID'];
if(array_key_exists($niddle, $uniqueHotels)) continue;
$uniqueHotels[$niddle] = $hotel;
}
return $uniqueHotels;
}
$unique_hotels = getUniqueHotels($hotels);
print_r($unique_hotels);
results in:
Array
(
[10] => Array
(
[id] => 1
[hotelID] => 10
)
[20] => Array
(
[id] => 3
[hotelID] => 20
)
)
You could simply loop through the array and add them to a new array, indexed by hotelID. This way any duplicates will just overwrite the existing value and you end up with one entry per hotel:
$unique = array();
foreach ($hotels as $value)
{
$unique[$value['hotelID']] = $value;
}
$data['uniqueHotels'] = array_values($unique);
Here is a dynmaic solution:
function uniqueAssocArray($array, $uniqueKey){
$unique = array();
foreach ($array as $value){
$unique[$value[$uniqueKey]] = $value;
}
$data = array_values($unique);
return $data;
}
How to use:
uniqueAssocArray($yourArray, 'theKey');
along the lines of what you're trying,
array_unique(array_map(function($hotel) { return $hotel['hotelID']; }, $array))