Display array elements in smarty - php

I have merged two mysql results :
while($rs_1 = mysql_fetch_array($r1))
{
$arr1[] = $rs_1;
}
while($rs_2 = mysql_fetch_array($r2))
{
$arr2[] = $rs_2;
}
$resN = array_merge($arr1,$arr2);
var_dump($resN) shows the following result :
array(5) {
[0] => array(4) {
[0] => string(6) "Petric"
["bz_pro_first_name"] => string(6) "Petric"
[1] => string(8) "Naughton"
["bz_pro_last_name"] => string(8) "Naughton"
}
[1] => array(4) {
[0] => string(6) "Nitish"
["bz_pro_first_name"] => string(6) "Nitish"
[1] => string(12) "Dolakasharia"
["bz_pro_last_name"] => string(12) "Dolakasharia"
}
[2] => array(4) {
[0] => string(6) "Martin"
["bz_pro_first_name"] => string(6) "Martin"
[1] => string(3) "Rom"
["bz_pro_last_name"] => string(3) "Rom"
}
[3] => array(4) {
[0] => string(5) "Steve"
["bz_pro_first_name"] => string(5) "Steve"
[1] => string(5) "Wough"
["bz_pro_last_name"] => string(5) "Wough"
}
[4] => array(4) {
[0] => string(3) "Liz"
["bz_pro_first_name"] => string(3) "Liz"
[1] => string(6) "Hurley"
["bz_pro_last_name"] => string(6) "Hurley"
}
}
I am supposed to display them in smarty so :
assign_values('rand_ad',$resN);
Now I tried to display in smarty like this :
{foreach name=outer item=pro from=$rand_pro}
{foreach key=key item=item from=$pro}
{$key}: {$item}<br />
{/foreach}
{/foreach}
It displays the results but serially. I need to extract the values in some positions . So how can I extract the values eg first name, last name etc ?

You didn't give an example output of what you wanted, so I'll take a guess. Assuming you are looking for the following output for the array above:
0: Petric Naughton<br />
1: Nitish Dolakasharia<br />
2: Martin Rom<br />
3: Steve Wough<br />
4: Liz Hurley<br />
You'd could do this:
{foreach from=$rand_pro item=pro key=pro_key}
{$pro_key}: {$pro.bz_pro_first_name} {$pro.bz_pro_last_name}<br />
{/foreach}
If I am assuming incorrectly, please update your question with the output you are expecting to be more clear.

Use {foreach} to display array in smarty.
and for database connection use ADODB.
In php file:
type $smarty->assign->("arrname",$aodDBconn->Execute($sql)).
It will work.

Related

Get Unique from Multi-dimentional array based on one key - PHP [duplicate]

This question already has answers here:
Filter/Remove rows where column value is found more than once in a multidimensional array
(4 answers)
Closed 9 months ago.
Referring to this question & this, i have a little different problem here.
I have an array like this:
Array
(
[0] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1201"
)
[1] => Array
(
[name] => "Size"
[value] => "Small"
[id] => "1203"
)
[2] => Array
(
[name] => "Size"
[value] => "Medium"
[id] => "1204"
)
[3] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1205"
)
[4] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1206"
)
[5] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1207"
)
)
Above array have repetition of Large Three times, i want to identify unique on key based value. and remove that index (0,1,2,3,4,5) from that array.
Mentioned questions contains problems like this, but not the exact problem i am facing.
I am trying like this:
array_map("unserialize", array_unique(array_map("serialize", $input)));
but not working.
Since you have not answered my question yet I assume "id" is irrelevant.
By using array_column to make the array associative on "value" and it will delete any duplicates, then array_values will reset the keys to indexed.
This way you don't have to loop at all.
$arr = array_values(array_column($arr, NULL, "value"));
var_dump($arr);
output:
array(3) {
[0]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Large"
["id"]=>
string(4) "1207"
}
[1]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Small"
["id"]=>
string(4) "1203"
}
[2]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(6) "Medium"
["id"]=>
string(4) "1204"
}
}
https://3v4l.org/aOhVS
If you want to keep the lowest "id", and the "id" is higher the further down in the array you go (as in your example), then you can use rsort($arr); before the code.
rsort($arr);
$arr = array_values(array_column($arr, NULL, "value"));
var_dump($arr);
output:
array(3) {
[0]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Small"
["id"]=>
string(4) "1203"
}
[1]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(6) "Medium"
["id"]=>
string(4) "1204"
}
[2]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Large"
["id"]=>
string(4) "1201"
}
}
https://3v4l.org/VtgAM
You could try make a foreach creating another array like you need
$arrayOrdenado = array();
foreach($array as $a){
$arrayOrdenado[$a["value"]][] = $a;
}

