Getting array value by string key - php

I have array and i nees access to key:
var_dump($load);
array (size=18)
'4' =>
array (size=3)
0 => string '12.3%' (length=5)
1 => string '17.8%' (length=5)
2 => string '11.8%' (length=5)
'9' =>
array (size=3)
0 => string '5.0%' (length=4)
1 => string '8.2%' (length=4)
2 => string '8.9%' (length=4)
'24' =>
array (size=3)
0 => string '53.1%' (length=5)
1 => string '86.1%' (length=5)
2 => string '53.6%' (length=5)
'7' =>
array (size=3)
0 => string '95.1%' (length=5)
1 => string '90.1%' (length=5)
2 => string '80.7%' (length=5)
Hove a can get data on key?
var_dump($load["7"]);
var_dump($load["'7'"]);
var_dump($load['"7"']);
var_dump($load[7]);
All var_dump return null;
Var dump array keys, Keys match.
$keys=array_keys($load);
var_dump($keys);
array (size=18)
0 => string '14' (length=2)
1 => string '60' (length=2)
2 => string '19' (length=2)
3 => string '7' (length=1)
4 => string '15' (length=2)
5 => string '24' (length=2)
6 => string '1' (length=1)
7 => string '16' (length=2)
8 => string '5' (length=1)
9 => string '11' (length=2)
10 => string '17' (length=2)
11 => string '18' (length=2)
12 => string '3' (length=1)
13 => string '6' (length=1)
14 => string '2' (length=1)
15 => string '10' (length=2)
16 => string '9' (length=1)
17 => string '4' (length=1)

Related

Find and get array key from multilevel array from other array

