How to get the 0th index key values in array object - php

I have one array object in that object galleryImages i having like json string , in this galleryImages i want display onely the first image value , how can get the only first image value?
My array check below
$data=Array
(
[dbResultgallery] => Array
(
[0] => stdClass Object
(
[gallery_id] => 1
[className] => 1
[sectionName] => 1
[title] => Title 1
[description] => gfhfg
[galleryImages] => ["1.jpeg","2.jpg"]
[reg_on] => 2017-04-03 12:21:59
[created_by] => soupranjali#gmail.com
[school_id] => 2
[status] => 0
)
[1] => stdClass Object
(
[gallery_id] => 2
[className] => 1
[sectionName] => 1
[title] => Title 2
[description] => sdfdsfsdfsdfsdf
[galleryImages] => ["3.jpeg","4.jpg","5.jpg"]
[reg_on] => 2017-04-03 12:23:37
[created_by] => soupranjali#gmail.com
[school_id] => 2
[status] => 0
)
)
)
now i am using json_decode ,see below my code
foreach ($dbResultgallery as $gal) :
$role = json_decode($gal->galleryImages,true);
endforeach;
Now i am getting array format like
Array
(
[0] => 1.jpeg
[1] => 2.jpg
)
Array
(
[0] => 3.jpeg
[1] => 4.jpg
[2] => 5.jpg
)
Expected results
I need only the first images value like 1.jpeg and 3.jpeg, each array i need only the 0 th key values
Updated code
var_dump(json_decode($gal->galleryImages,true))
array(2) {
[0]=>
string(37) "6be1954c4bec91fe26fb7447fc551782.jpeg"
[1]=>
string(36) "153651d989591e76444c92cf037d5ac4.jpg"
}
array(3) {
[0]=>
string(37) "f356ceafa408e61cd1c62cfc39752b32.jpeg"
[1]=>
string(36) "a4fd29005696d92e0fc4cd3931454609.jpg"
[2]=>
string(36) "8eaaecdac1ff219192806acba7978a1b.jpg"
}

Change to this:
$role = json_decode($gal->galleryImages,true)[0];

foreach ($dbResultgallery as $gal) :
if(strpos(json_decode($gal->galleryImages,true) ,"jpeg")!== false){
echo 'found'; // do your stuff here
$role = json_decode($gal->galleryImages[0],true);
}
endforeach

Related

PHP Get values from nested array

I am new to PHP and Arrays, I am trying to get the values from an array. But no matter how I'm trying to do it, I can't get the value. What am I doing wrong?
The Array:
Array ( [playerinfo] => Array ( [rank] => Godfather [cash] => € 8,520,530 [weapon] => M-16 (29000) [health] => Array ( [width] => 100 [color] => green ) [wealth] => Too rich to be true [protection] => Bulletproof Humvee [plf] => Huge [plane] => Concorde [crew] => None [pbf] => Large [ship] => None ) [character] => Array ( [crime] => Array ( [0] => 120 [1] => 69 ) [gta] => Array ( [0] => 400 [1] => 70 ) [drugs] => Array ( [0] => 120 [1] => 2528 ) [airport] => Array ( [0] => 2700 [1] => 2529 ) [oc] => Array ( [0] => 86400 [1] => 1442364 ) [tr] => Array ( [0] => 10800 [1] => 1640016011 ) [plf] => Array ( [0] => 7200 [1] => 6712 ) [kill] => Array ( [0] => 3600 [1] => 1640019611 ) ) )
The way I tried to get the info:
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
foreach ($AccData as $playerinfo) {
echo $playerinfo['playerinfo']['rank'].'<br/>';
echo $playerinfo['character']['gta'].'<br/>';
}
EDIT:
The json string
{"playerinfo":{"rank":"Boss","cash":"€ 5,923,712","weapon":"M-16 (4500)","health":{"width":"100","color":"green"},"wealth":"Too rich to be true","protection":"Bulletproof Humvee","plf":"Huge","plane":"Concorde","crew":"None","pbf":"Large","ship":"None"},"character":{"crime":[120,122],"gta":[400,369],"drugs":[120,2582],"airport":[2700,2582],"oc":[86400,1640020450],"tr":[10800,1640016850],"plf":[7200,3935],"kill":[3600,1640020450]}}
Anyone knows how to do this ? For example I need the Concorde from plane in a variable and the time values from gta in a variable. And some more from this string.
So your first Part is okay and you rank you can just display like the first part as well
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['plane'].'<br/>';
echo $AccData['character']['gta'][0].'<br/>';
but the character is on the same level as playerinfo so you need to access it from AccData. also gta is an array like health, so you have to specify which value you want to show, the first so 0 or second which is 1

