Illegal string offset for array within an array - php

I have the main array called $quizzes which contains the collection of $Quiz.
Each $Quiz has the following fields: $Quiz['correct'] gives me the number of correct questions.
I can get the number of correct questions for 12th quiz using $quizzes[12]['correct']
However, since these quizzes are not displayed in order, I decided to define a new array:
$listoftests = array('$quizzes[30]','$quizzes[51]');
In my head, $listoftests[0]['correct'] should be equal to $quizzes[30]['correct'] but it's giving me
Warning: Illegal string offset 'correct' in
/demo.php
on line 14 $
when I try to echo $listoftests[0]['correct'];

In this $listoftests = array('$quizzes[30]','$quizzes[51]'); These are considered as two strings $quizzes[30] and $quizzes[51]. You should remove single quotes ' and try again.
Change this:
$listoftests = array('$quizzes[30]','$quizzes[51]');
To:
$listoftests = array($quizzes[30],$quizzes[51]);

By doing this
$listoftests = array('$quizzes[30]','$quizzes[51]');
you created array of 2 strings. That's it. Not an array of arrays.
You should remove quotes. And you can also use isset() to check if array item exists.

Remove the single quote and it shell work fine $listoftests = array($quizzes[30],$quizzes[51]);

#GRS you can do it in two way:
//case one where the element will be of string type
<?php
$quizzes[30] = 3;
$quizzes[51] = 32;
$listoftests = array("$quizzes[30]","$quizzes[51]");
var_dump($listoftests);
//or
//case two where the element will be of integer type
<?php
$quizzes[30] = 3;
$quizzes[51] = 32;
$listoftests = array($quizzes[30],$quizzes[51]);
var_dump($listoftests);

Related

How do i have to define the if condition to check if the array_key length =13?