Accessing object inside array that is inside another object using PHP

I am new to CI. Using codeigniter, I am trying to print categories and their items as below:
Category 1 Name
item 1
item 2
Category 2 Name
item 3
item 4
item 5
and so on.
I have build an array and subarray inside controller as below:
$data['categories'] = $this->publicpages_model->getcategories();
foreach($data['categories'] as $category)
{
$data['categories'][$category->cID]= $this->publicpages_model->getitems($category->cID);
}
In the view, I am trying to use the following code but I am not able to complete it to get the desired output as discussed above.
foreach($categories AS $category)
{
echo '<h1>' . $category->name . '</h1>';
//Missing code to print the items
echo "<br>";
}
Although the name of category is printed but I am also getting the following error:
Severity: Notice
Message: Trying to get property of non-object
print_r($categories) gives following result:
Array ( [0] => stdClass Object ( [cID] => 1 [name] => Breakfast ) [1] => Array ( [0] => stdClass Object ( [itemID] => 1 [name] => Aaloo Paratha [description] => descriptio 1 [menu] => 1 [price] => 22 [tax] => 20 [sStatus] => ) ) [2] => stdClass Object ( [cID] => 7 [name] => Fast Food ) [5] => Array ( [0] => stdClass Object ( [itemID] => 5 [name] => Missi Roti [description] => Hot and Crunchy [menu] => 5 [price] => 5 [tax] => 1 [sStatus] => 1 ) ) [7] => )
vardump gives following output:
array(5) { [0]=> object(stdClass)#22 (2) { ["cID"]=> string(1) "1" ["name"]=> string(9) "Breakfast" } [1]=> array(1) { [0]=> object(stdClass)#25 (7) { ["itemID"]=> string(1) "1" ["name"]=> string(13) "Aaloo Paratha" ["description"]=> string(12) "descriptio 1" ["menu"]=> string(1) "1" ["price"]=> string(2) "22" ["tax"]=> string(2) "20" ["sStatus"]=> string(0) "" } } [2]=> object(stdClass)#24 (2) { ["cID"]=> string(1) "7" ["name"]=> string(9) "Fast Food" } [5]=> array(1) { [0]=> object(stdClass)#26 (7) { ["itemID"]=> string(1) "5" ["name"]=> string(10) "Missi Roti" ["description"]=> string(15) "Hot and Crunchy" ["menu"]=> string(1) "5" ["price"]=> string(1) "5" ["tax"]=> string(1) "1" ["sStatus"]=> string(1) "1" } } [7]=> bool(false) }
Please help with the solution.
You have a little problem there in your code. What you are trying to do is fine, but how you do it is the problem.
In this foreach loop, you are modifying the same array which you loop; hence it breaks the structure of the $data['categories'] array completely. As a result of that, your second element of the array is not having a name key, so it throws that warning.
foreach($data['categories'] as $category)
{
$data['categories'][$category->cID] = ...;
}
If you were trying to get the subitems of each category, and add them as a new key, you need to modify the $category array. Not the outer array :) So change it like this.
foreach($data['categories'] as $category)
{
$category->subItems = $this->publicpages_model->getitems($category->cID);
}
Or if you wanted the $category->cID instead of the key subItems, you can change the above to this:
foreach($data['categories'] as $category)
{
$category->{$category->cID} = $this->publicpages_model->getitems($category->cID);
}
I hope it helps :) feel free to ask anything if it's not clear.
imho what you are doing makes no sense - you should add the items to the category
try the following instead
your controller
$data['categories'] = $this->publicpages_model->getcategories();
foreach($data['categories'] as $category)
{
$category->arrItems = $this->publicpages_model->getitems($category->cID);
}
your view
foreach($categories AS $category)
{
echo '<h1>' . $category->name . '</h1>';
if (isset($category->arrItems) && is_array($category->arrItems))
{
foreach($category->arrItems AS $objItem)
{
print_r($objItem);
}
}
echo "<br>";
}

Access to PHP array elements

