I want to compare the key values in the array and if a match is found to increase the value of the element.
For this I use code:
// var_dump $all_array
array(2) {
[0]=> array(6) {
[0]=> string(8) "art_7880"
[1]=> string(11) "Арт.7880"
[2]=> string(1) "1"
[3]=> NULL
[4]=> string(45) "png"
[5]=> int(1372269755)
}
[1]=> array(6) {
[0]=> string(8) "art_7880"
[1]=> string(11) "Арт.7880"
[2]=> string(1) "1"
[3]=> NULL
[4]=> string(45) "png"
[5]=> int(1372269874)
}
}
// var_dump $count
array(2) { [0]=> string(2) "10" [1]=> string(1) "1" }
// var_dump $product
array(2) { [0]=> string(10) "1372269755" [1]=> string(10) "1372269874" }
$count=$_POST['count'];
$product=$_POST['product'];
$count_arr_products=count($product);
for ($i=0; $i<=$count_arr_products; $i++){
foreach ($all_array as $keys => $elms) {
if ($product[$i]==$elms[5]) {
if($count[$i] > 0) {
$elms[2] = $count[$i];
} else {
unset($keys);
}
}
}
}
but step $elms[2] = $count[$i]; not work - in result value $elms[2] not change...
You need to majke $elms a reference. By default it will be a copy of the sub-array, so assignment won't update the original array.
$all_array = array(array("art_7880", "Арт.7880", "1", NULL, "png", 1372269755),
array("art_7880", "Арт.7880", "1", NULL, "png", 1372269874));
$count = array("10", "1");
$product = array("1372269755", "1372269874");
$count = array("10", "1");
$product = array("1372269755", "1372269874");
$count_arr_products = count($product);
for($i=0; $i<$count_arr_products; $i++){ // Use < not <=
foreach ($all_array as $keys => &$elms) { // Use a reference so we can update it
if ($product[$i]==$elms[5]){
if ($count[$i] > 0) {
$elms[2] = $count[$i];
} else {
unset($all_array[$keys]); // not unset($keys)
}
}
}
}
var_dump($all_array);
Output:
array(2) {
[0]=>
array(6) {
[0]=>
string(8) "art_7880"
[1]=>
string(11) "Арт.7880"
[2]=>
string(2) "10"
[3]=>
NULL
[4]=>
string(3) "png"
[5]=>
int(1372269755)
}
[1]=>
&array(6) {
[0]=>
string(8) "art_7880"
[1]=>
string(11) "Арт.7880"
[2]=>
string(1) "1"
[3]=>
NULL
[4]=>
string(3) "png"
[5]=>
int(1372269874)
}
}
Related
I'm working on algorithm to display my events on the website.
I want to sort my multidimensional array by specific key value.
My array:
["2022-02-28"]=>
array(1) {
[0]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
["2022-03-01"]=>
array(2) {
[2]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
[3]=>
array(3) {
["post_id"]=>
string(4) "3784"
["time"]=>
string(5) "13:00"
["priority"]=>
string(1) "0"
}
}
["2022-03-03"]=>
array(1) {
[5]=>
array(3) {
["post_id"]=>
string(4) "3663"
["time"]=>
string(5) "13:06"
["priority"]=>
string(1) "1"
}
}
}
I want to sort the array by "time" key value. So for example at this index :
["2022-03-01"]=>
array(2) {
[2]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
[3]=>
array(3) {
["post_id"]=>
string(4) "3784"
["time"]=>
string(5) "13:00"
["priority"]=>
string(1) "0"
}
}
I want first 13:00 to appear then 16:05. Thank you for your help in advance! :)
Try with this:
<?php
$arr = array();
$arr["2022-02-28"] = [
array("post_id"=>"3656", "time"=>"16:05", "priority"=>"0"),
array("post_id"=>"4856", "time"=>"13:05", "priority"=>"3")];
$arr["2022-03-01"] = [
array("post_id"=>"3656", "time"=>"16:05", "priority"=>"0"),
array("post_id"=>"3636", "time"=>"13:05", "priority"=>"1")
];
foreach($arr as $key => $value){
usort($value, function($a,$b){
return strtotime($a["time"])>strtotime($b["time"]);
});
$arr[$key] = $value;
}
echo "<pre>";
var_dump($arr);
echo "</pre>";
Output:
array(2) {
["2022-02-28"]=>
array(2) {
[0]=>
array(3) {
["post_id"]=>
string(4) "4856"
["time"]=>
string(5) "13:05"
["priority"]=>
string(1) "3"
}
[1]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
["2022-03-01"]=>
array(2) {
[0]=>
array(3) {
["post_id"]=>
string(4) "3636"
["time"]=>
string(5) "13:05"
["priority"]=>
string(1) "1"
}
[1]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
Use usort for define custom sort.
function time_sort(array $arr){
usort($arr, function($a, $b){
return strcmp($a['time'], $b['time']);
});
}
I've got array of object form database like below:
array(3) {
[1]=>
array(2) {
[0]=>
object(stdClass)#99 (3) {
["id"]=>
string(2) "42"
["name"]=>
string(4) "NAME1"
["type"]=>
string(1) "6"
}
[1]=>
object(stdClass)#98 (3) {
["id"]=>
string(3) "146"
["name"]=>
string(3) "STH1"
["type"]=>
string(1) "2"
}
}
[2]=>
array(2) {
[0]=>
object(stdClass)#97 (3) {
["id"]=>
string(2) "422"
["name"]=>
string(4) "NAME2"
["type"]=>
string(1) "3"
}
[1]=>
object(stdClass)#96 (3) {
["id"]=>
string(3) "16"
["name"]=>
string(3) "STH2"
["type"]=>
string(1) "2"
}
}
[3]=>
array(2) {
[0]=>
object(stdClass)#95 (3) {
["id"]=>
string(2) "11"
["name"]=>
string(4) "NAME3"
["type"]=>
string(1) "5"
}
[1]=>
object(stdClass)#94 (3) {
["id"]=>
string(3) "69"
["name"]=>
string(3) "STH3"
["type"]=>
string(1) "3"
}
}
}
And if i want to add the same object to the next array and change value of its type, i override the current object. How can i fix it? My foreach loop below:
foreach($events as $key => $event){
foreach($event as $k => $v){
if($v->type == 6){
$v->type = "0";
$events[$key+1][] = $v;
$v->type = "6";
}
}
}
If my guess what you are trying to achieve is right i would go like this
foreach($events as $key => $event){
foreach($event as $k => $v){
if($v->type == 6){
$tmp = $v;
$tmp->type="0";
$events[$key+1][] = $tmp;
}
}
}
guys i have multidimensional array which i got it from var_dump of $menu_order with this following array :
array(5) {
[0]=>
array(1) {
[0]=>
array(1) {
["variant_name"]=>
string(5) "Spicy"
}
}
[1]=>
array(2) {
[0]=>
array(1) {
["variant_name"]=>
string(5) "Spicy"
}
[1]=>
array(1) {
["variant_name"]=>
string(5) "small"
}
}
[2]=>
array(2) {
[0]=>
array(1) {
["variant_name"]=>
string(5) "Salty"
}
[1]=>
array(1) {
["variant_name"]=>
string(6) "medium"
}
}
[3]=>
array(2) {
[0]=>
array(1) {
["variant_name"]=>
string(12) "Mix of Herbs"
}
[1]=>
array(1) {
["variant_name"]=>
string(5) "large"
}
}
[4]=>
array(0) {
}
}
from that array, i need to get the variant_name become variant_menu_id with this following code :
foreach ($menu_order as $item) {
if (isset($item[0]["variant_name"])) {
foreach($item as $value) {
$variant_id[] = $this->Main_home_m->m_get_choice_id($value["variant_name"]);
}
} else {
$variant_id[] = array();
}
}
the model of m_get_choice_id have this following code :
Function m_get_choice_id($variant_name){
$this->db->select("variant_menu_id");
$this->db->from("uhd_variant_menu");
$this->db->where("variant_name",$variant_name);
$query = $this->db->get();
return $query->row_array();
}
the variant_id will be return to this multidimensional array :
array(8) {
[0]=>
array(1) {
["variant_menu_id"]=>
string(1) "3"
}
[1]=>
array(1) {
["variant_menu_id"]=>
string(1) "3"
}
[2]=>
array(1) {
["variant_menu_id"]=>
string(1) "6"
}
[3]=>
array(1) {
["variant_menu_id"]=>
string(1) "4"
}
[4]=>
array(1) {
["variant_menu_id"]=>
string(1) "7"
}
[5]=>
array(1) {
["variant_menu_id"]=>
string(1) "5"
}
[6]=>
array(1) {
["variant_menu_id"]=>
string(1) "8"
}
[7]=>
array(0) {
}
}
but i want the result variant_id become this multidimensional array :
array(5) {
[0]=>
array(1) {
[0]=>
string(1) "3"
}
[1]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "6"
}
[2]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "7"
}
[3]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "8"
}
[4]=>
array(0) {
}
}
guys can you help me how to get the multidimensional array?
thank you (:
Alternatively, you can create a temporary container holding the ids with an array. After getting them all as an array, push that whole batch inside a parent container:
$result = array();
foreach ($menu_order as $item) {
$temp = array(); // initialize temporary storage
if (isset($item[0]["variant_name"])) {
foreach($item as $value) {
$variant = $this->Main_home_m->m_get_choice_id($value["variant_name"]);
$temp[] = $variant['variant_menu_id']; // push single id into temporary storage
}
}
$result[] = $temp; // push ending batch
}
I am trying to loop through a multidimensional array but in the foreach loop it just outputs error
index 'name' not found. index 'calories' not founder
foreach($responsex['foods'] as $fx5)
{
echo($fx5['name']);
echo($fx5['calories']);
}
Response: i.e. $responsex
array ( 'encodedId' => '4H8xxx', 'displayName' => 'sam', )array(3) {
["foods"]=> array(3) { [0]=> array(5) { ["isFavorite"]=> bool(false)
["logDate"]=> string(10) "2016-04-15" ["logId"]=> int(7139364449)
["loggedFood"]=> array(10) { ["accessLevel"]=> string(6) "PUBLIC"
["amount"]=> int(2) ["brand"]=> string(0) "" ["calories"]=> int(574)
["foodId"]=> int(536497687) ["locale"]=> string(5) "en_AU"
["mealTypeId"]=> int(7) ["name"]=> string(14) "Potato Pudding"
["unit"]=> array(3) { ["id"]=> int(91) ["name"]=> string(3) "cup"
["plural"]=> string(4) "cups" } ["units"]=> array(8) { [0]=> int(6754)
[1]=> int(91) [2]=> int(256) [3]=> int(279) [4]=> int(226) [5]=>
int(180) [6]=> int(147) [7]=> int(389) } } ["nutritionalValues"]=>
array(6) { ["calories"]=> int(574) ["carbs"]=> float(49.16) ["fat"]=>
float(34.98) ["fiber"]=> float(3.6) ["protein"]=> float(16.1)
["sodium"]=> int(1524) } } [1]=> array(5) { ["isFavorite"]=>
bool(false) ["logDate"]=> string(10) "2016-04-15" ["logId"]=>
int(7138517833) ["loggedFood"]=> array(10) { ["accessLevel"]=>
string(6) "PUBLIC" ["amount"]=> int(1) ["brand"]=> string(0) ""
["calories"]=> int(359) ["foodId"]=> int(535239347) ["locale"]=>
string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=> string(54) "Fish,
Noodles and Vegetables in Cheese Sauce (Mixture)" ["unit"]=> array(3)
{ ["id"]=> int(91) ["name"]=> string(3) "cup" ["plural"]=> string(4)
"cups" } ["units"]=> array(8) { [0]=> int(6837) [1]=> int(91) [2]=>
int(256) [3]=> int(279) [4]=> int(226) [5]=> int(180) [6]=> int(147)
[7]=> int(389) } } ["nutritionalValues"]=> array(6) { ["calories"]=>
int(359) ["carbs"]=> float(28.01) ["fat"]=> float(14.05) ["fiber"]=>
float(2.9) ["protein"]=> float(29.08) ["sodium"]=> int(534) } } [2]=>
array(5) { ["isFavorite"]=> bool(false) ["logDate"]=> string(10)
"2016-04-15" ["logId"]=> int(7138326866) ["loggedFood"]=> array(10) {
["accessLevel"]=> string(6) "PUBLIC" ["amount"]=> int(1) ["brand"]=>
string(0) "" ["calories"]=> int(157) ["foodId"]=> int(536493638)
["locale"]=> string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=>
string(11) "Cashew Nuts" ["unit"]=> array(3) { ["id"]=> int(226)
["name"]=> string(2) "oz" ["plural"]=> string(2) "oz" } ["units"]=>
array(4) { [0]=> int(226) [1]=> int(180) [2]=> int(147) [3]=> int(389)
} } ["nutritionalValues"]=> array(6) { ["calories"]=> int(157)
["carbs"]=> float(8.56) ["fat"]=> float(12.43) ["fiber"]=> float(0.9)
["protein"]=> float(5.17) ["sodium"]=> int(3) } } } ["goals"]=>
array(2) { ["calories"]=> int(1161) ["estimatedCaloriesOut"]=>
int(1411) } ["summary"]=> array(7) { ["calories"]=> int(1090)
["carbs"]=> float(85.73) ["fat"]=> float(61.46) ["fiber"]=> float(7.4)
["protein"]=> float(50.35) ["sodium"]=> int(2061) ["water"]=> int(0) }
}
you can recursively iterate through the arrays and print them as follows as key value pairs.
<?php
//initially call the function
print_array($responsex);
function print_array($array){
foreach($array as $key=>$value){
//recursively print the array
if(is_array($value)){
echo("Array : ".$key."\n");
print_array($value);
}
else{
echo($key." => ".$value);
}
}
}
?>
You can define additional tasks other than printing them with the above code.
Edit:
if you are sure that the array is two dimensional, no need to go recursively.
<?php
//initially call the function
print_array($responsex);
//if you are sure that the array is two dimensional, no need to go recursively.
function print_array($array){
foreach($array as $key=>$value){
if(is_array($value)){
if($key==="foods"){
var_dump($array[$key]);
}
}
else{
echo($key." => ".$value);
}
}
}
Use this way..
<?php
$keys = array_keys($data);// put your array name as a place of $data
$iterations = count($array[$keys[0]]);
for($i = 0; $i < $iterations; $i++) {
$data = array();
foreach($array as $key => $value) {
$data[$key] = $value[$i];
}
print_r($data);
}
?>
I have this Array:
array(66) {
[0]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105a"
}
[1]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105b"
}
[2]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105c"
}
[3]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105d"
}
[4]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "117b"
}
[5]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "117c"
}
[6]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "123a"
}
[7]=> array(2) {
["location"]=> string(10) "Whateverelse" ["bsid"]=> string(4) "123b"
}
}
How can I count how many Breakwater's I have and how many Whateverelse's and get something like this:
array(2) {
[0]=> array(2) {
["Breakwater"]=> string(2) "20"
} [1]=> array(2) {
["Whateverelse"]=> string(1) "1"
}
}
Just loop around the original array, and each time a location is hit, increment a counter in the locations array where the index is the location.
$loc = array();
foreach($arr as $value) {
$location = $value['location'];
if(isset($loc[$location])) {
$loc[$location]++;
} else {
$loc[$location] = 1;
}
}
print_r($loc);
Will output
array
(
["Breakwater"] => 7,
["Whateverelse"] => 1
}
I got this finally :
$output = array("Breakwater" => 0, "Whateverelse" => 0);
foreach ($array as $val) {
$output["Breakwater"] += ($val["location"] == "Breakwater") ? 1 : 0;
$output["Whateverelse"] += ($val["location"] == "Whateverelse") ? 1 : 0;
}
var_dump($output);