URL string compare not working in php

I have this URL "https://www.example.com/thymus_vulgaris?tag=Oil&page=2"
where I get /thymus_vulgaris?tag=Oil&page=2 from $_SERVER['REQUEST_URI'] in php
and I have also saved the same URL in database that I am getting in a variable
$abc = /thymus_vulgaris?tag=Oil&page=2
you can see both URL URI is same I am writing this code.
but comparison is not working please see the screenshot value for better understand
foreach ($redirects as $redirectlink) {
if('/thymus_vulgaris?tag=Oil&page=2' == $redirectlink['title']){
die;
}
}
if I do print_r($redirects) I got this array results see below
Array
(
[0] => Array
(
[title] => /thymus_vulgaris?tag=Oil&page=2
[url] => essential-oils
)
[1] => Array
(
[title] => /products/diffusers
[url] => diffusers
)
[2] => Array
(
[title] => /essential-oils/Essential-Oil-Kits/sleep_and_breathe
[url] => essential-oils
)
[3] => Array
(
[title] => /Solum%20Lux%20Telum
[url] => essential-oils
)
[4] => Array
(
[title] => /organic-boswellia-serrata
[url] => essential-oils
)
[5] => Array
(
[title] => /faqs
[url] => FAQ
)
[6] => Array
(
[title] => /clearance/refurbished-replacement-bottles/amp
[url] => accessories/bottles-and-caps/amp
)
[7] => Array
(
[title] => /refurbished-replacement-bottles/amp
[url] => accessories/bottles-and-caps/amp
)
)
if you see first array value is same as request $_SERVER['REQUEST_URI'] value.
Can anybody help me quickly?
I guess probably some invisible char, because of which == resulting false,
For example : DEMO
$ php -r '$f1="foo"; $f2="foo\0"; echo $f1.PHP_EOL; echo $f2.PHP_EOL; var_dump($f1); var_dump($f2); var_dump($f1==$f2);'
foo
foo
string(3) "foo"
string(4) "foo"
bool(false)
As you can see above, $f1 and $f2 both are not equal, but looks like same.
Output :

Access data inside stdObject

I have stdClass data after submitting the form:
"Array
(
[0] => stdClass Object
(
[username] => stdClass Object
(
[0] => 1001037
[1] => AARONJAMES
[2] => 1
[3] => ULTIMA (Red)
[4] => 1001037|1
[staff_id] => 1001037
[username] => AARONJAMES
[fkhouseid] => 1
[hname] => ULTIMA (Red)
[id] => 1001037|1
[$$hashKey] => object:13
)
[$$hashKey] => object:6
[points] => 50
)
[1] => stdClass Object
(
[username] => stdClass Object
(
[0] => 1001075
[1] => AARONKOH
[2] => 2
[3] => NACHOZZ (Purple)
[4] => 1001075|2
[staff_id] => 1001075
[username] => AARONKOH
[fkhouseid] => 2
[hname] => NACHOZZ (Purple)
[id] => 1001075|2
[$$hashKey] => object:14
)
[house] =>
[points] => 20
[$$hashKey] => object:306
)
)"
I want to access id variable inside username array to explode it later and insert data into database.
I tried to use to access it but it didnt work:
foreach ($persons as $key => $object) {
var_dump($persons[$key]->id);
}
How can I access id variable inside username?
Assuming $persons have array. You can try like below.
foreach ($persons as $key => $object) {
var_dump($object->username->id);
}
As per my comments you should try this
foreach ($persons as $key => $object) {
echo $object[$key]->username->id;
}
some details about array
$key is the index number of person array.
username is the stdClass Object so you can get like this ->username.

PHP Unserialise Within an Array

