PHP push new data into given array - php

I may seem having difficulty in understanding on how array works. So result, I can't work on the issue.
foreach($results as $key => $value){
$product_key = array(
'key' => $key
);
array_push($results, $product_key);
}
var_dump($results); exit;
Expected Output
array(2) {
[0]=>
object(stdClass)#21 (4) {
["items_id"]=>
string(1) "1"
["item_name"]=>
string(6) "laptop"
["price"]=>
string(5) "20000"
["quantity"]=>
string(2) "10"
["key"]=>
int(0)
}
[1]=>
object(stdClass)#22 (4) {
["items_id"]=>
string(1) "2"
["item_name"]=>
string(10) "smartphone"
["price"]=>
string(5) "10000"
["quantity"]=>
string(3) "200"
["key"]=>
int(1)
}
Unexpected Output
array(4) {
[0]=>
object(stdClass)#21 (4) {
["items_id"]=>
string(1) "1"
["item_name"]=>
string(6) "laptop"
["price"]=>
string(5) "20000"
["quantity"]=>
string(2) "10"
}
[1]=>
object(stdClass)#22 (4) {
["items_id"]=>
string(1) "2"
["item_name"]=>
string(10) "smartphone"
["price"]=>
string(5) "10000"
["quantity"]=>
string(3) "200"
}
[2]=>
array(1) {
["key"]=>
int(0)
}
[3]=>
array(1) {
["key"]=>
int(1)
}
}

You push new value (which is array) to the end of existsing array, what do you expect then?
If you want to modify current interated array value use this approach:
foreach($results as $key => $value) {
// use `->` as `$value` is object
$value->key = $key;
}
var_dump($results); exit;

Related

Sort multidimensional array by specific key value

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']);
});
}

php access property of object in array

I have a php variable that I got from a _POST. var_dump shows this:
array(9) { [0]=> array(2) { ["age"]=> string(2) "62"
["amount"]=> string(5) "10878" } [1]=> array(2) { ["age"]=> string(2) "63"
["amount"]=> string(5) "10878" } [2]=> array(2) { ["age"]=> string(2) "64"
["amount"]=> string(5) "10878" } [3]=> array(2) { ["age"]=> string(2) "65"
["amount"]=> string(5) "10878" } [4]=> array(2) { ["age"]=> string(2) "66"
["amount"]=> string(5) "10878" } [5]=> array(2) { ["age"]=> string(2) "67"
["amount"]=> string(5) "28416" } [6]=> array(2) { ["age"]=> string(2) "68"
["amount"]=> string(5) "28416" } [7]=> array(2) { ["age"]=> string(2) "69"
["amount"]=> string(5) "28416" } [8]=> array(2) { ["age"]=> string(2) "70"
["amount"]=> string(5) "28416" } }
I loop through the array but can't get the properties to print:
for ($i=0; $i<count($incomeSched); $i++) {
$age = $incomeSched[$i]->age;
$amt = $incomeSched[$i]->amount;
echo "age=$age, amount=$amt<br>";
}
age and amount are blank:
age=, amount=
There's a difference between associative arrays and objects.
$incomeSched[$i]->age;
is what you'd do to access the property of an object. For an associative array you'd want
$incomeSched[$i]["age"]
You can cast an array as an object if need be:
$obj = (object)$incomeSched;
learn more here:
PHP - associative array as an object
As far as I remember ->age is object syntax. You need array syntax which would be ['age'].
for ($i=0; $i<count($incomeSched); $i++) {
$age = $incomeSched[$i]['age'];
$amt = $incomeSched[$i]['amount'];
echo "age=$age, amount=$amt<br>";
}

unexpected changes - overriding object inside foreach php

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;
}
}
}

Data mismatch while dumping array in csv file