I have a PHP array when I used var_dump() this is the result I get:
array(1) { ["GetVehicleConfigurationByVehicleIdResult"]=> array(9) { ["Id"]=> string(1) "2" ["VIN"]=> NULL ["Year"]=> array(2) { ["Id"]=> string(4) "2006" ["Value"]=> string(4) "2006" } ["Make"]=> array(2) { ["Id"]=> string(1) "2" ["Value"]=> string(5) "Acura" } ["Model"]=> array(2) { ["Id"]=> string(1) "2" ["Value"]=> string(2) "TL" } ["Trim"]=> array(2) { ["Id"]=> string(6) "268650" ["Value"]=> string(12) "3.2 Sedan 4D" } ["Mileage"]=> string(6) "100000" ["OptionalEquipment"]=> array(1) { ["EquipmentOption"]=> array(35) { [0]=> array(13) { ["DisplayName"]=> string(19) "V6, VTEC, 3.2 Liter" ["VehicleOptionId"]=> string(3) "204" ["IsSelected"]=> string(4) "true" ["OptionTypeDisplayName"]=> string(6) "Engine" ["OptionGroupName"]=> string(3) "N/A" ["DisplayNameAdditionalData"]=> string(3) "N/A" ["ManufactureCode"]=> string(0) "" ["OptionAvailabilityDisplayName"]=> string(3) "N/A" ["IsDefaultConfiguration"]=> string(4) "true" ["DetailName"]=> string(3) "N/A" ["NonBoldName"]=> string(3) "N/A" ["Footer"]=> string(3) "N/A" ["SortOrder"]=> string(4) "1000" }
How I can get the elements from this array?
Some of the elements are complex, like they are array inside array.
That is the formatted print out of the array to understand better:
Array
(
[GetVehicleConfigurationByVehicleIdResult] => Array
(
[Id] => 2
[VIN] =>
[Year] => Array
(
[Id] => 2006
[Value] => 2006
)
[Make] => Array
(
[Id] => 2
[Value] => Acura
)
[Model] => Array
(
[Id] => 2
[Value] => TL
)
[Trim] => Array
(
[Id] => 268650
[Value] => 3.2 Sedan 4D
)
[Mileage] => 100000
[OptionalEquipment] => Array
(
[EquipmentOption] => Array
(
[0] => Array
(
[DisplayName] => V6, VTEC, 3.2 Liter
[VehicleOptionId] => 204
[IsSelected] => true
[OptionTypeDisplayName] => Engine
[OptionGroupName] => N/A
[DisplayNameAdditionalData] => N/A
[ManufactureCode] =>
[OptionAvailabilityDisplayName] => N/A
[IsDefaultConfiguration] => true
[DetailName] => N/A
[NonBoldName] => N/A
[Footer] => N/A
[SortOrder] => 1000
)
I want to get: Id, VIN, Year, Make, Model, Trim,Mileage and OptionalEquipment and pass them as 1 single parameter to another method.
It solved:
$Id = $resultVehicleId['GetVehicleConfigurationByVehicleIdResult']['Id'];
$Year = $resultVehicleId['GetVehicleConfigurationByVehicleIdResult']['Year']['Value'];
You are correct. There are arrays inside of arrays here. And var_dump shows it very nicely so you can perfectly know how to navigate the levels of this multi-dimensional array.
If you want VIN just get $array['GetVehicleConfigurationByVehicleIdResult']['VIN']
For Year you need to get $array['GetVehicleConfigurationByVehicleIdResult']['Year']['Value']
I think you can guess the others now.
They are just array elements no matter how deep you go so you can just reference them by name like below:
$id = $that_array['GetVehicleConfigurationByVehicleIdResult']['Id'];
$id = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Id'];;
$vin = $array_name["GetVehicleConfigurationByVehicleIdResult"]['VIN'];
$year = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Year']
$make = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Make'];
$model = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Model'];
$trim = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Trim']['Value'];
$mileage = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Mileage'];
$optional_equipment = $array_name["GetVehicleConfigurationByVehicleIdResult"]['OptionalEquipment']['EquipmentOption'][0]['DisplayName'];
For simplicity's sake I'm assuming this array is saved as $array.
You can access the data from the array like this:
$vin = $array['GetVehicleConfigurationByVehicleIdResult']['VIN'];
But you stated that you want to pass them all as a single parameter, so to do that you would probably want to just pass an array.
someFuntion($array['GetVehicleConfigurationByVehicleIdResult']);

Exclude data from brackets using regex (preg_match_all)

Input string:
:txt{sometext}:alpha
I want to extract data like this (extracted from brackets):
Result using preg_match_all():
sometext
Trying like this, but none of this works:
php > preg_match_all('/^(\:txt)(.*)+(\{)(.*)+(\})/i', ':txt{sometext}:alpha', $m); var_dump($m);
array(6) {
[0] =>
array(1) {
[0] =>
string(14) ":txt{sometext}"
}
[1] =>
array(1) {
[0] =>
string(1) ":"
}
[2] =>
array(1) {
[0] =>
string(0) ""
}
[3] =>
array(1) {
[0] =>
string(1) "{"
}
[4] =>
array(1) {
[0] =>
string(0) ""
}
[5] =>
array(1) {
[0] =>
string(1) "}"
}
}
Note: as sample I have like this :txt{sometext}:alpha:another{mydata}, so I can extract data from :another and give results like mydata.
RESULTS:
Result from Sniffer:
php > preg_match_all('/(?<=:txt{)([^}]+)(?=})/', ':txt{sometext}:alpha', $x); var_dump($x);
array(2) {
[0] =>
array(1) {
[0] =>
string(8) "sometext"
}
[1] =>
array(1) {
[0] =>
string(8) "sometext"
}
}
Result from Jerry:
php > preg_match_all('/^:txt\{([^}]+)\}/', ':txt{sometext}:alpha', $x); var_dump($x);
array(2) {
[0] =>
array(1) {
[0] =>
string(14) ":txt{sometext}"
}
[1] =>
array(1) {
[0] =>
string(8) "sometext"
}
}
Why all this, why not just:
(?<=:txt{)([^}]+)(?=})
Regex101 Demo

need preg_match_all links

i have a string like this one:
$string = "some text
http://dvz.local/index/index/regionId/28
http://stuff.kiev.ua/roadmap_page.php http://192.168.3.192/roadmap_page.php
http://192.168.3.192/roadmap_page.php#qwe";
need to get all links.
i tried this way: /http:\/\/(.*)[|\s]?/
returns:
array(2) {
[0] =>
array(3) {
[0] =>
string(42) "http://dvz.local/index/index/regionId/28\r\n"
[1] =>
string(77) "http://stuff.kiev.ua/roadmap_page.php http://192.168.3.192/roadmap_page.php\r\n"
[2] =>
string(41) "http://192.168.3.192/roadmap_page.php#qwe"
}
[1] =>
array(3) {
[0] =>
string(34) "dvz.local/index/index/regionId/28\r"
[1] =>
string(69) "stuff.kiev.ua/roadmap_page.php http://192.168.3.192/roadmap_page.php\r"
[2] =>
string(34) "192.168.3.192/roadmap_page.php#qwe"
}
}
EDIT 1:
expect:
array(2) {
[0] =>
array(3) {
[0] =>
string(42) "http://dvz.local/index/index/regionId/28"
[1] =>
string(77) "http://stuff.kiev.ua/roadmap_page.php"
[2] =>
string(77) "http://192.168.3.192/roadmap_page.php"
[3] =>
string(41) "http://192.168.3.192/roadmap_page.php#qwe"
}
[1] =>
array(3) {
[0] =>
string(34) "dvz.local/index/index/regionId/28"
[1] =>
string(69) "stuff.kiev.ua/roadmap_page.php"
[2] =>
string(69) "192.168.3.192/roadmap_page.php"
[3] =>
string(34) "192.168.3.192/roadmap_page.php#qwe"
}
}
Try this one:
/http:\/\/([^\s]+)/
Try this:
preg_match_all('|http://([^\s]*)|', $string, $matches);
var_dump($matches);
All links from text
http[s]?[^\s]*
Numerous pages have only relative links to the main document, (thus no http(s):// ... to parse), for those the following works fine, splitting by the href attribute:
preg_match_all('|href="([^\s]*)"><\/a>|', $html, $output_array);
Or even simpler:
preg_match_all('|href="(.*?)"><\/a>|', $html, $output_array);
Example output:
[0]=>
string(56) "/broadcast/bla/xZr300"
[1]=>
string(50) "/broadcast/lol/fMoott"

Categories