I have read other questions regarding reading arrays in PHP and implemented the suggested solutions but these do not seem to work for me...
My array looks like this:-
{
[0]=> array(6)
{ ["name"]=> string(9) "Test04Feb" [0]=> string(9) "Test04Feb"
["paymentvalue"]=> string(6) "500.00" [1]=> string(6) "500.00"
["transactiondate"]=> string(19) "2020-02-05 13:29:37" [2]=> string(19) "2020-02-05 13:29:37"
}
[1]=> array(6)
{ ["name"]=> string(9) "Test04Feb" [0]=> string(9) "Test04Feb"
["paymentvalue"]=> string(7) "1500.00" [1]=> string(7) "1500.00"
["transactiondate"]=> string(19) "1970-01-01 05:30:00" [2]=> string(19) "1970-01-01 05:30:00"
}
[2]=> array(6)
{ ["name"]=> string(9) "Test04Feb" [0]=> string(9) "Test04Feb"
["paymentvalue"]=> string(5) "90.00" [1]=> string(5) "90.00"
["transactiondate"]=> string(19) "2020-02-05 18:12:18" [2]=> string(19) "2020-02-05 18:12:18"
}
}
And none of these work for me:-
$stmt1->execute([$myname]);
$value = $stmt1->fetchAll();
...
foreach ($stmt1 as $row1) {
echo $row1[0]->name;
echo $row1['0']['transactiondate'];
echo $row1[0]['paymentvalue'];
}
Any help would be much appreciated, thank you.
Using
foreach ($stmt1 as $row1) {
$row1 is each individual record from the database. Therefore, rather than having to use [0] as in your code, you should simply be using...
echo $row1->name;
etc.
To check this for yourself, use something like print_r($row1); in the loop to see what each loop has to work with.
In your case $row1 is an array as indicated in your dump. In foreach should use $value and not $stmt. Then:
foreach ($value as $row1) {
echo $row1['name'];
}
P.S. If you want to get only associative keys, use like this:
$value = $stmt1->fetchAll(PDO::FETCH_ASSOC);
Try this if you are using foreach
foreach ($stmt1 as $row1) {
echo $row1['name'];
}
or you can use
echo $stmt1[0]['name'];
Hope this helps you
What is the shortest way to change multiple values in array key that has a specific value in it?
For example, I have this array:
array(2) {
[0]=>
array(5) {
["state"]=>
string(6) "active"
["payer_mail"]=>
string(12) "mail#none.com"
["start"]=>
string(12) "06/05/2015"
["end"]=>
string(8) "08/07/2017"
["price"]=>
string(8) "45.00"
["keystring"]=>
string(8) "493457025"
}
[1]=>
array(6) {
["place"]=>
string(2) "47"
["state"]=>
string(8) "canceled"
["payer_mail"]=>
string(12) "mail#none.com"
["start"]=>
string(9) "20/8/2014"
["end"]=>
string(10) "20/10/2017"
["price"]=>
string(5) "95.00"
["keystring"]=>
string(8) "34879205"
}
}
And I want to change the "state" value of the key that has "34879205" value for his "keystring" sub-key.
You can use foreach():-
foreach($array as &$value) {
if ($value['keystring'] == '34879205'){
$value['state'] = "";//change value here
}
}
Output:- https://eval.in/838789
Another way i found is:
$key = array_search('34879205', array_column($array, 'keystring'));
$array[$key]['state'] = 'newvalue';
You can use array_map function
$result = array_map(function ($element) {
if ($element['keystring'] === '34879205') {
$element['state'] = 'new_state';
}
return $element;
}, $array);
I want to loop through the following array.
array(2) {
[0]=> array(6) {
[0]=> string(10) "excel_id "
[1]=> string(12) "excel_name "
[2]=> string(11) "excel_email"
[3]=> string(13) "excel_address"
[4]=> string(12) "excel_giveby"
[5]=> string(16) "excel_accesories"
}
[1]=> array(6) {
[0]=> float(111)
[1]=> string(10) "sh1exname1"
[2]=> string(11) "sh1exemail1"
[3]=> string(12) "sh1exaddress"
[4]=> string(12) "sh1exgivenby"
[5]=> string(16) "sh1exaccesorries"
}
}
can anyone help me?
If you tried anything at all you should have found foreach.
this will loop through all of your array values.
You can simply loop through a multi-dimensional array by using a multi-dimensional foreach like this:
foreach($array as $parentValue)
{
foreach($parentValue as $childValue)
{
// your code
}
}
try this:
foreach ($main_array as $outer_entry){
foreach($outer_entry as $entity){
print_r($entity);
}
}
need some help with splitting mysql single column query array into different php variables here.
example:
here's the query, it's pretty simple to be honest.
but, i'm running out of ideas right now.
$string = "select Description from tblQuestion
where Employeeid = '$param'"
$query = $this->db->query($string);
$result = return $query->result_array();
btw, i am using Codeigniter and i tried to var_dump and the results are like this.
array(9) { [0]=> array(1) { ["Description"]=> string(5) "tidak" } [1]=> array(1) { ["Description"]=> string(5) "tidak" } [2]=> array(1) { ["Description"]=> string(5) "tidak" } [3]=> array(1) { ["Description"]=> string(5) "tidak" } [4]=> array(1) { ["Description"]=> string(5) "tidak" } [5]=> array(1) { ["Description"]=> string(5) "tidak" } [6]=> array(1) { ["Description"]=> string(5) "tidak" } [7]=> array(1) { ["Description"]=> string(5) "tidak" } [8]=> array(1) { ["Description"]=> string(5) "tidak" } }
i tried to use json_encode and the result is
[{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"}]
the question is.
how do i convert this stack of arrays into different variables like this?
$var0 = "tidak";
$var1 = "tidak";
$var2 = "tidak";
$var3 = "tidak";
and on and on....
thanks in advance.
cheers!
Put the results in a foreach loop and assign the values to a dynamic variable...
sample code like,
foreach($results as $key=>$val){
$str = 'var'.$key;
$$str = $val['Description'];
}
echo $var0;
Im new to json & php and I'm having some issues with json into php string
My json string looks like this
{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}
So Far i have created my string
$json_raw = '{"status":"OK","cards": [{"id":100001,"name": .....
Decoded the json
$arr = json_decode($json_raw, TRUE);
I var_dump($arr);
then it returns
array(2) { ["status"]=> string(2) "OK" ["cards"]=> array(4) { [0]=> array(8) { ["id"]=> int(100001) ["name"]=> string(6) "batman" ["image"]=> int(11111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T11:37:07Z" } [1]=> array(8) { ["id"]=> int(100002) ["name"]=> string(8) "superman" ["image"]=> int(111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:30:09Z" } [2]=> array(8) { ["id"]=> int(100003) ["name"]=> string(8) "catwoman" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:39:42Z" } [3]=> array(8) { ["id"]=> int(100004) ["name"]=> string(4) "bane" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-09-08T12:56:04Z" } } }
Now all I want to do is be able to use this data
e.g if name = batman then
I know this is a stupid question but I am struggling :(
Thank in Advance
json_decode() with TRUE as second parameter gives you an associative array. You need to access the correct index to do what you want.
To list the complete associative array with nice formatting, you can do:
echo '<pre>', print_r($arr), '</pre>';
Now, to access the name in your array:
$man = $arr['cards'][0]['name'];
To check if it's Batman (yay!):
if( isset($man) && $man == 'batman' ) {
# code ...
}
For getting the name of all similar names:
$man = $json['cards']['0']['name'];
for ($i=0; $i < count($json['cards']); $i++) {
echo $json['cards'][$i]['name']."\n";
}
See it live!
when you got the array
$arr = json_decode($json_raw, TRUE);
then check if cards key exist
if(array_key_exists('cards', $arr)){
foreach($arr['cards'] as $key=>$val){
echo $key; ///name, id..
echo $val; /// batman,...
if($key == 'name' && $val =='batman'){
//-------do your stuff
}
}
}
Try with:
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
EDIT:
Ok, so this worked for me using code above, try it yourself if you want:
<?php
$json_raw = '{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}';
$arr = json_decode($json_raw, TRUE);
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
?>