I have 7 array fetched by mysql and its working correctly each array have exactly 9 rows which are city_names but one of the array has only 7 rows since two values on the results where null . This causes confunsion while dumping file in csv. I use the below code. Is there any way to check it by city name without using foreach for eact array ?
for($i=0 ; $i <= $count-2 ; $i++)
{
$data[] = $all_restaurants_opr_no_temp_off[$i]['city_name'];
$data[] = $all_restaurants_opr[$i]['total'];
$data[] = $all_restaurants_opr_no_temp_off[$i]['total'];
$data[] = $restaurants_opr_temp_off[$i]['total'];
$data[] = $restaurants_opr_operations_closed[$i]['total'];
$data[] = $restaurants_opr_automated[$i]['total'];
$data[] = $restaurants_opr_automated_working[$i]['total'];
$data[] = $restaurants_opr_online_payment[$i]['total'];
$data[] = $restaurants_opr_online_payment_with_tempoff[$i]['total'];
$data[] = $restaurants_opr_atleast_one_order[$i]['total'];
fputcsv($fp, $data,",");
unset($data);
}
Array 1:
array(9) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(3) "687"
}
[1]=>
array(2) {
["city_name"]=>
string(9) "Hyderabad"
["total"]=>
string(2) "16"
}
[2]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(3) "568"
}
[3]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(3) "139"
}
[4]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(3) "232"
}
[5]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(2) "57"
}
[6]=>
array(2) {
["city_name"]=>
string(5) "Noida"
["total"]=>
string(2) "77"
}
[7]=>
array(2) {
["city_name"]=>
string(9) "Faridabad"
["total"]=>
string(1) "4"
}
[8]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(3) "113"
}
}
Array 2:
array(9) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(3) "674"
}
[1]=>
array(2) {
["city_name"]=>
string(9) "Hyderabad"
["total"]=>
string(2) "16"
}
[2]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(3) "547"
}
[3]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(3) "135"
}
[4]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(3) "202"
}
[5]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(2) "56"
}
[6]=>
array(2) {
["city_name"]=>
string(5) "Noida"
["total"]=>
string(2) "77"
}
[7]=>
array(2) {
["city_name"]=>
string(9) "Faridabad"
["total"]=>
string(1) "4"
}
[8]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(3) "111"
}
}
Array 3:(Problem is here)
array(6) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(2) "13"
}
[1]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(2) "21"
}
[2]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(1) "4"
}
[3]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(2) "30"
}
[4]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(1) "1"
}
[5]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(1) "2"
}
}
I'm not sure I understand the question 100%, but perhaps you can test each value if it is null and put a default value there if it is, for example, for this row, you could use this above your for loop:
foreach($badArray as $b){
$reformattedArray[$b['city_name']] = $b['total'];
}
then replace the bad array line with this in your for loop
data[] = is_null($reformattedArray[$all_restaurants_opr_no_temp_off[$i]['city_name']]) ? 'Default' : $reformattedArray[$all_restaurants_opr_no_temp_off[$i]['city_name']];

getting a unique values from array

I currently have an array that looks like the one below but I only want to display show results in the array from which are unique (description), I presume with array_unique? but I keep getting the same results?
Here is my array:
$taglist = array(5) {
[0]=> array(5) {
["id"]=> string(2) "27"
["page_id"]=> string(2) "18"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
[1]=> array(5) {
["id"]=> string(2) "29"
["page_id"]=> string(2) "18"
["description"]=> string(3) "Tutorials"
["slug"]=> string(3) "tutorials"
["visibility"]=> string(7) "visible"
}
[2]=> array(5) {
["id"]=> string(2) "31"
["page_id"]=> string(2) "21"
["description"]=> string(3) "tag"
["slug"]=> string(3) "tag"
["visibility"]=> string(7) "visible"
}
[3]=> array(5) {
["id"]=> string(2) "32"
["page_id"]=> string(2) "21"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
}
Here is my while:
$items = array();
$results = $taglist;
foreach ($results as $result)
{
$items[]= $result['description'];
$items = array_unique($items);
}
echo '<ul>';
while ($tag_item = current($items))
{
echo '<li>'.$tag_item['description'].'</li>';
next($items);
}
echo '</ul>';
$taglist = array(
0 => array('description'=>'one'),
1 => array('description'=>'two'),
2 => array('description'=>'one'),
3 => array('description'=>'three'),
4 => array('description'=>'one'),
);
// echo var_export($taglist, 1); // uncomment to see the diff vs var_dump()
foreach($taglist as $tag){
$new[] = $tag['description'];
}
var_dump(array_unique($new));

Categories