Add values into same array key - php

I'm sure this is a silly question, but I don't understand the reason. Why I cannot add values into the same array key? The values were overwritten.
$rows=array();
while($row = mysqli_fetch_array($result)) {
$rows[strval($row['year'])]=array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']);
}

For now I'm assuming you want a multidimensional array with the year and inside each row with the month and satisfaction as array. You just have to append it by using $rows[strval($row['year']][], since each array key is unique and you will just overwrite it currently.
So your code would look like:
$rows=array();
while($row = mysqli_fetch_array($result)) {
$rows[strval($row['year'])][] = array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']);
}

In PHP (and almost all programming languages) array indexes are unique. This means that $arr["teletubbies"] cannot be "lala" and "noonoo" at the same time. This is for the simple reason that if you would call $arr["teletubbies"] at a later time PHP cannot know whether you meant "lala" or "noonoo" then.
To solve this issue (you want some things ordered by their year, e.g. 2014) it is advisable to make the value correlated to key 2014 a new array, thus creating a multidimensional array. You seem to understand that, but your implementation is wrong, as you are now just changing $rows[strval($row['year'])] to something new instead of appending the new values.
There are multiple solutions to this problem to do so:
Solution 1:
$rows=array();
while($row = mysqli_fetch_array($result)) {
//The syntax [] means: insert after the last defined key, more information at: http://php.net/manual/en/language.types.array.php look for $arr[] = 56;
$rows[strval($row['year'])][] = array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']);
}
Solution 2:
$rows=array();
while($row = mysqli_fetch_array($result)) {
//Make sure $rows[strval($row['year'])] is an array
if (!isset($rows[strval($row['year'])])) $rows[strval($row['year'])] = Array();
//array_push pushes a new array element to the last element of an array, documentation here: http://php.net/manual/en/function.array-push.php
array_push($rows[strval($row['year'])],array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']));
}
There are probably several other solutions aswell, but you get the idea.
To call one of the $rows[strval($row['year'])] later, you must make sure you call the newly created indexes! For example:
//Three indexes! 1st is the year, 2nd is the nth child you added and third is Array("month" => "..", "satisfaction" => "...");
print_r($rows["2014"][5]);
or
echo $rows["2014"][3]["month"]." ".$rows["2014"][3]["satisfaction"];

Array index is unique. The code below should work for your case.
$rows=array();
while($row = mysqli_fetch_array($result)) {
$rows[strval($row['year'])][] = array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']);
}

hope I understood your question correctly. One array key can store only one value (or one array of values) so:
$array[$key] = $value; //will overwrite what is under that key
$array[$key][] = $value //will add a value as a new sub-array node resulting in:
array(
'key' => array (
'0' => 'value',
),
)

Related

Foreach loop over array of objects Laravel

I'm receiving this array of objects and need to iterate over it, but the problem is that I need to get the value of the next item when its iterating, to compare the value of the current object with the next one, and if it's a different value, split this array in two.
So, I was doing it with next() php function:
//looking for the next register in the array
$next = next($finances);
//first array if exist different values
$aiuaEd = [];
//second array if exist different values
$aiua = [];
foreach ($finances as $finance) {
if ($finance->cnpj <> $next->cnpj) {
$aiua[] = $finance;
} else {
$aiuaEd[] = $finance;
}
}
This code works fine at some point, but then I got this error:
Trying to get property 'cnpj' of non-object in
I don't know why sometimes works well and sometimes don't, debugging this variables, I found that if my array have 2 objects inside only, when I'm looping over it, the $next->cnpj variable came as empty, and sometimes don't.
Can someone help me with this?
I solved it with a different approach, instead of using php next(), I first loop over this array saving the cnpj's into an array.
$cnpjs = [];
foreach($finances as $finance){
$cnpj[] = $finance->cnpj;
}
Then I use array_unique() to group this 2 differents CNPJ's and sort() to get the correct keys order.
//grouping cnpjs as unique, should exist only 2 keys
$cnpj = array_unique($cnpj);
//sort array keys to get in order
sort($cnpj);
Then I iterate over my $finances array again, but now I'm counting if this $cnpj array has more than 2 positions, which means that I have to split this data in two differents arrays.
foreach($finances as $finance){
if(count($cnpj) > 1){
if($finance->cnpj == $cnpj[1]){
$aiua[] = $finance;
}else{
$aiuaEd[] = $finance;
}
}else{
$aiuaEd[] = $finance;
}
}
I'm pretty sure that this is not the best approach for that problem, or at least the most optimized one, so I'm open for new approach's suggestions!
Just posting how I solved my problem in case anyone having the same one.
Notice that this approach is only usable because I know that will not exist more than 2 different's CNPJ's in the array.

Array ( [0] => Array ... problems extracting array values

I am trying to run a function that gets information from a DB and returns an array of the values, so I can then extract it on the page.
Inside the function, after my query I have the following code:
$example_array = array();
while ($row = mysql_fetch_assoc($query) {
$example_array[] = $row;
}
return $example_array;
And there ends my function. Outside of it, I have this:
extract($example_array);
And I would assume I could then directly echo any of the variables that were previously in $example_array, e.g. <?= $example_var ?> but they do not contain any data.
Running print_r($example_array); gives an array that looks like this:
Array ( [0] => Array ( [example_var] => Example String ) )
The start of that code makes me think my array is somehow "lost" inside another array's first ([0]) value, and as such is not extracting correctly.
Have I gone about adding data to that initial $example_array incorrectly?
When you do $example_array[] = $row;, you assign the current row to a new index of $example_array. If you want to access it like $example_array['row_name'], you'd have to assign it like this:
$example_array = $row;
But when you do this, $example_array will be overwritten until it has reached the last row (which means that $example_array will always contain the last row from the query). If you just want the first row, you can do this and skip the whole while loop:
$example_array = mysql_fetch_assoc($query);
Maybe :
$example_array = array();
while ($row = mysql_fetch_assoc($query) {
array_push($example_array, $row['exemple_var']);
}
return $example_array;
The issue is that mysql_fetch_array would have meant $row['row_name'] was valid.
As you added $row to the array $example_array, you now need to access it via it's array id too, such as;
$example_array[0]['row_name'], $example_array[1]['row_name'] etc.
What exactly are you trying to achieve? May be easier to offer assistance if we know.

Find index of value in associative array in php?

If you have any array $p that you populated in a loop like so:
$p[] = array( "id"=>$id, "Name"=>$name);
What's the fastest way to search for John in the Name key, and if found, return the $p index? Is there a way other than looping through $p?
I have up to 5000 names to find in $p, and $p can also potentially contain 5000 rows. Currently I loop through $p looking for each name, and if found, parse it (and add it to another array), splice the row out of $p, and break 1, ready to start searching for the next of the 5000 names.
I was wondering if there if a faster way to get the index rather than looping through $p eg an isset type way?
Thanks for taking a look guys.
Okay so as I see this problem, you have unique ids, but the names may not be unique.
You could initialize the array as:
array($id=>$name);
And your searches can be like:
array_search($name,$arr);
This will work very well as native method of finding a needle in a haystack will have a better implementation than your own implementation.
e.g.
$id = 2;
$name= 'Sunny';
$arr = array($id=>$name);
echo array_search($name,$arr);
Echoes 2
The major advantage in this method would be code readability.
If you know that you are going to need to perform many of these types of search within the same request then you can create an index array from them. This will loop through the array once per index you need to create.
$piName = array();
foreach ($p as $k=>$v)
{
$piName[$v['Name']] = $k;
}
If you only need to perform one or two searches per page then consider moving the array into an external database, and creating the index there.
$index = 0;
$search_for = 'John';
$result = array_reduce($p, function($r, $v) use (&$index, $search_for) {
if($v['Name'] == $search_for) {
$r[] = $index;
}
++$index;
return $r;
});
$result will contain all the indices of elements in $p where the element with key Name had the value John. (This of course only works for an array that is indexed numerically beginning with 0 and has no “holes” in the index.)
Edit: Possibly even easier to just use array_filter, but that will not return the indices only, but all array element where Name equals John – but indices will be preserved:
$result2 = array_filter($p, function($elem) {
return $elem["Name"] == "John" ? true : false;
});
var_dump($result2);
What suits your needs better, resp. which one is maybe faster, is for you to figure out.

Sorted Array of Objects

Is there any way to maintain a sorted array of objects?
For example, if I have an object with properties ID, Date, Name and a collection of these objects:
$col = array();
public function addNewObject($id, $date, $name)
{
$col[] = new Object($id, $date, $name);
//but instead of appending, it should place it by Name desc
}
If I call something like getObjects, it would return the items in the collection by Name desc.
I think there were some answers for getting objects back in a sorted order, but for efficiency, I would think it be better to sort at insert as the "sort by" variable in my case will never change.
UPDATE:
So based on the comments, I should resort the whole array each time something is added but that seems a bit memory intensive...
Since the array would always be in sorted order to start out with I can identify the location where I want to insert by traversing the array (would this be efficient, is there a better way?). Once I find that how could I "insert" a new object into the array?
I do not imagine that the array will be very large but I would like to implement this the most efficient way possible.
If you're not keen on resorting the array after you add (although I'd recommend it; realistically this wont be a performance issue and it keeps the code readable.
However, if you definitely don't want to do this then you can, as you said, traverse the array and find out where to insert:
$col = array();
public function addNewObject($id, $date, $name){
//Find the index to insert at
$index = 0;
foreach($col as $i => $item){
if($item->name > $name){
//This item is after the item we want to insert.
//Use the previous index and stop traversing
break;
}
$index = $i;
}
$col = array_splice($col, $index, 0, new Object($id, $date, $name));
}
Using array_splice to insert at an arbritary position thanks to https://stackoverflow.com/a/3797526/505722
this is a good example of a function that sorts an array from whatever key you want it sorted by
http://www.php.net/manual/en/function.sort.php#99419
in your example you should run it like :
array_sort($col, 'Name', SORT_DESC));
take in mind that every time you add a new item to the array the whole array is sorted each time

PHP Array from MySQL is not empty but has an empty entry - how to avoid

I am trying to check whether an entry exists (one or more) in our database. However, even when I know there are no entries, I am getting an array which has a first entry of zero. Therefore it is not empty and I am not getting what I need.
Here's my code:
<?php
$query = mysql_query("SELECT * FROM table WHERE column = $yfbid_number AND timestamp BETWEEN (NOW()- Interval 1 DAY) AND NOW()");
$array[] = array();
while ($row = mysql_fetch_assoc($query)){
$array[] = $row['column'];
}
?>
When doing print_r on an array which should be empty, I am getting: ( [0] => Array ( ) ) and therefore count is 1 and not zero, which messes up my code. Any ideas how to get to a truly empty array in this situation?
I'd rather not delete this entry but avoid it in the first place, because in most use cases I will get either an empty array or one that only has one entry (a real entry), in which case I will want to easily distinguish between the two. (as it is now, both give a count of 1 entry, which is very bad for our porpuses). Thanks.
Change:
$array[] = array();
to
$array = array();
With your version, if $array doesn't already exist, PHP will first create an array, then append an empty array to it. So you end up with a 1 element array whose only member is an empty array.
$array[] = array(); should be $array = array(). Right now, you are appending an array element to an array that is initialized. Turn notices on and you'll get a complaint about an undefined variable (probably).
I suggest you first do a SELECT COUNT(*) to determine how many entries you'll get. Then you KNOW that the result will be a useful answer, and can make or not make a subsequent query on the basis of your result.

Categories