0 key element in mysql_fetch_array($result,MYSQL_BOTH) - php

Can anyone explain why running mysql_fetch_array($result,MYSQL_BOTH) on a result sometimes means that key[0] = 0 whilst at other times it ends up with a string/value. See example array below:
Array
(
[0] => 0
[groupTitle] => TEST 5
)
In this instance I would expect 0 to equal 'TEST 5'

The reason was because I wanted Mysql to return '0' as one of the elements/fields. So my query read something like this:
SELECT field1,field2,'0',field3 FROM table
What PHP did was returned the first element in the array with a value '0' but then set the first string key with the correct first element. I believe that may be a bug.

Related

PHP using key of one array to access same key position on another array

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 !!!

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.

Change The Value of a Field in a Multidimensional Array in 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]

Strange behavior with isset() returning true for an Array Key that does NOT exist

I have the following array called $fruits:
Array
(
[response] => Array
(
[errormessage] => banana
)
[blah] => Array
(
[blah1] => blahblah1
[blah2] => blahblah2
[blah3] => blahblah3
[blah4] => blahblah4
)
)
Yet when I do:
isset($fruits['response']['errormessage']['orange'])
It returns true!
What on earth would cause such a strange behavior and how can I fix this?
Thanks!
It just boils down to PHP's crazy type system.
$fruits['response']['errormessage'] is the string 'banana', so you're attempting to access a character in that string by the ['orange'] index.
The string 'orange' is converted to an integer for the purposes of indexing, so it becomes 0, as in $fruits['response']['errormessage'][0]. The 0th index of a string is the first character of the string, so for non-empty strings it's essentially set. Thus isset() returns true.
I don't know what you're trying to do in the first place so I can't offer any "fix" for this. It's by design.
[n] is also a way to access characters in a string:
$fruits['response']['errormessage']['orange']
==
$fruits['response']['errormessage'][0] // cast to int
==
b (the first character, at position 0) of 'banana'
Use array_key_exists, possibly in combination with is_array.
to fix
if (is_array($fruits['response']['errormessage'])
&& isset($fruits['response']['errormessage']['orange'])) { .. }

Categories