At the moment my "method" checks if the array_key is in the array, i need to add that if the key length is 13 it is okay, but when it is longer it has to delete the like first 5 numbers of the list. My code searches the price according to my key out of the database, and compares it to a list where the "keys" are a bit different. So to make it easier to understand:
The key in my database is: 2840529503100
The key in the table where i have to get the price from is: 000002840529503100
So what i basically need to do is get a substring from the "list" or "table" which takes the last 13 numbers because the right keys of my database are always 13 numbers long.
$teile = array();
while ($dbRow = $sqlres->fetch_assoc()) {
$teile[$dbRow['nummer']] = $dbRow;
if (array_key_exists($fileRow[0], $teile)) {
If your first five 0 values is constant means you can use substr() like this:
$str = '000002840529503100';
echo $str2 = substr($str, 5);
Another method for substr() is only keep the last last 13 digits like this:
$str = '000002840529503100';
echo $str2 = substr($str, -13);
Else to remove the first 0 values you can use ltrim() like this:
echo ltrim('000002840529503100', '0');
Note: without quotes these two functions won't work.
As an alternative to Nawins solution you can also use PHP Typecasting to automatically remove the leading zeros.
NOTE: for my example; I can't see which variable you're trying to change. But assumed the first...
Thus:
$fileRow[0] = "000002840529503100"
(int)$fileRow[0] = "2840529503100";
This would make:
if (array_key_exists((int)$fileRow[0], $teile)) {
...
}
BEWARE:
This example doesn't change the stored value of $fileRow[0] so using it as a future key reference (~print $array[$fileRow[0]]) will still use the longer number until the (int) version has been set as the variable/key value, example:
$fileRow[0] = (int)$fileRow[0];

Php generate multiple array() without duplicate value and match with other array?

I have 2 data.
$dataA = 01,05,07;
$dataB = 01,07;
Now, I would like to explode the $dataA and combine back with only 2 string. like below:
$explode = explode(',',$dataA);
/*combine back get the max combination without duplicate string, example for the $dataA.
My suspect is: first array (01,05), second array(05,07), third array(07,01), I don't need (05,01) since first array have both value.
*/
After that, if the combination array value match with the $dataB (no matter which the value position is), then do something like:
$array[0] = '01,05';
$array[1] = '05,07';
$array[2] = '07,01';
//since $array[2] value = $dataB (no need to check specific position); so I would like to do as below
if(in_array(third array,$dataB)){ //use in_array or?
//do something
}
Is it any array function can done this?

array_multisort Converts string index into an integer index

I have two arrays:
$info = array();
$submitted = array();
I declared an assignment below:
$info['idnumber'] = 10066;
$submitted[$info['idnumber']] = 'Wow';
array_multisort($submitted);
After doing so, displayed $submitted array.
foreach($submitted as $key => $row){
echo $key;
}
Why does it display 0 instead of 10066? I tried tweaking my code to:
$info['idnumber'] = 10066;
$submitted[(string)$info['idnumber']] = 'Wow';
or
$info['idnumber'] = 10066;
$submitted[strval($info['idnumber'])] = 'Wow';
Still it displays 0. What shall I do to display 10066 as the index of the $submitted array?
Update:
I found out it's a known bug of array_multisort, but still it has no solutions. Any idea how to fix ]it?
As you pointed out it is a known behaviour.
The solution was proposed in the discussion
For the moment I'm going to say prefix all your array keys with an extra 0 (or any non-numeric) to force their casting as strings.
When you try to cast integer into string like this:
(string)$info['idnumber']
you still get the integer, because you have a valid number as a string.
So you need to have a string as with some prefix. Prefix can be a 0 or any other non-numeric character. like an i
$info['idnumber'] = '010066';
Or
$info['idnumber'] = 'i00066';
This will return the exact index.

How does PHP determine whether a variable is an array or a string?

I'm confused as to how PHP determines whether a variable is a string or an array. It seems to depend on the operators being used.
Here's an example:
<?php
$z1 = "abc";
$out = "";
for ($i = 0; $i < strlen($z1); $i++)
{
// $out[$i] = $z1[$i];
$out = $out.$z1[$i];
}
print $out;
?>
In the above version $out becomes a string (print $z1 shows "abc"). However, if I use the first line $out[$i] = $z1[$i];, $out becomes an array.
Can someone please clarify why this happens, and if its possible to access a string's characters with square brackets without converting the output to an array?
The definition of a string in PHP is considered a set of data writen in linear format (i.e: $var = "username=SmokeyBear05,B-day=01/01/1980";)
An array however is a set of data broken down into several parts. A sort of list format if you will. As an example I've written the data string from before, into an array format...
Array(['username']=>"SmokeyBear05", ['B-day']=>"01/01/1980")
Now strings are generally defined as such: $var="Your String";
Arrays however can be written in three different formats:
$var1 = array('data1','data2','data3');
$var2 = array('part A'=>'data1','part B'=>'data2','part C'=>'data3');
The output of var1 starts the index value at 0. The output of var2 however, sets a custom index value. Now the third way to write an array (least common format) is as such:
$var[0]="data1";
$var[1]="data2";
$var[2]="data3";
This takes more work, but allows you to set the index.
Most web developers working with PHP will set data from an external source as a string to deliver it to another PHP script, and then break it down into an array using the explode() function.
When you define variable $out = "", for loop doesn't understand this variable as string value. If you set $out[$i] value, by default, it was treated as an array.
If you want to get the output result as string value, you can define $out = "a" to make sure it's a string variable.

PHP get the 5 characters after the 'strstr' match

The variable $page includes a webpage (full HTML file), but I want to filter that file on this line:
<a href="http://[website]/[folder]/
And I want that the 5 characters after parsed in a string.
But that strings is multiple times inside $page, so the numbers has to be stored in an array too.
So if a match is found with <a href="http://[website]/[folder]/23455, how do I get the '23455' into $nums[0]
And if another match is found with <a href="http://[website]/[folder]/12345, the '12345' will be put into $nums[1]
Take a look at http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/ maybe this regular expression works for you:
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
From what I understand from your question you are just trying to get the ending piece of the url and then do a comparison then store the value in an array? I believe this code should do the trick.
$nums = new array(); //declare your array
$a = new SimpleXMLElement('Your Link'); //create a new simple xml element
$a = $a['href'];
$array = explode("/",$a); //split the string into an array by using the / as the delimiter
if($array[count($array)-1] == '23455') { //get the count of the array and -1 from the count to get the last index then compare to required number
$nums[0] = '23455'; //store value in array
} else if ($array[count($array)-1] == '12345'{
$nums[1] = '12345'; //
}

Categories