i have this array structure, and i want to find and compare with other array that i have.
This is the array that i want search:
array (size=7)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
2 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
3 =>
array (size=9)
0 => string 'Verde' (length=5)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
An this is the array with search values:
Array
(
[0] => Blanco
[1] => 32GB
[2] => Plastico
)
I have the key and value, but i need find, in this example the main key is 1 in the long and main array, how can i get that?
PD: the number of search values are the numbers inside main arrays
Let's say that elements is the name of the array that you want to iterate over and find the index whose first three values match the search values. You can simply iterate over the elements, checking if the values match and if they do, then you can store the index in a variable called $wantedKey:
$target = array(
'0' => Blanco
'1' => 32GB
'2' => Plastico
);
$wantedKey = null;
foreach($elements as $key => $elem){
if($elem[0] == $target[0] && $elem[1] == $target[1] && $elem[2] == $target[2]){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
In case you have an arbitrary amount of values, you can use a foreach to iterate over them and check if they match:
foreach($elements as $key => $elem){
$sameValues = true;
foreach($target as $t_key => $t_value){
if($elem[$t_key] != $t_value){
$sameValues = false;
break;
}
}
if($sameValues){
$wantedKey = $key;
break;
}
}
echo $wantedKey;

Disallowed Key Characters in Codeigniter 2

I'm getting this error while submitting my form. Have read similar questions but couldn't find my problem.
I just disabled '_clean_input_keys' function temporarily and printed the $_POST to see what's wrong. but could not see anything weird.
Where should I search for this disallowed character?
This is my $_POST:
array (size=67)
'Naam' => string '1' (length=1)
'NaamKhanevadegi' => string '1' (length=1)
'NaamPedar' => string '1' (length=1)
'Shenasname' => string '1' (length=1)
'CodeMelli' => string '1111111222' (length=10)
'TarikhTavalodDay' => string '10' (length=2)
'TarikhTavalodMonth' => string '10' (length=2)
'TarikhTavalodYear' => string '1361' (length=4)
'Jensiat' => string '1' (length=1)
'MahalTavalod' => string '1' (length=1)
'Khedmat' => string '1' (length=1)
'SerialShenasname' => string '222222' (length=6)
'Taahol' => string '1' (length=1)
'SarparastHastam' => string '1' (length=1)
'TedadeTakafol' => string '2' (length=1)
'Ghad' => string '2' (length=1)
'Jesmani' => string '1' (length=1)
'Vazn' => string '2' (length=1)
'SabegheBimari' => string '1' (length=1)
'SabegheBime' => string '1' (length=1)
'Bimegar' => string '1' (length=1)
'BimeShode' => string '1' (length=1)
'ShomareBime' => string '2' (length=1)
'BimeDay' => string '28' (length=2)
'BimeMonth' => string '8' (length=1)
'BimeYear' => string '8' (length=1)
'ShoruMostamariDay' => string '8' (length=1)
'ShoruMostamariMonth' => string '8' (length=1)
'ShoruMostamariYear' => string '1360' (length=4)
'BimeBikari' => string '1' (length=1)
'PayanMostamariDay' => string '8' (length=1)
'PayanMostamariMonth' => string '8' (length=1)
'PayanMostamariYear' => string '1360' (length=4)
'Isargari' => string '1' (length=1)
'NameHemayati' => string '1' (length=1)
'PostalCode' => string '1212121212' (length=10)
'Tel' => string '' (length=0)
'Mobile' => string '09090909090' (length=11)
'Email' => string '' (length=0)
'Address' => string '' (length=0)
'MarjaNaam' => string '' (length=0)
'MarjaTel' => string '0909090909' (length=10)
'MarjaMobile' => string '09090909090' (length=11)
'Savad' => string '5' (length=1)
'Reshte' =>
array (size=5)
0 => string '0' (length=1)
1 => string '0' (length=1)
2 => string '0' (length=1)
3 => string '0' (length=1)
4 => string '0' (length=1)
'Moadel' =>
array (size=5)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '' (length=0)
4 => string '' (length=0)
'NoeDaneshgah' =>
array (size=4)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string '1' (length=1)
3 => string '1' (length=1)
'Mokaleme' =>
array (size=1)
''.$i.'' => string '1' (length=1)
'Neveshtan' =>
array (size=1)
''.$i.'' => string '1' (length=1)
'ZabanTasalot' =>
array (size=1)
''.$i.'' => string '0' (length=1)
'NoeMaharat' =>
array (size=1)
0 => string '' (length=0)
'DarajeMaharat' =>
array (size=1)
0 => string '' (length=0)
'Gavahiname' =>
array (size=3)
0 => string '1' (length=1)
1 => string '10' (length=2)
2 => string '11' (length=2)
'NaameMoasese' =>
array (size=1)
0 => string '' (length=0)
'SabegheJobTitle' =>
array (size=1)
0 => string '0' (length=1)
'SabegheKar' =>
array (size=1)
0 => string '' (length=0)
'DarkhastiJobTitle' =>
array (size=1)
0 => string '0' (length=1)
'NoeEstekhdam' => string '1' (length=1)
'ShahreDarkhasti1' => string '' (length=0)
'ShahreDarkhasti2' => string '' (length=0)
'ShahreDarkhasti3' => string '' (length=0)
'KhodEshteghali' => string '2' (length=1)
'Kharej' => string '2' (length=1)
'Keshvar1' => string '' (length=0)
'Keshvar2' => string '' (length=0)
'Comment' => string '' (length=0)
'Tamas' => string '1' (length=1)
I found it!
That little $ in $i made the problem.
I forgot some php tag there.
In most of the cases when you have a existing software and you are trying to deploy in a new enviroment this kind of error should be caused by the PHP property
short_open_tag
Check if you have enabled in your new enviroment. In other words PHP couldn't read the tags in your code.

fill missing dates for specific key and value

I have an array:
array (size=3)
0 =>
array (size=3)
'quantity' => string '20' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '2' (length=1)
1 =>
array (size=3)
'quantity' => string '10' (length=2)
'date' => string '2016-03-01' (length=10)
'eID' => string '2' (length=1)
2 =>
array (size=3)
'quantity' => string '15' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '1' (length=1)
how can i fill missing date for each 'eID'? so the output would be:
array (
0 =>
array (
'quantity' => string '20' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '2' (length=1)
1 =>
array (
'quantity' => null
'date' => string '2016-02-01' (length=10)
'eID' => string '2' (length=1)
2 =>
array (
'quantity' => string '10' (length=2)
'date' => string '2016-03-01' (length=10)
'eID' => string '2' (length=1)
...until 2016-12-01. 12 arrays for each 'eID' key with same value

PHP Set Array Key to Value Inside that Array [duplicate]

This question already has answers here:
How do you reindex an array in PHP but with indexes starting from 1?
(12 answers)
Closed 7 years ago.
I have an array which looks like (var_dump):
array (size=3)
0 =>
array (size=8)
'id' => string '1' (length=1)
'user_id' => string '64' (length=2)
'level' => string '1' (length=1)
'score' => string '9999' (length=4)
't1' => string '1' (length=1)
't2' => string '0' (length=1)
't3' => string '0' (length=1)
'attempts' => string '1' (length=1)
1 =>
array (size=8)
'id' => string '2' (length=1)
'user_id' => string '64' (length=2)
'level' => string '2' (length=1)
'score' => string '123456789' (length=9)
't1' => string '1' (length=1)
't2' => string '1' (length=1)
't3' => string '0' (length=1)
'attempts' => string '4' (length=1)
2 =>
array (size=8)
'id' => string '3' (length=1)
'user_id' => string '64' (length=2)
'level' => string '3' (length=1)
'score' => string '123456789' (length=9)
't1' => string '1' (length=1)
't2' => string '1' (length=1)
't3' => string '0' (length=1)
'attempts' => string '7' (length=1)
How can I change the key 0, 1, 2 etc.. to be the value of level inside that array?
For example:
1 =>
array (size=8)
'id' => string '1' (length=1)
'user_id' => string '64' (length=2)
'level' => string '1' (length=1)
'score' => string '9999' (length=4)
't1' => string '1' (length=1)
't2' => string '0' (length=1)
't3' => string '0' (length=1)
'attempts' => string '1' (length=1)
2 =>
array (size=8)
'id' => string '2' (length=1)
'user_id' => string '64' (length=2)
'level' => string '2' (length=1)
'score' => string '123456789' (length=9)
't1' => string '1' (length=1)
't2' => string '1' (length=1)
't3' => string '0' (length=1)
'attempts' => string '4' (length=1)
3 =>
array (size=8)
'id' => string '3' (length=1)
'user_id' => string '64' (length=2)
'level' => string '3' (length=1)
'score' => string '123456789' (length=9)
't1' => string '1' (length=1)
't2' => string '1' (length=1)
't3' => string '0' (length=1)
'attempts' => string '7' (length=1)
I have already tried renaming the key inside a forloop, that did not replace only the key, but instead, it replaced the whole array and left it blank.
Thanks
$arr = [ ['id'=>1], ['id'=>2], ['id'=>3]];
$new = [];
foreach($arr as $item)
$new[$item['id']] = $item;
print_r($new);
result
Array (
[1] => Array ( [id] => 1)
[2] => Array ( [id] => 2)
[3] => Array ( [id] => 3)
}

Return Values of Entire Table Using PDO

I want to run what I thought was going to be a simple query.
I want to return a database table just the values) in the same 2D style it is written in into $data[i][k]
I am using PHP's PDO, and the closest I have been able to get is:
$result=$database->query("SELECT * FROM `garage_statistics`",$bind = null,$fetch = 'FETCH_COLUMN');
$this->statement = $this->pdo->prepare($query);
$result = $this->statement->fetchAll(PDO::FETCH_COLUMN,1);
Which returns
0 => string 'Garage2' (length=1)
1 => string 'Garage3' (length=7)
2 => string 'Garage4' (length=7)
3 => string 'Garage6' (length=7)
4 => string 'Garage7' (length=7)
However I can't get it to loop for all the columns. I tried fetchall and got back:
array (size=10)
0 =>
array (size=14)
'name' => string 't' (length=1)
0 => string 't' (length=1)
'tablename' => string 't' (length=1)
1 => string 't' (length=1)
'numfloors' => string '3' (length=1)
2 => string '3' (length=1)
'status' => string '4' (length=1)
3 => string '4' (length=1)
'numspots' => string '0' (length=1)
4 => string '0' (length=1)
'spotsinuse' => string '0' (length=1)
5 => string '0' (length=1)
'time' => string '2012-12-07 13:47:13' (length=19)
6 => string '2012-12-07 13:47:13' (length=19)
1 =>
array (size=14)
'name' => string 'Garage 3' (length=8)
0 => string 'Garage 3' (length=8)
'tablename' => string 'Garage3' (length=7)
1 => string 'Garage3' (length=7)
'numfloors' => string '2' (length=1)
2 => string '2' (length=1)
'status' => string '3' (length=1)
3 => string '3' (length=1)
'numspots' => string '0' (length=1)
4 => string '0' (length=1)
'spotsinuse' => string '0' (length=1)
5 => string '0' (length=1)
'time' => string '2012-12-07 13:49:46' (length=19)
6 => string '2012-12-07 13:49:46' (length=19)
While this does contain all the data, it has so much stuff that I don't want.
Is there a simple way to get a basic 2D array of a table using PDO?
$result = $this->statement->fetchAll(PDO::FETCH_NUM);
Returns correct type

Categories