I have an array that has the following values
[0] => array(4) {
["sku"] => string(12) "WMS-M-VN-MRN"
["name"] => string(62) "Maroon V-neck Jumper"
["qty_ordered"] => string(6) "1.0000"
["product_options"] => string(533) "a:2:{s:15:"info_buyRequest";a:6:{s:4:"uenc";s:64:"aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,";s:7:"product";s:3:"780";s:8:"form_key";s:16:"gDXvCEtQOlRWihqc";s:15:"related_product";s:0:"";s:7:"options";a:1:{i:1970;s:5:"17201";}s:3:"qty";s:1:"1";}s:7:"options";a:1:{i:0;a:7:{s:5:"label";s:27:"PLEASE SELECT SIZE REQUIRED";s:5:"value";s:12:"36 inch (13)";s:11:"print_value";s:12:"36 inch (13)";s:9:"option_id";s:4:"1970";s:11:"option_type";s:9:"drop_down";s:12:"option_value";s:5:"17201";s:11:"custom_view";b:0;}}}"
}
Now is there a way to unserialise the "Product_options" field in-line, without having to break the array apart and rebuild it again?
This is the way. You just have to reassign the result of unserialize to the same array item.
<?php
$array = [
[
"sku" => "WMS-M-VN-MRN",
"name" => "Maroon V-neck Jumper",
"qty_ordered" => "1.0000",
"product_options" => 'a:2:{s:15:"info_buyRequest";a:6:{s:4:"uenc";s:64:"aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,";s:7:"product";s:3:"780";s:8:"form_key";s:16:"gDXvCEtQOlRWihqc";s:15:"related_product";s:0:"";s:7:"options";a:1:{i:1970;s:5:"17201";}s:3:"qty";s:1:"1";}s:7:"options";a:1:{i:0;a:7:{s:5:"label";s:27:"PLEASE SELECT SIZE REQUIRED";s:5:"value";s:12:"36 inch (13)";s:11:"print_value";s:12:"36 inch (13)";s:9:"option_id";s:4:"1970";s:11:"option_type";s:9:"drop_down";s:12:"option_value";s:5:"17201";s:11:"custom_view";b:0;}}}'
]
];
$array[0]["product_options"] = unserialize($array[0]["product_options"]);
echo "<pre>";
print_r($array);
echo "</pre>";
Output:
Array
(
[0] => Array
(
[sku] => WMS-M-VN-MRN
[name] => Maroon V-neck Jumper
[qty_ordered] => 1.0000
[product_options] => Array
(
[info_buyRequest] => Array
(
[uenc] => aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,
[product] => 780
[form_key] => gDXvCEtQOlRWihqc
[related_product] =>
[options] => Array
(
[1970] => 17201
)
[qty] => 1
)
[options] => Array
(
[0] => Array
(
[label] => PLEASE SELECT SIZE REQUIRED
[value] => 36 inch (13)
[print_value] => 36 inch (13)
[option_id] => 1970
[option_type] => drop_down
[option_value] => 17201
[custom_view] =>
)
)
)
)
)

php help: I have an array as a property of an object, and i cant seem to call the items in the array

I have a object 'aff' with an array 'bill' as a property.
var_dump($aff);
echo "<br/>";
print_r($aff->bill);
echo "<br/>";
var_dump($aff->bill);
echo "<br/>";
echo "<br/> $aff->bill[0]";
This returns:
affiliation Object ( [id] => 1 [afname] => TaylorGang [user1] => 18 [user2] => 19 [user3] => 0 [user4] => 0 [value1] => 65.432 [value2] => 52.14 [value3] => 0 [value4] => 0 [bill1] => Grape Soda [bill2] => Cable [bill3] => empty [bill4] => empty [due_date1] => 18 [due_date2] => 15 [due_date3] => 0 [due_date4] => 0 [startdate] => 2014-07-21 22:03:21 [user] => Array ( [0] => 18 [1] => 19 [2] => 0 [3] => 0 ) [bill] => Array ( [0] => Grape Soda [1] => Cable [2] => empty [3] => empty ) [value] => Array ( [0] => 65.432 [1] => 52.14 [2] => 0 [3] => 0 ) )
Array ( [0] => Grape Soda [1] => Cable [2] => empty [3] => empty )
array(4) { [0]=> string(10) "Grape Soda" [1]=> string(5) "Cable" [2]=> string(5) "empty" [3]=> string(5) "empty" }
Array[0]
I would expect that:
echo $aff->bill[0];
would return: 'Grape Soda'
If anyone could explain why this does not work and more importantly how I can get it to work it would be greatly appreciated.
Your string contains a complex expression, so you should wrap it up in curly braces:
echo "<br/> {$aff->bill[0]}";
Or, don't put the expression in the string (use concatenation):
echo "<br/> " . $aff->bill[0];
Without the curly braces, the [0] part is ignored as part of the expression, effectively leaving PHP to output the type of the object ("Array"), after which it treats the [0] as just part of the text, so the final output is effectively "<br/> " . "Array" . "[0]".
Read More

Categories