Change The Value of a Field in a Multidimensional Array in PHP - php

I have an array that looks like this
Array
(
[0] => Array
(
[Title] => The Title
[Price] => 700
[Quantity] => 2
)
)
Say I wanted to change the Quantity to 5 how would I do that if the array were stored in the variable $ItemArray?

Try $itemArray[0]['Quantity'] = 5;.
Basically, you have an array, $itemArray, which contains an associative array. To access that inside array, you simply use standard PHP array syntax: $itemArray[0].
Then, you need the Quantity field of that inner array. Using the nested array syntax, you append ['Quantity'] to the end of our previous statement, resulting in: $itemArray[0]['Quantity'].
At this point, you have the field you want, and you can use the normal = to set the field value.
$itemArray[0]['Quantity'] = 5;

thats very simple, try
$itemArray[0]["Quantity"] = 5;
What we're doing here is accessing the first index within $itemArray which is 0; 0 contains an array, so we now specify which part of 0 we want to access: Like this basically:
$array[index][innerarrayindex]

Related

How to extract value from COUNT array within array

How to extract only value from the below array
Array ( [COUNT(department_name)] => 3 )
this is a response from a db query the actual value came like this
Array ( [0] => Array ( [COUNT(department_name)] => 3 ) )
if we select the 0th index $department[0] it is giving this
Array ( [COUNT(department_name)] => 3 )
but now i need only the value from this i.e 3 how can i extract the value from this.
and i tried like this $department[0]['department_name'] and $department[0]['COUNT(department_name)'] but no result.
You can use this simply and it's working -
$department = var_export($department,true);
print_R($department[0]['COUNT(department_name)'])
var_export helps you get the structured information of a variable.
For more see this -
var_export
NOTE - I will suggest you to to get a more readable key from database.

FETCH_ASSOC removes indexes in the wrong array

I am creating a small web application and I am running on two arrays - one retrieved by simplexml_load_file and the other generated by query to database. I have a little problem with the latter - I need to create an associative array that I can reference through indexes. For that I do something like that.
$stmt->execute();
$db = $stmt->fetchAll(PDO::FETCH_ASSOC);
The array should therefore look like this:
Array (
['element'] => value,
)
It looks like this:
Array (
[0] => Array (
['element'] => value,
)
)
The only thing I noticed was that in the query the records are created so
Array (
[0] => Array (
['element'] => value,
[0] => value
)
)
My solution removes indexes inside the first array, in this example it will remove the line [0] => value, although the main index will remain. How can I change this to result in a full associative associative array? I mention that I want to display all the records from the query, the same fetch () works, although it displays one record (last) from the query.
Try to change
From
$db = $stmt->fetchAll(PDO::FETCH_ASSOC);
To
$db = $stmt->fetch(PDO::FETCH_ASSOC);
PDOStatement::fetch — Fetches the next row from a result set
While PDOStatement::fetchAll — Returns an array containing all of the result set rows
The 0 means row one. If you want to process only one row use the current function on the array which will give you expected results

Get the key value from a multidimensional array in PHP

I've the following type of array:
Array (
[2017-01-01] => Array (
[booking_nb] => 0
)
[2017-01-02] => Array (
[booking_nb] => 0
);
How can I get the value of booking_nb if the date is equal to 2017-01-02 ?
Do I need to loop into the array ?
Thanks.
Assuming 2017-01-02 is an array key, you can do the following:
$array['2017-01-02']['booking_nb']; // will return the value 0
However, I recommend that if you are only storing the booking_nb value inside of each sub-array (i.e. no other elements inside the sub-arrays), you simply store them like so:
array(
'2017-01-01' => 0,
'2017-01-02' => 0,
)
This way, you can select with the following:
$array['2017-01-01']; // gives 0
The simplicity gained from this method also has the downside of the inability to store additional data, so use according to your needs.

PHP -modify value of last accessed element in multidimensional associative array

I am reading a GEDCOM-formatted family tree flat file, and producing an array from the data for staging into table. If I encounter the values CONC <some value>, then, instead of adding an element, I need to append <some value> to the value of the last element that was just inserted (regardless of dimension depth).
I tried with current(...) etc but does this work for a multidimensional associative array?
please consider following element in an array:
[#N163#] => Array ( [INDI] => Array ( [TEXT] => Some data of this person) )
if the next line reads "1 CONC including his profession"
instead of adding a line as such
[#N163#] => Array (
[INDI] => Array ( [TEXT] => Some data of this person)
[INDI] => Array ( [CONC] => including his profession) )
I would like the array to look as follows:
[#N163#] => Array (
[INDI] => Array ( [TEXT] => Some data of this person including his profession) )
What I have researched thus far:
end($theArray)
to set pointer to last inserted element followed by $theArray[key($theArray)] = .... to update this element.
But I did not get this method to work for multidimensional arrays and/or it became really messy.
And:
merging two arrays using e.g. += notation,
but this only seems to overwrite a new element, not affect the last one, if keys are same
And:
examples with foreach calls, which does not help in my case.
Hope somebody can shed some light... many thanks!
When you adding $array[#N163#][INDI][TEXT] = 'smtng'; you can save position
$pos = &$array[#N163#][INDI][TEXT];
And if you need concatenate, write
$pos .= "concate line";

Store an array to an array key

I'm trying to store an array ($temp) into the $data array, where key is prices.
$data['prices'] = $temp;
However, PHP converts the array into string instead and is throwing me and error.
Notice: Array to string conversion
Is $data = array('prices' => $temp); the only solution?
edit:
I found my mistake. Yes, $data was used previously as a string that's why PHP is converting the input into string.
Problem 2, doing a print_r on $data['prices'] = $xml->result->Prices->Price, shows only 1 set of array. But I am able to retrive 2 sets of result by doing a foreach loop on $data['prices']. Why is that so?
Content of $temp http://pastebin.com/ZrmnKUPB
Let me be more clear..
The full xml object I'm trying to extract information from: http://pastebin.com/AuMJiyrw
I'm only interested in the price array (Price_strCode and Price_strDescription) and store them in $data['prices']. End result something like this:
Array(
[0] => (
[Price_strCode] => 0001
[Price_strDescription] => Gold
)
[1] => (
[Price_strCode] => 0002
[Price_strDescription] => Silver
)
)
Unless you are doing some other array to string conversion elsewhere, the array is actually being stored as another array.
$data['prices'] will be an array, which can be accessed as $data['prices']['key'].
This is not possible, I have been doing this always and it works fine. You must be doing something else somewhere which is causing your array to convert to strong. share your code here

Categories