print_r($_POST['Receipt']['name'])
gave me an array like this
Array (
[Donations] => Array (
[name] => Donations
)
[Fees] => Array (
[ledger] => Fees
)
[100] => Array (
[amount] => 100
)
[Others] => Array (
[name] => Others
)
)
here the two repeating numeric values 100 and fees(they are both the values the field amount and ledger respectively) is what i want to store in an array. I tried some thing like this
foreach ($_POST['Receipt']['name'] as $item)
{
if (isset($item['amount']))
{
$amount[]= $item['amount'];
}
if (isset($item['ledger']))
{
$ledger[]= $item['ledger'];
}
}
but $ledger[] and $amount[] will not store repeating values of them. for amounnt[], it would store only the 100 in [amount] => 100 and will not store the 100 in [100] => Array How can i store the repeating values in this array?
try this
foreach ($_POST['Receipt']['name'] as $item)
{
if (isset($item['100']))
{
$amount[]= $item['100']['amount'];
}
if (isset($item['fees']))
{
$ledger[]= $item['fees']['ledger'];
}
}
Related
I find I can't figure out the desired output.
I have JSON raw data contains this:
Array
(
[data] => Array
(
[0] => Array
(
[title] => currency1
[tickers] => Array
(
[0] => USD
)
)
[1] => Array
(
[title] => currency2
[tickers] => Array
(
[0] => USD
[1] => EUR
)
)
[2] => Array
(
[title] => currency3
[tickers] => Array
(
[0] => USD
)
)
)
)
This is what I have tried
$num =0;
foreach ($json_data as $key => $story){
$num++;
foreach($story as $subkey => $subvalue){
if(is_array($subvalue)){
$title = $story[$subkey]['title'];
$tickers = $story[$subkey]['tickers'][$num];
foreach($tickers as $key2 => $val2){
if($val2>=1){
unset($story);
}else{
//echo output
}
}
}
}
I want to get all the keys of the arrays and if tickers has multiple value don't echo it.
Sample desired output:
curreny1 Key is 0 and tickers is USD
curreny3 Key is 2 and tickers is USD
You should be able to just loop over the data key of your source data, extracting the title and tickers and outputting a result if the count of the tickers is 1:
foreach ($json_data['data'] as $key => $story) {
$tickers = $story['tickers'];
if (count($tickers) != 1) continue;
$title = $story['title'];
echo "$title Key is $key and tickers is {$tickers[0]}\n";
}
Output (for your sample data):
currency1 Key is 0 and tickers is USD
currency3 Key is 2 and tickers is USD
Demo on 3v4l.org
I have a query result set which I loop through. Depending on the data inside the result set, I want to add it to the array, or if the specific value contains an ID, loop again, and add it at the current position. I want to end up with something like this.
Array
(
[0] => Array
(
[title] => Array
(
[0] => Array
(
[value] => Lorem ipsum
)
)
[uid] => Array
(
[0] => Array
(
[uid] => Array
(
[value] => 1
)
[field_name] => Array
(
[value] => John Doe
)
)
)
)
)
I accomplished the result, but I'm looping over and over again, I would like it to be recursive. So I ended up doing something like this.
foreach ($fields as $nr => $field){
if ($field_type == 'entity_reference'){
// Query again
...
foreach ($fields2 as $nr2 => $field2){
if ($field_type2 == 'entity_reference'){
// Query again
...
} else {
$return[$nr][$field][$nr2][$fieldl2] = $value2;
}
}
} else {
$return[$nr][$field][] = $value;
}
}
How can I make it recursive, so I don't have to loop many times?
I have an array like this. Array name is amount
Array
(
[0] => Array
(
[amount] => 0
)
[1] => Array
(
[amount] => 0
)
[2] => Array
(
[amount] => 0
)
[3] => Array
(
[amount] => 207.2
)
[4] => Array
(
[amount] => 1458.8
)
[5] => Array
(
[amount] => 207.2
)
)
Here I want to remove last key value from list when I display it into a table.
foreach(amount as key => values)
{
print_r($values);
echo"<br>";
}
When this print it should be show without last key element. like this
0
0
0
207.21
1458.8
I hope somebody will help me to solve this problem.
Hopefully
If you want to remove the last value if the array, you can:
First option, you can use array_pop to remove the last value.
//Assign the values on a temp array
$tempAmount = $amount;
//Remove the last value of the temp array.
array_pop( $tempAmount );
//You can loop as usual the temp array
http://php.net/manual/en/function.array-pop.php
Second option: You can use a condition, like:
foreach($amount as $key => $values)
{
if ( $key < count( $amount ) - 1 ) {
print_r($values);
echo"<br>";
}
}
You can use array_slice function to extract a sub-array from the index 0 and having size the size of old array - 1:
$newArray = array_slice($oldArray, 0, count($oldArray) -1);
I am using Codeigniter and I have a function like this .
function total_income(){
$data['total_income'] = $this->mod_products->total_income();
echo"<pre>";
print_r($data['total_income']);
}
The above code return an array like this.
Array
(
[0] => stdClass Object
(
[sub_product_price] => 1000
[quantity] => 1
)
[1] => stdClass Object
(
[sub_product_price] => 1000
[quantity] => 1
)
[2] => stdClass Object
(
[sub_product_price] => 50
[quantity] => 15
)
[3] => stdClass Object
(
[sub_product_price] => 500
[quantity] => 5
)
)
Now I want to get the [sub_product_price] and multiply that value with [quantity] .Then I want to get the array_sum. I don't have an idea how to do that. Could some one help me It would be grate ,
Cheers!! Rob
$sum = 0;
foreach ($array as $obj) {
$sum += ($obj->quantity * $obj->sub_product_price);
}
Add the all values after multiplication in an empty array and use array_sum() to get the final value.
$temp = array();
foreach($data['total_income'] as $in)
{
$temp[] = $in->sub_product_price * $in->quantity;
}
$total = array_sum($temp);
Explanation:
array_sum() returns the sum of all the values of an array.
Example,
$array = array(4,5,6);
echo array_sum($array); // 15
Im creating an array of products, each with an ID and score:
$savedProducts = array( 'product' => array("product_id" => $product_id,"product_score" => $score));
I want to be able to update the score by using the product_id as identifier.
I've tried:
foreach($savedProducts as $key => $val)
{
if ($val == $property_id )
{
$savedProducts[$key] = $score;
break;
}
}
Which keeps adding a new array item, rather than updating the original.
I think the issue is that my initial array setup then doesn't match the edited one.
Initial array:
Array
(
[product] => Array
(
[product_id] => 125026
[product_score] => 5
)
)
After trying to update score:
Array
(
[0] => Array
(
[product] => Array
(
[product_id] => 125026
[product_score] => 4
)
)
[1] => Array
(
[0] => Array
(
[product] => Array
(
[product_id] => 125026
[product_score] => 4
)
)
)
)
So it keeps addding elements, rather than updating the existing.
with PHP 5.5 use:
$savedProducts = array_column($savedProducts, NULL, 'product_id');
then you can access your product with:
$savedProducts[$product_id]
Please try this
foreach($savedProducts as $key => $val)
{
if($val['product_id'] == $property_id)
{
$savedProperties[$key]['product_score'] = $score;
break;
}
}