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
Related
I have this code:
$order = wc_get_order( 988613 );
$product_array = array();
$daty = array();
$counter = 1;
foreach ($order->get_items() as $item_key => $item ){
$daty_dostaw = $item->get_meta('Daty dostaw');
$product_id = $item->get_product_id();
$daty_dostaw_explode = explode(',',$daty_dostaw);
$daty[$counter]['product_id'] = $product_id;
foreach($daty_dostaw_explode as $key => $data){
$daty[$counter]['data'][] = $data;
}
$counter++;
}
When I print it it shows me this
Array
(
[1] => Array
(
[product_id] => 988012
[data] => Array
(
[0] => 13-08-2022
[1] => 25-08-2022
[2] => 30-08-2022
)
)
[2] => Array
(
[product_id] => 988087
[data] => Array
(
[0] => 25-08-2022
[1] => 31-08-2022
[2] => 30-09-2022
)
)
)
I want to combine an array that have the same dates to display like this:
Array
( [1] => Array
(
[product_id] => array(988012, 988087)
[data] => Array
(
[0] => 25-08-2022
)
)
[2] => Array
(
[product_id] => 988012
[data] => Array
(
[0] => 13-08-2022
[1] => 30-08-2022
)
)
[3] => Array
(
[product_id] => 988087
[data] => Array
(
[0] => 31-08-2022
[1] => 30-09-2022
)
)
)
I want to merge the array those with the same dates. I don't know how to explain it exactly, above I showed what I would like to achieve. I have already written thousands of lines of code using foreach and have not been able to achieve this :(
Basically you want to convert product_id into an array if there are multiple products with the same date. I would search for such matches, create the merges, remove the elements from the individual occurrence or even the individual occurrence if it gets emptied. An example would be this:
$cachedDates = []; //Here we will store the dates we have gone through so far
foreach ($product_array as $key => $product) {
foreach ($product_array[$key]["data"] => $date) {
if (!in_array($date, $cachedDates)) {
//Creating a cache entry for this date
$cachedDates[$date] = ["keys" => [], "values" => []];
}
//Adding the key and product to the date's cache
$cachedDates[$date]["keys"][]= $key;
$cachedDates[$date]["values"][]= $product["product_id"];
}
}
//Adding the new items to $product_array and cleaning up items that become redundant
foreach ($cachedDates as $date => $entry) {
//We only merge items that have a not-unique date
if (count($entry["values"]) > 0) {
//adding a new item with the correct values and the date
$product_array[]= [
"product_id" => $entry["values"],
"data" => $date
];
//Cleanup
for ($index = 0; $index < count($entry["keys"]); $index++) {
//We remove the date from the data entry after finding its index
$removeIndex = array_search($date, $product_array[$entry["keys"][$index]]["data"]);
unset($product_array[$entry["keys"][$index]]["data"][$removeIndex]);
//If the data array is empty, then the whole product was already merged into some other item(s)
if (!count($product_array[$entry["keys"][$index]]["data"])) {
unset($product_array[$entry["keys"][$index]]);
}
}
}
}
The code above was untested, if you find issues with it and errors, then provide the appropriate PHP input instead of its print, even a json_encode result is good-enough
Nothing really does exactly what I want to achieve. I have the following array:
Array(
[0] => Array
(
[chicken] => 7
)
[1] => Array
(
[cheese] => 9
)
[2] => Array
(
[marinade] => 3
)
[3] => Array
(
[cookbook] => 7
)
[4] => Array
(
[chicken] => 11
)
[5] => Array
(
[cheese] => 6
)
[6] => Array
(
[marinade] => 12
)
)
I want to sum all values by their key. If the key is multiple times in the array, like chicken, I want to sum the values.
array
(
[chicken] => 18,
[cheese] => 16
... etc
)
So you'd first need a loop to iterate through the first array to get the second-level arrays. Then you can get the current key and value from each of those arrays, summing the values in a new array associated by the key.
// where the sums will live
$sum = [];
foreach($array as $item) {
$key = key($item);
$value = current($item);
if (!array_key_exists($key, $sum)) {
// define the initial sum of $key as 0
$sum[$key] = 0;
}
// add value to the sum of $key
$sum[$key] += $value;
}
Here simple example i hope it's help you.
$result = array();
foreach($data as $key => $value)
{
$valueKey = key($value);
$result[$valueKey] += $value[$valueKey];
}
Hello how to show values in table when i have a three arrays?
I have 3 three arrays: urls,name,rok
stdClass Object
(
[urls] => Array
(
[0] => Array
(
[key] => /film/Marsjanin-2015-715533
)
[1] => Array
(
[key] => /film/Pi%C4%99%C4%87dziesi%C4%85t+twarzy+Greya-2015-655761
)
)
[name] => Array
(
[0] => Array
(
[key] => Marsjanin
)
[1] => Array
(
[key] => Pięćdziesiąt twarzy Greya
)
)
[rok] => Array
(
[0] => Array
(
[key] => (2015)
)
[1] => Array
(
[key] => (2015)
)
)
)
I want show values in table like this but missing $valu3 to third row in table, how to do?
urls name rok
/film/Marsjanin-2015-715533 Marsjanin (2015)
/film/Pi%C4%99%C4%87dziesi%C4% Pięćdziesiąt twarzy Greya (2015)
This is my code:
foreach($results as $itemz => $valuez) {
foreach($valuez as $key1 => $value1) {
foreach($value1 as $key2 => $value2) {
if ($itemz=='urls'){
$valu1[] = str_replace("/film/", "film/", $value2);
}
if($itemz=='name'){
$valu2[] = $value2;
}
if($itemz=='rok'){
$valu3[] = $value2;
}
}
}
}
$Result = array_combine($valu1, $valu2);
//echo '<pre>'; print_r($Result); echo '</pre>';
foreach($Result as $key1 => $value1) {
echo '
<tr>
<th><input type="checkbox" value="'.$key1.'" name="list[]" /></th>
<th>'.$value1.' ()</th><th>'.$rating.'</th>
<th>'.$value1.'</th>
<th>'.$genres.'</th><th>Importuj</th>
</tr>';
}
You have an object that contains three arrays with identical index number, so you can perform only one foreach loop in this way:
foreach( $results->urls as $key => $array )
{
echo $results->urls[$key]['key'];
echo $results->name[$key]['key'];
echo $results->rok[$key]['key'];
}
All the first part of code can be deleted. In the example above I simply echo values, without reproducing your html structure, because I don't understand it (you have two $value1 and a lot of <th>... ), but you can easily change it in your preferred way, like this:
echo '<td>'.$results->urls[$key]['key'].'</td>';
( etc... )
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'];
}
}
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;
}
}