Array
(
[0] => Array
(
[TotalPaid] => 0
[Description] => One-time:
[PayStatus] => 0
[InvoiceTotal] => 34.78
[TotalDue] => 34.78
[JobId] => 66
[DateCreated] => 20150311T02:57:10
[Id] => 66
)
[1] => Array
(
[TotalPaid] => 0
[Description] => One-time:
[PayStatus] => 0
[InvoiceTotal] => 89.06
[TotalDue] => 89.06
[JobId] => 68
[DateCreated] => 20150311T02:58:27
[Id] => 68
)
)
i have ds array as a output this output is in a single variable ie $invoices.. i want each value in a separate variable...
When you store an array as a variable you can access each value in the array by adding the name of the array key between [] brackets after the variable name. like so...
$invoices[0]['Description']
If you wanted to loop through your invoices you could do...
foreach( $invoices as $invoice ) {
$description = $invoice['Description'];
}
If you need more info try the PHP manual - http://php.net/manual/en/language.types.array.php
EDIT: As Chris has pointed out the second example is only for use inside the loop because the example foreach would simply overwrite the variable at each iteration so you would need to do something immediately after its assignment within the iteration or it will be lost. However the first method would allow access to the value just increment the numeric key. from 0 to 1 and you would have both results assigned their own variable.
Hope that helps.
Dan
You could loop over the outer array, with a nested loop to go over the inner array. Within these, you can construct a string using the array keys (e.g. 'TotalPaid0', 'Description0', ... 'TotalPaid1', 'Description1', etc.) and then use Variable variables to store the values in.
http://php.net/manual/en/language.variables.variable.php
In response to user4501586 comment.
The code you posted to me makes no sense, you want to output to specific variables when actually to out put to that form you actually just need to call the relevant $array[0]['key'], $array[1]['key']
By this i mean select the appropriate array key and replace corresponding letter variable that you have chosen with it.
As has been suggested this is not a make me some code that works site, we are hear to help but you seem to want us to do it for you.
Also suggested. PHP Documentation you really should read up on it because i feel you do not yet grasp even the basics and that is something we cant help you with here.
Related
I've looked at a number of suggestions for this, and they seem to rely on array_combine() which unfortunately is not suitable, as they arrays need to stay separate for other functions.
The arrays are set out as per
Array ( [0] => 3 [1] => 1 [2] => 3 )
Array ( [0] => 194 [1] => 0 [2] => 452 )
When I read in the first array, I get the key as $key and everything works fine.
Whenever I try and access the second array the whole script just whites out the page.
I want the code to work simliar to this ...
$a2value = $a2[$key] => $value;
echo $a2value;
Then I can do SQL lookups using $a2value
Any help would be appreciated
Here Try this
let's suppose two arrays of same number of elements
$a1=[1,2,3];
$a2=[194,0,452];
//A Simple foreach
foreach($a1 as $key=>$a1value){
$a2value=$a2[$key];
//Perform Query here
}
Remember one thing number of elements of both array should always be equal or it will cause an error
If you aren't certain about the size of both arrays then replace the line with this code
$a2value=empty($a2[$key]) ? null : $a2[$key];
Hope this works !!!
Can someone tell me why the $_SESSION array is not getting modified by the code below. The object is to put the date in DoNotShipBefore of each record. It is obviously putting the value somewhere as the output of $Row['DoNotShipBefore'] shows the correct date. But, when the loop is done, it doesn't appear to have taken.
session_start();
print_r($_SESSION[cart_array]);
foreach($_SESSION[cart_array] as $Row) {
$Row['DoNotShipBefore'] = date("m/d/Y") ;
echo "<br>New Value of DoNotShipBefore (From cart_array): ". $Row['DoNotShipBefore'] ;
}
print "<br><br>";
print_r($_SESSION[cart_array]);
die();
Output follows:
Array ( [0] => Array ( [groupId] => 26141 [DoNotShipBefore] => 10/01/2017))
New Value of DoNotShipBefore (From cart_array): 07/06/2017
Array ( [0] => Array ( [groupId] => 26141 [DoNotShipBefore] => 10/01/2017))
// Should be (or what I want is):
Array ( [0] => Array ( [groupId] => 26141 [DoNotShipBefore] => 07/06/2017))
I'm having a hard time wrapping my head around how to address Arrays within the SESSION, so would really appreciate any help.
Except when the value is an object, when you assign a variable it makes a copy of the value (internally it uses copy-on-write for efficiency). So $Row is a copy of the row in $_SESSION. You're modifying that copy, not the original array.
You can use a reference variable to make it refer to the original array, by putting & before the variable.
foreach($_SESSION['cart_array'] as &$Row) {
What is cart_array? Maybe you should use $_SESSION['cart_array']? Any PHP notice?
There is nothing in your code that is actually assigning any values in the session array.
$_SESSION['some_var'] = "Some Value";
But you're not doing that so the session you have remains untouched.
Here is the Print_r of a $_SESSION variable. I am trying to access the value of user_id.
Array (
[userPieUser] => loggedInUser Object (
[email] => xxxxx#hotmail.com
[hash_pw] => xxxxxxxxx
[user_id] => 3
[clean_username] => scott
[display_username] => scott
[remember_me] => [remember_me_sessid] => c13348e6d296b8d96797eed631b20ad13f58e60af00760620327b019e4773c2d6
)
)
I have tried a dozen or so ways to get that value in PHP, however no luck. such as looping through and doing if ($key = 'user_id'){ echo $value } but that just returns the first element in the array. I'm sure it is rudimentary, however appreciate the help.
The one you're looking for is:
$_SESSION['userPieUser']->user_id
As it is part of the userPieUser object.
You should try:
echo $_SESSION['userPieUser']->user_id;
fyi: There is object withing array 'userPieUser'.
Access the variable like so:
echo $_SESSION['userPieUser']['user_id'];
PHP supports accessing object indicies like this within other object indicies.
I am pulling some data from a mysql table via the following:
$result = mysql_query("SELECT characters_ID, name, borndate, deathdate, marrieddate, ispregnant FROM characters WHERE isfemale='1'",$db);
$femaledata = array();
while ($row_user = mysql_fetch_assoc($result))
$femaledata[] = $row_user;
This gives me an array that looks like this:
Array (
[0] => Array ( [characters_ID] => 2 [name] => Helene [borndate] => 35 [deathdate] => 431 [marrieddate] => 157 [ispregnant] => 0 )
[1] => Array ( [characters_ID] => 4 [name] => Isabelle [borndate] => 161 [deathdate] => [marrieddate] => 303 [ispregnant] => 1 )
[2] => Array ( [characters_ID] => 7 [name] => Helene [borndate] => 326 [deathdate] => [marrieddate] => [ispregnant] => 0 )
[3] => Array ( [characters_ID] => 72 [name] => Faylinn [borndate] => 335 [deathdate] => [marrieddate] => [ispregnant] => 0 )
[4] => Array ( [characters_ID] => 74 [name] => Relina [borndate] => 349 [deathdate] => [marrieddate] => [ispregnant] => 0 )
)
Now I need to remove any characters who have a value for deathdate or ispregnant, and then I need to run some code on the others. For instance I need to grab the borndate value, compare it to the current date to find age, and based partly on age, I need to run code for each to determine if the character has become pregnant on the turn.
Apologies that this seems like a long-reaching question. Multidimensional arrays still seem to confound me.
Edit: (question needs to be more clear)
Can you please suggest the best way that I would either explode or break up the array, and then do conditional modification to the data, or instead how I could remove unneeded data and then do conditional modification to the data.
My ultimate output here would be taking suitable female characters (not dead or pregnant already), and based on their age, giving them a chance at becoming pregnant. If true, I'd throw some code back at the SQL database to update that character.
Thanks!
All the things you need could probably get done with SQL :
Now I need to remove any characters who have a value for deathdate or
ispregnant
Simply add some argument to your WHERE condition :
isPregnant IS NULL AND deathdate IS NULL
For instance I need to grab the borndate value, compare it to the
current date to find age
Depending of your field format the maths could be done in SQL , have look to the DATE function of mysql
Don't underestimate the power of your sql server , 99% of the time it is probably faster than php to work on data set.
Instead if immediately removing some rows from your array, try limiting the data you recieve through SQL.
You can loop through your array like this:
foreach($femaledata as $female)
{
echo $female['name'];
}
do you mean something like this?
$femaledata = array();
while ($row_user = mysql_fetch_assoc($result)) {
$ok = false;
// do you validation for every user
if($ok) array_push($femaledata,$row_user);
}
TJHeuvel gave you the right answer, and you should accept that answer. However, to inform: multidimensional arrays need not confound. Let me see if I can explain.
In PHP, you can put any object at all into an array, including other arrays. So, let's say you have an array that contains other arrays. When you iterate over that array using a looping construct (usually a foreach loop), each iteration of the loop will give you another array; if you want to access the elements of this sub-array, just loop over it. This is called a nested loop. Example:
$r = array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
foreach ($r as $cur) {
foreach ($cur as $num) {
echo $num;
}
}
In each iteration of the outer loop, $cur contains an array; the inner loop iterates over contents of this array. This technique allows you to process arrays of any dimension.
However, in your specific case, you don't need to use an inner loop to iterate over your subarrays. You only need to access certain elements of your subarrays by their keys, rather that processing all of them in turn. So, a simple foreach loop will do.
I am creating an array and want to delete the first element of the array and re-index it. From what I can tell, array_shift() is the right solution. However, it is not working in my implementation.
I have a member variable of my class that is defined as an array called $waypoint_city. Here is the variable output prior to shifting the array:
print_r($this->waypoint_city);
Result:
Array ( [0] => [1] => JACKSONVILLE [2] => ORLANDO [3] => MONTGOMERY [4] => MEMPHIS )
If I do the following, I get the correct result:
print_r(array_shift($this->waypoint_city));
Result:
Array ( [0] => JACKSONVILLE [1] => ORLANDO [2] => MONTGOMERY [3] => MEMPHIS )
However, if I try to reassign the result to the member variable it doesn't work... Anyone know why that is?
$this->waypoint_city = array_shift($this->waypoint_city);
If I try to print_r($this->waypoint_city) it looks like nothing is in there. Thanks to anyone who can save the hair that I haven't pulled out, yet.
array_shift[docs] changes the array in-place. It returns the first element (which is empty in your case):
Returns the shifted value, or NULL if array is empty or is not an array.
All you have to do is:
array_shift($this->waypoint_city);
That's because there IS nothing there. You have element 0 set to nothing, and array_shift returns the shifted element, which the first time through is null.
array_shift() gets its parameter as reference, so you should call array_shift() like this:
$shiftedElement = array_shift(&$this->waypoint_city);